Arrays and pointers for programming exercises

Source: Internet
Author: User

Arrays and pointers

Read the code below to see why something went wrong.

1 intMain () {2     CharA[] = {"I am a bad boy" }; 3     Char* PA =New Char[sizeof(a)]; 4PA =A; 5       6      for(size_t i =0; I <sizeof(a); ++i) {7Std::cout << *pa <<Std::endl; 8++PA; 9     }  Ten     Delete[] PA;  OnePA =NULL;  A     return 0;  -}

We named the Magic String X.

The first step is to put the string x into the stack space, and the second step is to request a piece of memory in the heap that can hold just the X. The above two steps are quite right, but the third step is very problematic.

The intent is to attempt to copy X into the heap memory, but the result of the actual operation is to point the pointer to the stack space where the original x resides. This will result in a memory leak. (Note that the equals sign of the string class can be assigned because of an overload of the operator). The next step is to traverse the entire string, as if the result of the copy succeeds, but you are actually traversing the x that exists in the stack space. The value that the pointer points to is also constantly modified by the traversal, because the string is allocated at the end of an empty string (equivalent to null or 0), and the pointer eventually points to an empty string at the end of the x of the stack space. Not to mention the explosion caused by an attempt to release the stack space.

How does the code change correctly?

1 intMain () {2     CharA[] = {"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) {6Std::cout << * (PA +i); 7     }  8     Delete[] PA; 9PA =NULL; Ten     return 0;  One}

Copying a string into the heap memory allocation space requires the C function strcpy, which copies the values past. You can also assigned the value of every word in X. Note that you should try not to change the pointer to the first position of the heap memory if you are sure to revert back. Release must start from the first part of the heap memory, or it will cause an explosion.

You may be ignoring these issues during the period.

(1)

1 ++pa;

Already mentioned above, do not grasp the release is the first words try not to change the direction of the pointer. You can use the pointer's offset or array subscript to manipulate

(2)

1 Char New Char

Note that strlen does not count for empty characters, so there is less space in the heap memory allocation for empty characters, resulting in many functions not stopping reading at the end of this character.

1 Char New Char 1

Allocate one more, or you can use sizeof (note that sizeof points to the heap memory pointer to the size of the pointer).

(3)

You might be releasing the array heap memory like this.

1 Delete PA;  

Compile and run without any problems. But this is an undefined behavior (UB), the results may be different on different platforms or compilers, in short this is a potentially dangerous operation, you should let yourself strictly use delete[] to release the array heap memory.

Postscript:

Refers to the string overloaded equals sign. For people who are familiar with C + +, there will be no problem in this area, but for beginners this may indeed not be a good understanding stir understand. I've never used the overloaded operator, and maybe it has something to do with it.

Arrays and pointers for programming exercises

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.