Programming Exercise array and pointer, programming array pointer
Array and pointer
Read the following code to identify the error.
1 int main() { 2 char a[] = { "I am a bad boy" }; 3 char * pA = new char[ sizeof( a ) ]; 4 pA = a; 5 6 for ( size_t i = 0; i < sizeof( a ); ++i ) { 7 std::cout << *pA << std::endl; 8 ++pA; 9 } 10 delete [] pA; 11 pA = NULL; 12 return 0; 13 }
We name the magic string x.
The first step is to store string x in the stack space; the second step is to apply for a memory that can store x in the heap space. The above two steps are correct, but the third step is quite problematic.
The intention is to copy x into the heap memory, but the result of the actual operation is to direct the pointer to the stack space where the original x is located. This will cause a memory leak. (It should be noted that the equal sign of the string class can be assigned due to the heavy load of the operator ). The next step is to traverse the entire string, and the running result is as if the copy was successful, but you are actually traversing the x that exists in the stack space. The pointer pointing value is constantly modified during traversal, because an empty string (equivalent to NULL or 0) is automatically added at the end of the allocation ), the pointer finally points to the null string at the end of x in the stack space. The explosion caused by an attempt to release the stack space is not to mention.
How can I modify the code correctly?
1 int main() { 2 char a[] = { "I am a bad boy" }; 3 char * pA = new char[ sizeof( a ) ]; 4 strcpy( pA, a ); 5 for ( size_t i = 0; i < sizeof( a ); ++i ) { 6 std::cout << *( pA + i ); 7 } 8 delete [] pA; 9 pA = NULL; 10 return 0; 11 }
To copy a string to the heap memory allocation space, use the c function strcpy to copy the value. You can assign values to each character of x. Note: Do not change the pointer to the first position of the heap memory if you are sure to restore it back. Release must start from the first part of the heap memory, otherwise it will also cause an explosion.
During this period, you may ignore these issues.
(1)
1 ++pA;
As mentioned above, do not change the pointer direction if you are not sure to release first. You can use the pointer offset or array subscript for operations.
(2)
1 char * pA = new char[ strlen( a ) ];
Note that strlen does not include null characters. Therefore, when the heap memory is allocated, there will be less space to store null characters. As a result, many functions cannot stop reading at the end of the character.
1 char * pA = new char[ strlen( a ) + 1 ];
Allocate one more resource, or you can use sizeof (note that sizeof points to the heap memory pointer to the pointer size ).
(3)
You may release the array heap memory in this way.
1 delete pA;
Compile and run without any problems. However, this is an undefined behavior (UB). Running results on different platforms or compilers may be different. In short, this operation is a potentially dangerous operation, you should strictly use delete [] to release the array heap memory.
Postscript:
The equal sign of the string overload is mentioned. People familiar with C/C ++ will not have any problems in this regard, but they may not be very easy to understand and understand for beginners. I have never used the overload operator. It may be related to this.