C + + memory management

Source: Internet
Author: User

One, memory allocation method

1. Allocate from the static storage area. Memory is allocated at the time of program compilation, and the entire running period of the program is present in this block. For example, global variables, static variables.

2. Create on the stack. When executing a function, the storage units of local variables within the function can be created on the stack, which are automatically freed when the function is executed at the end. The stack memory allocation operation is built into the processor's instruction set and is highly efficient, but allocates limited memory capacity.

3. Created on the heap, also known as dynamic memory allocation. When the program is running, it uses malloc or new to request any amount of memory, and the programmer is responsible for freeing the memory with free or delete. The lifetime of dynamic memory is determined by us and is very flexible to use, but the problem is the most.

Second, common memory errors and their countermeasures

Typically, a memory error occurs when the program is running, and the compiler cannot automatically discover it. And most of these errors are not obvious symptoms, and the hidden, increased the difficulty of error. Common memory errors and countermeasures are as follows:

1. The memory allocation was unsuccessful, but it was used.

Novice programmers often make this mistake, primarily because they are unaware of the success of the memory allocation, and the common workaround is to check if the pointer is NULL before using memory, and if the pointer p is the function's argument, use ASSERT (P! = NULL) At the entrance of the function. If you are using malloc or new to request memory, you should use if (P = = null) or if (P! = NULL) for error-proof handling.

2. The memory allocation succeeds, but it is not initialized to reference it.

One of the main causes of this error is the concept of no initialization, and the mistaken idea that the default initial value of the memory is all 0, resulting in a reference to the initial error (for example, an array).

3. The memory allocation succeeds and has been initialized, but the operation crosses the memory boundary.

For example, the following table "more 1" or "1 less" occurs frequently when using arrays. Especially in a For loop statement, the number of loops can be easily mistaken, resulting in array operations being out of bounds.

4. Forget to release memory, causing memory leaks.

The function that contains this error loses one piece of memory each time it is called. At first, the system has plenty of memory and you can't see the error. One day the program will suddenly die, the system appears prompt: memory exhaustion.

The application and release of dynamic memory must be paired, the use of malloc in the program must be communicated with the number of free, otherwise there must be errors. Of course new/delete.

5. Release the memory but continue to use it.

There are several cases where the return statement of the function returns an error, note that the "stack memory" pointer or reference is not returned because the memory is automatically destroyed at the end of the function body, and the second is that no pointer is set to NULL when free or delete is used, resulting in a "wild pointer ”。

Thirdly, it is recommended

1. After requesting memory with malloc or new, you should immediately check if the pointer is null. Prevents the use of memory with a pointer value of NULL.

2. Do not forget to assign an initial value to an array or dynamic memory.

3. Avoid the array or pointer subscript out of bounds.

4. The application and release of dynamic memory must be paired to prevent memory leaks.

5. After freeing memory with free or delete, be careful to set the pointer to NULL to prevent "also pointer".

Thanks to the C + + high-quality Programming Guide, I would like to remember and thank the author.

C + + memory management

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.