Pit in the C++ learning process-continuous update

This is the pit I encountered in the process of learning C++

Pit 1:

1.1 Problem description:

When using the new operator to allocate memory to a character array, garbled characters will appear when using cout to output. The code is as follows:

#include <iostream>

using namespace std;

int main()
{
    
    
	char* pChar;

	pChar = new char[5];

	int i = 0;
	while (i < 5)
	{
    
    
		cin >> pChar[i];
		i++;
	}

	cout << pChar << endl;

	return 0;
}

pChar is a character pointer. After using the new operator to allocate 5 char lengths, use cin for input, so that pChar is a character array containing 5 chars.
but(Important point)
When using cout for output, there will be garbled characters. Although the characters we input can also be output, a bunch of garbled characters will follow. As shown below:
Enter fqwer, the first few characters of the output are also fqwer, but a bunch of garbled characters follow

1.2 Cause of the problem:

cost——Yes, it's cout!
In C++, when cout outputs a character array,The identifier for judging the end of the output is a null character, Which is'\0'. But when we input, there is no empty character at the end of the pChar array, so cout cannot determine when the output ends. In other words, after the pChar is output, cout will continue to output the data in the memory immediately following pChar+4 because it cannot find the trailing empty character. No one knows what is in that memory, what is possible nothing. So there will be garbled characters.
The output results in the following figure:
Insert picture description here

1.3 Solution

1.3.1 Remove cyclic input

You can change the code to this:

#include <iostream>

using namespace std;

int main()
{
    
    
	char* pChar;

	pChar = new char[5];

	cout << "请输入5个字符:";
	cin >> pChar;

	for (int i = 0; i < 5; i++)
	{
    
    
		cout << "pChar[" << i << "]" << "的值为:" << pChar[i] << "------";
		cout << "pChar[" << i << "]" << "的地址为:" << &pChar + i << endl;
	}
	cout << "pChar的最后一个元素的后面一个内存的地址为:" << &pChar + 5 << ", 其值为:" << pChar + 5 << endl;

	cout <<"直接输出pChar的结果为:"<< pChar << endl;


	return 0;
}

The output of this is:
Insert picture description here

Cause Analysis:

Do not use cyclic input, directly transfer the judgment to cout. When we press Enter after inputting, cout will directly read 5 characters in sequence and store them in pChar. Even if the input length is greater than the length of pChar, only 5 characters will be stored in pChar.

1.3.2 Add null characters when looping input

code show as below:

#include <iostream>

using namespace std;

int main()
{
    
    
	char* pChar;

	pChar = new char[6];

	cout << "请输入5个字符:";
	for (int i = 0; i < 5; i++)
		cin >> pChar[i];
	pChar[5] = '\0';

	for (int i = 0; i < 5; i++)
	{
    
    
		cout << "pChar[" << i << "]" << "的值为:" << pChar[i] << "------";
		cout << "pChar[" << i << "]" << "的地址为:" << &pChar + i << endl;
	}
	cout << "pChar的最后一个元素的后面一个内存的地址为:" << &pChar + 5 << ", 其值为:" << pChar + 5 << endl;

	cout <<"直接输出pChar的结果为:"<< pChar << endl;


	return 0;
}

When adding a null character, you need to allocate a char memory when new allocates memory to store'\0', so that cout will judge the end of the output when it reads'\0' when directly outputting, so it will not appear Garbled.
The output of the program is as follows: I
Insert picture description here
personally feel that handing over control to cout is a very unsafe behavior. I have not yet come up with relevant examples to illustrate this point, but since people control the computer, the control should definitely be in people’s hands. So the author thinks that it is better to add a null character at the end of solution 2.

Pit 2:

2.1 Problem description:

When using operator overloading subscript operator, what if the object is a two-dimensional array?

In fact, this is not a problem, but a very interesting problem encountered by the author when I stepped on the pit. The program I wrote can output normally, but it is not output according to my own understanding.
The program code is as follows:

#include <iostream>
using namespace std;

class A
{
    
    
public:
	int _ia[3][4];

	void putData()
	{
    
    
		for (int i = 0; i < 3; i++)
		{
    
    
			for(int j=0;j<4;j++)
				cin >> _ia[i][j];
		}
	}

	int* operator[](int iIndex)
	{
    
    
		return _ia[iIndex];
	}
	int operator[](int* jIndex)
	{
    
    
		return *jIndex;
	}
};

int main()
{
    
    
	A testa;
	testa.putData();

	for (int i = 0; i < 3; i++)
	{
    
    
		for (int j = 0; j < 4; j++)
			cout << testa[i][j]<<"\t";
		cout << endl;
	}

	return 0;
}

// 1 2 3 4 5 6 7 8 9 10 11 12

There is a member variable of a two-dimensional array in class A in the code. Now I want to implement C++ to get the value of a two-dimensional array by overloading the subscript operator. which is:

class A{
    
    };
type c = A[i][j];

This kind of operation will make the use of class A very convenient. Therefore, the subscript operator must be overloaded.
But the subscript operator is a unary operator, and there is only one input parameter. So what to do? We all know that a two-dimensional array can be regarded as a one-dimensional array, but the element of the one-dimensional array is a pointer. Then the problem is easy to handle, reload!
as follows:

	int* operator[](int iIndex)
	{
    
    
		return _ia[iIndex];
	}

The _ia in the code is a two-dimensional array in class A, so for the overloaded subscript operator, first pass in an integer to get the row element of the two-dimensional array, and the row element is a pointer of type int. After obtaining the pointer, then reload it!
code show as below:

	int operator[](int* jIndex)
	{
    
    
		return *jIndex;
	}

(Manually funny<-<-)

Overload the subscript operator for the second time, pass in a pointer of type int, and output the value of that type.I laughed myself up to this point. .
Well, it looks normal, but it's not normal at all. Some big guys may think it looks abnormal at a glance.
But the program can output normally, the output part of the code is as follows:

	A testa;
	testa.putData();

	for (int i = 0; i < 3; i++)
	{
    
    
		for (int j = 0; j < 4; j++)
			cout << testa[i][j]<<"\t";
		cout << endl;
	}

Since the program does not look normal, why can it output normally?

The reasons are as follows:

testa is an instantiated object of class A, so testa[i] calls the overloaded operator in class A, and the return result is a pointer of type int, and this pointer is also an element of testa's member variable _ia. Then, (_ia [i ]) [j] outputs the value of the i-th row and j-th column of _ia.
As for the overloaded in class A

	int operator[](int* jIndex)
	{
    
    
		return *jIndex;
	}

This subscript operator is actually useless,It's SHIT at all
Insert picture description here
======================= Dividing line-after encountering a pit, then update ================= ===

Guess you like

Origin blog.csdn.net/GeomasterYi/article/details/106728267