On the problems needing attention in using pointers in C + + _c language

Source: Internet
Author: User

First, use the pointer when you need to pay attention to several points:

• Allocate space

• initialization

• Release

Two, common mistakes are several:

1 The memory allocation was unsuccessful, but it was used

Novice programmers often make this mistake because they are unaware that memory allocations will be unsuccessful. A common workaround is to check whether the pointer is null before using memory.

If the pointer p is an argument to a function, check with assert (P!= NULL) at the entrance to the function. If you are using malloc or new to request memory, you should use if (P = = null) or if (P!= null) for misplaced processing.

2 The memory allocation is successful, but it has not been initialized to reference it

There is a major cause of this error: first, there is no idea of initialization; the second is to mistakenly assume that the default initial value of memory is zero, resulting in a reference to the initial error (eg. Char p[]={' yes '}. The default initial value of the memory is not a uniform standard, although sometimes zero, so no matter how to create an array, do not forget to assign the initial value, that is, assign 0 values can not be omitted, do not bother.

3 The memory allocation was successful and initialized, but the operation crossed the bounds of the memory

For example, using an array is a frequent occurrence of subscript " 1" or "less 1", especially in a For loop statement, where the number of loops is easily mistaken, causing the array to operate out of bounds.

4 forgot to release memory, causing memory leak

A function that contains this error loses a piece of memory every time it is called, and the system has sufficient memory at first. You can't see the mistake. When the program occupies a large amount of memory, the system prompts: memory is running out. Dynamic memory application and release must be paired, in the program malloc and free usage must be the same, otherwise there must be a mistake

5 frees up memory but continues to use it

In this case, there are three reasons why the object invocation relationship in the:<1> program is too complex to find out whether an object has freed up memory, then the data structure should be redesigned to fundamentally solve the confusion of object management. The return statement for the <2> function is wrong, and be careful not to go back to "stack memory" pointer or "reference" because the memory is automatically destroyed at the end of the function body. <3> after freeing memory with free or delete, the pointer is not set to null. resulting in "wild pointer"

[Rule 1] after memory is requested with malloc or new, the pointer value is immediately checked for null. Prevents the use of memory with a pointer value of NULL.

[Rule 2] do not forget to assign an initial value to an array and dynamic memory. Prevents unused memory from being initialized as a right value.

[Rule 3] avoid the subscript of numbers or pointers being crossed, especially beware of "more 1" or "less 1" operations

[rule 4] the application and release of dynamic memory must be paired to prevent memory leaks

[Rule 5] after freeing memory with free or delete, set the pointer to null immediately to prevent the "wild pointer"

Above this article on the use of pointers in C/s + + to pay attention to the problem is small series to share the whole content of everyone, hope to give you a reference, but also hope that we support the cloud habitat community.

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.