It's a cliche that C and C++ memory management

  • 2020-05-19 05:22:37
  • OfStack

Memory allocation

Introduction to the

In C++, memory is divided into five zones: heap, stack, free storage, global/static storage, and constant storage.

Stack: when a function is executed, the storage units of local variables within the function can be created on the stack, and these storage units are automatically released at the end of the function execution. Stack memory allocation operations are built into the processor's instruction set, which is very efficient, but the allocated memory capacity is limited.

Heap: a block of memory allocated by new that is left unattended by the release compiler and controlled by our application. One new corresponds to one delete. If the programmer is not released, the operating system will automatically recycle the program when it is finished.

Free storage: a block of memory allocated by malloc et al., which is similar to the heap at 10 but ends its life with free.

Global/static storage: global variables and static variables are allocated to the same block of memory. In the previous C language, global variables were divided into initialized and uninitialized. There is no such distinction in C++, they occupy the same block of memory together.

Constant storage: this is a special storage area, they are stored in a constant, do not allow modification.

Common memory errors and their countermeasures

Memory errors can be very troublesome. The compiler does not automatically detect these errors and usually catches them while the program is running. Most of these errors are asymptomatic and come and go, making it more difficult to correct them. Sometimes the user comes to you in a huff, and there's nothing wrong with the program.

Common memory errors and their countermeasures are as follows:

Memory allocation failed, but it was used. Novice programmers often make this mistake because they don't realize that memory allocation will be unsuccessful. A common solution is to check that the pointer is NULL before using memory. If the pointer p is an argument to a function, use assert(p! =NULL) check. If you are using malloc or new to request memory, you should use if(p==NULL) or if(p! =NULL) for error prevention.

Memory allocation is successful, but it is referenced before it is initialized. There are two main causes of this error: 1. 2 is the mistake of thinking that the default initial value of memory is all zero, resulting in a reference error to the initial value (such as an array). There is no universal standard for what the default initial value of memory is, and although it is sometimes zero, we would rather believe it. So no matter how you create an array, don't forget to assign an initial value, even if it's zero.

The memory allocation was successful and initialized, but the operation crossed the memory boundary. For example, subscript "1 more" or "1 less" often occurs when using arrays. In particular, in for looping statements, it is easy to get the number of loops wrong, causing array operations to cross the line.

Forgetting to free memory, causing a memory leak. A function with this error loses 1 block of memory every time it is called. At the beginning, the system has enough memory, you can't see the error. There is always a time when the program dies suddenly and the system is prompted: memory is exhausted. Application and release of dynamic memory must be matched, and malloc and free must be used the same number of times 1 in the program, otherwise there must be an error (same for new/delete).

Free up memory and continue to use it.

There are three situations:

(1). The object call relationship in the program is too complex, it is difficult to figure out whether a certain object has freed the memory, at this time should redesign the data structure, fundamentally solve the chaos of object management.

(2). The return statement of the function is miswritten. Be careful not to return "pointer" or "reference" to "stack memory", because the memory is automatically destroyed when the function body ends.

(3). After using free or delete to free memory, the pointer is not set to NULL. Result in "wild pointer".

So how do you avoid wild Pointers? Here is a list of 5 rules, usually when writing a program to pay more attention to 1, develop a good habit.

Rule 1: after applying for memory with malloc or new, you should check whether the pointer value is NULL immediately. Prevents the use of memory with a pointer value of NULL.

Rule 2: don't forget to assign initial values for arrays and dynamic memory. Prevents uninitialized memory from being used as an rvalue.

Rule 3: avoid array or pointer subscripts out of bounds, especially when "1 more" or "1 less" operations occur.

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

Rule 5: when you free up memory with free or delete, set the pointer to NULL immediately to prevent "wild Pointers."


Related articles: