For beginners, memory is a mysterious space. Most of the procedures are incorrect in memory, and these errors are hidden. Therefore, how to master the use of memory, knowing the system's management means for memory, will be a very critical factor in software success.
First we have to understand memory allocation. In general, there are three ways to distribute the memory:
1. Assign from static storage area. There is already a hidden when there is program compilation, and there is existence of the entire running period of the program. For example, global variables, static variables.
2. Create on the stack. When performing a function, the storage unit of local variables within the function can be created on the stack, and the memory cells are automatically released at the end of the function. The stack memory allocation operation is within the command set of the processor, the efficiency is high, but the assigned memory capacity is limited.
3. Allocate from the pile, also known as dynamic memory allocation. The program applies for any number of memory with Malloc or New when running, and the programmer will release the memory with free or delete. Dynamic memory survival is determined by us, it is very flexible, but the problem is most.
The above three allocations, we have to pay attention to the problem of memory life:
1. The life phase of the static allocation is the entire software period, that is, from the software running to the software termination exit. This memory will be recycled by the system after the software is terminated.
2. The life of the space allocated in the stack is related to the functions and classes of this variable. If it is a local variable defined in a function, its life is called when the function is called, and if the function runs, then this memory will be reclaimed. If it is a member variable in the class, its life period is the same as the life of class instance.
3. The memory allocated on the heap is starting from calling new or malloc, and the DELETE or FREE is called. If you don't drop DELETE or FREE. Then this space must be recycled by the system after the software is running.
Let's take a look at it. In the process of using memory, we often happen what errors. And what countermeasures should we take.
It is very troublesome to have a memory error. The compiler cannot automatically discover these errors, usually captured when the program is running. Most of these errors do not have obvious symptoms, and sometimes the difficulty of inclination is increased. Sometimes the user is angry, and the program has not happened, you will walk, the mistake is also episodes.
Common memory errors and their countermeasures are as follows:
1 The memory allocation is unsuccessful, but it uses it.
Programming newcomers often make this mistake because they don't realize that memory allocation will be unsuccessful. Commonly used solution is to check if the pointer is NULL before using the memory. If the pointer P is the parameter of the function, use Assert (P! = NULL) to check at the entry of the function. If you are using Malloc or New to apply for memory, you should use if (p = null) or if (p! = Null).
2 Although the memory allocation is successful, it is not initialized to reference it.
There are two main causes of this mistake: one is the concept of uniniterated; the second is that the default initial value of memory is all zero, causing the reference initial value error (such as arrays).
The default initial value of memory does not have a unified standard, although some time is zero, we would rather believe that it is untrustworthy. So don't forget to assign initial values, even if you are ingredient, you should not be troublesome.
3 Memory allocation is successful and has been initialized, but the operation has crossed the boundary of the memory.
For example, when using an array, the subscript "more 1" or "less 1" is often generated. Especially in the For cycle statement, the number of cycles is easy to make mistakes, causing array to operate the offshore.
4 Forgot to release memory, resulting in memory leakage.
A function containing such an error is lost once a time is called once. At the beginning, the system's memory is sufficient, you can't see the mistake. I have finally dead, and the system prompts: memory consumption.
Dynamic memory application and release must be paired, the number of malloc and free usage will be the same, otherwise there must be an error (New / Delete.).
5 Release the memory but continue to use it.
Three cases:
(1) Object call relationship in the program is too complicated, and it is difficult to figure out whether an object has released memory. At this time, the data structure should be redesigned, fundamentally solve the chaotic situation of the object management.
(2) The RETURN statement of the function is wrong. Be careful not to return "pointer" or "reference" pointing to "Stack Ins), because the internal existence is automatically destroyed.
(3) After using Free or Delete releases the memory, the pointer is not set to NULL. Resulting in a "wild pointer".
In summary, we should pay attention to:
1. After using the Malloc or New to apply for memory, you should immediately check if the pointer value is NULL. Prevent memory using the pointer value of NULL.
2. Don't forget to pay initial values for array and dynamic. Prevent memory that will not be initialized as the right value.
3. Avoid the subsidiaries of array or pointers, especially "more 1" or "less 1" operation occurs.
4. The application and release of dynamic memory must be paired to prevent memory leakage.
5. After released the memory with free or delete, set the pointer to NULL to prevent "wild pointer".
Let's take a few classic mistakes. Don't make the same mistake:
1. Return to the store memory pointer
Char * getString (Void)
{
Char * p = "Hello World";
Return P;
}
Char * pget = getString ();
There is no error in this program, but there is no error, but you can't make the data you want to point to the "Hello World" you want, because the pointer P's life is the function getString, run the function getString After the P-assigned stack space is immediately recovered. Although the PGET point to the Memory address allocated at the PGET, the address has no content.
2. This is a very high frequency error.
Char * pchar = new char;
......
Int a;
PCHAR = & A;
......
Delete pchar;
Of course, this is an example, and the specific procedures are different.
This process has two problems. First, PCHAR = & A; will lead to the original space that PCHAR is unable to be acquired, just like our mobile phone number, you can't contact this friend again. This causes memory leakage. If the memory is more, it may cause the system to crash because the available resources will be less and less until it is exhausted. The second problem is that delete pchar will result in an exception, because Pchar is not pointing to dynamically assigned memory, but pointing to a stack space allocated, and the stack space cannot be recycled with delete, so it will result in Memory exception.
Memory is wealth, correct use of wealth is the key, this is the case, the program is the same.