Memory Management articles

Source: Internet
Author: User

Welcome to the memory minefield. The great Bill Gates once said:
640 K ought to be enough for everybody-Bill Gates 1981
Programmers often write memory management programs, so they are always worried. If you don't want to touch the mines, the only solution is to discover all the hidden mines and exclude them. The content in this chapter is much deeper than that in general textbooks. Readers need to carefully read this chapter to truly understand memory management.
7.1 Memory Allocation Method
There are three memory allocation methods:
(1) distribution from the static storage area. The program has been allocated when it is compiled, and the program exists throughout the entire runtime. For example, global variables and static variables.
(2) create a stack. When a function is executed, the storage units of local variables in the function can be created on the stack. When the function is executed, these storage units are automatically released. Stack memory allocation computation is built into the processor's instruction set, which is highly efficient, but the memory capacity allocated is limited.
(3) allocate from the stack, also known as dynamic memory allocation. When the program runs, it uses malloc or new to apply for any amount of memory. The programmer is responsible for releasing the memory with free or delete. The lifetime of the dynamic memory is determined by us. It is very flexible to use, but the problem is also the most.
7.2 memory errors and Countermeasures
Memory Errors are very troublesome. The compiler cannot automatically detect these errors, which can be captured only when the program is running. Most of these errors do not have obvious symptoms, but they are often invisible and increase the difficulty of error correction. Sometimes the user finds you angrily, but the program has not encountered any problems. When you leave, the error occurs again.
Common memory errors and their countermeasures are as follows:
U memory allocation fails, but it is used.
New programmers often make this mistake because they do not realize that memory allocation will fail. A common solution is to check whether the pointer is NULL before using the memory. If the pointer p is a function parameter, use assert (p! = NULL. If you use malloc or new to apply for memory, you should use if (p = NULL) or if (p! = NULL.
Although the u memory allocation is successful, it is referenced before initialization.
There are two main causes for this mistake: first, there is no idea of initialization; second, the default initial values of the memory are all zero, resulting in incorrect reference values (such as arrays ).
There is no uniform standard for the default initial values of the memory. Although sometimes it is zero, we prefer to trust it without any trust. Therefore, no matter which method is used to create an array, do not forget to assign the initial value. Even the zero value cannot be omitted, so do not bother.

The u memory has been allocated successfully and initialized, but the operation has crossed the memory boundary.
For example, when an array is used, the subscript "more than 1" or "Less 1" is often performed. Especially in for loop statements, the number of loops is easy to make a mistake, resulting in array operations out of bounds.

U forgets to release the memory, causing memory leakage.
A function containing such errors loses a piece of memory every time it is called. At the beginning, the system had sufficient memory and you could not see the error. Once a program suddenly died, the system prompts: memory is exhausted.
Dynamic Memory application and release must be paired. The usage of malloc and free in the program must be the same, otherwise there must be an error (the same applies to new/delete ).

U releases the memory but continues to use it.
There are three scenarios:
(1) The object calling relationship in the program is too complex, so it is difficult to figure out whether an object has released the memory. At this time, we should re-design the data structure to fundamentally solve the chaos of Object Management.
(2) The return Statement of the function is incorrect. Do not return "Pointer" or "Reference" pointing to "stack memory" because the function body is automatically destroyed when it ends.
(3) After the memory is released using free or delete, the pointer is not set to NULL. As a result, "Wild Pointer" is generated ".
L [Rule 7-2-1] after applying for memory with malloc or new, check whether the pointer value is NULL immediately. Prevents the use of memory with NULL pointer values.
L [Rule 7-2-2] Do not forget to assign initial values to arrays and dynamic memory. Avoid using uninitialized memory as the right value.
L [Rules 7-2-3] avoid overrunning the subscript of an array or pointer, especially when the "more 1" or "Less 1" Operation occurs.
L [Rule 7-2-4] dynamic memory application and release must be paired to prevent memory leakage.
L [Rules 7-2-5] after the memory is released with free or delete, the pointer is immediately set to NULL to prevent "Wild Pointer ".

7.3 comparison between pointers and Arrays
In C ++/C Programs, pointers and arrays can be replaced with each other in many places, which leads to the illusion that the two are equivalent.
An array is either created in a static storage area (such as a global array) or on a stack. The array name corresponds to (rather than pointing to) a piece of memory, and its address and capacity remain unchanged during the lifetime, only the content of the array can be changed.
A pointer can point to any type of memory block at any time. Its feature is "variable". Therefore, we often use pointers to operate dynamic memory. Pointers are far more flexible than arrays, but they are more dangerous.
The following uses a string as an example to compare the features of pointers and arrays.
7.3.1 modified content
In the example 7-3-1, the size of character array a is 6 Characters and its content is hello \ 0. The content of a can be changed, for example, a [0] = 'x '. The pointer p points to the constant string "world" (in the static storage area with the content of world \ 0). The content of the constant string cannot be modified. In terms of syntax, the compiler does not think that the statement p [0] = 'X' is inappropriate, but this statement attempts to modify the content of the constant string and causes a running error.
Char a [] = "hello ";
A [0] = 'X ';
Cout <a <endl;
Char * p = "world"; // note that p points to a constant string
P [0] = 'x; // the compiler cannot find this error.
Cout <p <endl;

Example 7-3-1 modify the array and pointer content
7.3.2 content replication and Comparison
The array name cannot be directly copied or compared. In Example 7-3-2, if you want to copy the content of array a to array B, you cannot use statement B = a. Otherwise, a compilation error is generated. Use the standard library function strcpy for replication. Similarly, if the content of B and a is the same, it cannot be determined by if (B = a). The standard library function strcmp should be used for comparison.
Statement p = a does not copy the content of a, but assigns the address of a to p. To copy the content of a, use the library function malloc as p to apply for a memory with a capacity of strlen (a) + 1 characters, and then use strcpy to copy strings. Similarly, the statement if (p = a) compares not the content but the address, and should be compared using the database function strcmp.
// Array...
Char a [] = "hello"; // strlen (a) => 5, sizeof (a) => 6. the strlen statistics array removes the \ 0 length, and the sizeof statistics array allocation length, including \ 0
Char B [10];
Strcpy (B, a); // B = a cannot be used;
If (strcmp (B, a) = 0) // if (B = a) cannot be used)
...
// Pointer...
Int len = strlen ();
Char * p = (char *) malloc (sizeof (char) * (len + 1 ));
Strcpy (p, a); // do not use p =;
If (strcmp (p, a) = 0) // do not use if (p =)
...

Example 7-3-2 copying and comparing the array and pointer content
7.3.3 computing memory capacity
The sizeof operator can be used to calculate the array capacity (number of bytes ). In Example 7-3-3 (a), the value of sizeof (a) is 12 (do not forget '\ 0 '). The pointer p points to a, but the value of sizeof (p) is 4. This is because sizeof (p) obtains the number of bytes of a pointer variable, which is equivalent to sizeof (char *) rather than the memory capacity referred to by p. C ++/C language cannot know the memory capacity referred to by the pointer unless you remember it when applying for memory.
Note: When an array is passed as a function parameter, the array will automatically degrade to a pointer of the same type. In Example 7-3-3 (B), sizeof (a) is always equal to sizeof (char *) regardless of the size of array *). // This question has been answered by Huawei. But I don't think it's really interesting.

Char a [] = "hello world ";
Char * p =;
Cout <sizeof (a) <endl; // 12 bytes
Cout <sizeof (p) <endl; // 4 bytes
Example 7-3-3 (a) Calculate the memory capacity of the array and pointer
Void Func (char a [1, 100])
{
Cout <sizeof (a) <endl; // 4 bytes instead of 100 bytes
}

Example 7-3-3 (B) the array degrades to a pointer
7.4 how does the pointer Parameter Pass the memory?
If the function parameter is a pointer, do not expect this pointer to apply for dynamic memory. In Example 7-4-1, the GetMemory (str, 200) Statement of the Test function does not enable str to obtain the expected memory. str is still NULL. Why?
Void GetMemory (char * p, int num)
{
P = (char *) malloc (sizeof (char) * num );
}
Void Test (void)
{
Char * str = NULL;
GetMemory (str, 100); // str is still NULL
Strcpy (str, "hello"); // running error
}

Example 7-4-1 try to apply for dynamic memory with pointer Parameters

Mao is in the GetMemory function. The compiler always needs to make a temporary copy for each parameter of the function. The copy of the pointer parameter p is _ p, and the compiler makes _ p = p. If the program in the function body modifies the content of _ p, the content of parameter p is modified accordingly. This is why pointers can be used as output parameters. In this example, _ p applied for a new memory, but changed the memory address indicated by _ p, but p was not changed at all. Therefore, the GetMemory function cannot output anything. In fact, each execution of GetMemory exposes a piece of memory, because the memory is not released with free.
If you have to use the pointer parameter to request memory, you should use "pointer to Pointer". See example 7-4-2.

Void GetMemory2 (char ** p, int num)
{
* P = (char *) malloc (sizeof (char) * num );
}
Void Test2 (void)
{
Char * str = NULL;
GetMemory2 (& str, 100); // note that the parameter is & str, not str
Strcpy (str, "hello ");
Cout <str <endl;
Free (str );
}

Example 7-4-2 apply for dynamic memory with a pointer pointing to the pointer
Because the concept of "pointer to Pointer" is not easy to understand, we can use function return values to transmit dynamic memory. This method is simpler. See example 7-4-3.
Char * GetMemory3 (int num)
{
Char * p = (char *) malloc (sizeof (char) * num );
Return p;
}
Void Test3 (void)
{
Char * str = NULL;
Str = GetMemory3 (100 );
Strcpy (str, "hello ");
Cout <str <endl;
Free (str );
}

Example 7-4-3 use the function return value to pass the dynamic memory
Although it is easy to use the function return value to pass dynamic memory, some people often use the return statement wrong. It is emphasized that the return statement should not be used to return the pointer pointing to "stack memory" because the function exists in it automatically disappears when it ends. See example 7-4-4.

Char * GetString (void)
{
Char p [] = "hello world ";
Return p; // the compiler will give a warning
}
Void Test4 (void)
{
Char * str = NULL;
Str = GetString (); // str content is junk
Cout <str <endl;
}
Example 7-4-4 return Statement returns a pointer to "stack memory"
Use the debugger to track Test4 step by step. After executing the str = GetString statement, str is no longer a NULL pointer, but the str content is not "hello world" but garbage.
What if I rewrite Example 7-4-4 to Example 7-4-5?
Char * GetString2 (void)
{
Char * p = "hello world ";
Return p;
}
Void Test5 (void)
{
Char * str = NULL;
Str = GetString2 ();
Cout <str <endl;
}

Example 7-4-5 return statement returns a constant string
Although Test5 does not run properly, the design concept of the GetString2 function is incorrect. Because "hello world" in GetString2 is a constant string located in the static storage zone, it remains unchanged during the lifetime of the program. No matter when GetString2 is called, it always returns the same "read-only" memory block.

7.5 what about the pointer in free and delete?
Don't look at the bad names of free and delete (especially delete). They just release the memory indicated by the pointer, but didn't kill the pointer itself.
Use the debugger to track Example 7-5, and find that the address remains unchanged (non-NULL) after the pointer p is free, but the memory corresponding to this address is junk, and p becomes "Wild Pointer ". If p is not set to NULL at this time, it is mistaken that p is a valid pointer.
If the program is long, we sometimes cannot remember whether the memory referred to by p has been released. Before using p, the statement if (p! = NULL. Unfortunately, the if statement does not prevent errors at this time, because even if p is not a NULL pointer, it does not point to a valid memory block.

Char * p = (char *) malloc (100 );
Strcpy (p, "hello ");
Free (p); // The Memory indicated by p is released, but the address indicated by p remains unchanged.
...
If (p! = NULL) // does not prevent errors
{
Strcpy (p, "world"); // Error
}

Example 7-5 p becomes a wild pointer
7.6 will dynamic memory be automatically released?
Local variables in the function automatically disappear when the function ends. Many people mistakenly think that 7-6 is correct. The reason is that p is a local pointer variable. when it dies, it will make the dynamic memory it points to end together. This is an illusion!
Void Func (void)
{
Char * p = (char *) malloc (100); // will dynamic memory be automatically released?
}
Example 7-6 attempts to automatically release dynamic memory
We found that pointers have the following characteristics:
(1) If the pointer disappears, it does not mean that the memory it refers to will be automatically released.
(2) The memory is released, which does not mean that the pointer will die or become a NULL pointer.
This indicates that releasing the memory is not a hasty action. Some people may not be convinced, so they must find out the reasons for hasty actions:
If the program stops running, all pointers will die and dynamic memory will be recycled by the operating system. In this case, before the program ends, you do not have to release the memory or set the pointer to NULL. Finally, I can be lazy without making a mistake, right?
Think beautiful. What should I do if someone else pulls the program and uses it elsewhere?
7.7 eliminate "Wild Pointer"
"Wild Pointer" is not a NULL pointer, It is a pointer to "junk" memory. Generally, NULL pointers are not incorrectly used, because if statements are easy to judge. However, the "wild Pointer" is very dangerous, and the if statement does not work for it.
There are two main causes of "Wild Pointer:
(1) pointer variables are not initialized. When a pointer variable is created, it does not automatically become a NULL pointer. Its default value is random, which means it is random. Therefore, the pointer variable should be initialized at the same time when it is created, either set the pointer to NULL or set it to direct to the legal memory. For example
Char * p = NULL;
Char * str = (char *) malloc (100 );
(2) After the pointer p is free or deleted, It is not set to NULL, which makes people mistakenly think p is a valid pointer. See section 7.5.
(3) pointer operations go beyond the scope of variables. This situation is hard to prevent. The example program is as follows:
Class
{
Public:
Void Func (void) {cout <"Func of class A" <endl ;}
};
Void Test (void)
{
A * p;
{
A;
P = & a; // note the life cycle of
}
P-> Func (); // p is "Wild Pointer"
}

When the function Test executes the statement p-> Func (), object a has disappeared, and p points to a, so p becomes a "Wild Pointer ". But the strange thing is that I did not encounter any errors when running this program, which may be related to the compiler.
7.8 why new/delete is required when malloc/free is available?
Malloc and free are standard library functions in C ++/C, and new/delete are operators in C ++. They can be used to apply for dynamic memory and release memory.
For non-Internal data objects, maloc/free alone cannot meet the requirements of dynamic objects. The constructor must be automatically executed when the object is created, and the number of destructor must be automatically executed before the object is extinct. Since malloc/free is a library function rather than an operator and is not controlled by the compiler, it is impossible to impose the tasks of executing constructor and destructor on malloc/free.
Therefore, the C ++ language requires a new operator that can complete dynamic memory allocation and initialization, and a delete operator that can clean up and release memory. Note that new/delete is not a database function.
First, let's take a look at how malloc/free and new/delete implement dynamic memory management of objects. See example 7-8.

Class Obj
{
Public:
Obj (void) {cout <"Initialization" <endl ;}
~ Obj (void) {cout <"Destroy" <endl ;}
Void Initialize (void) {cout <"Initialization" <endl ;}
Void Destroy (void) {cout <"Destroy" <endl ;}
};
Void UseMallocFree (void)
{
Obj * a = (obj *) malloc (sizeof (obj); // apply for dynamic memory
A-> Initialize (); // Initialization
//...
A-> Destroy (); // clear the job
Free (a); // releases the memory.
}
Void UseNewDelete (void)
{
Obj * a = new Obj; // apply for dynamic memory and initialize
//...
Delete a; // clear and release the memory
}

Example 7-8 How to Use malloc/free and new/delete to manage dynamic memory of Objects
The class Obj function Initialize simulates the constructor function, and the function Destroy simulates the destructor function. In UseMallocFree, because malloc/free cannot execute constructor and destructor, you must call the member functions Initialize and Destroy to complete initialization and clearing. The UseNewDelete function is much simpler.
Therefore, we should not attempt to use malloc/free to manage the memory of dynamic objects. We should use new/delete. Because the "object" of the internal data type does not have a process of structure and analysis, malloc/free and new/delete are equivalent for them.
Since the new/delete function completely covers malloc/free, why does C ++ not eliminate malloc/free? This is because C ++ programs often call C functions, and C Programs can only use malloc/free to manage dynamic memory.
If "new Dynamic Object" is released with free, the program may fail because the Destructor cannot be executed. If you use delete to release "dynamic memory applied by malloc", theoretically, the program will not go wrong, but the program is poorly readable. Therefore, new/delete must be paired, and the same applies to malloc/free.
7.9 What should I do if the memory is exhausted?
If a large enough memory block cannot be found when applying for dynamic memory, malloc and new will return a NULL pointer, declaring that the memory application failed. There are usually three ways to handle the "memory depletion" problem.
(1) judge whether the pointer is NULL. If yes, use the return statement to terminate the function immediately. For example:
Void Func (void)
{
A * a = new;
If (a = NULL)
{
Return;
}
...
}
(2) judge whether the pointer is NULL. If yes, use exit (1) to terminate the entire program. For example:
Void Func (void)
{
A * a = new;
If (a = NULL)
{
Cout <"Memory Exhausted" <endl;
Exit (1 );
}
...
}
(3) set exception handling functions for new and malloc. For example, in Visual C ++, you can use the _ set_new_hander function to set your own exception handling function for new, or enable malloc to use the same exception handling function as new. For more information, see the C ++ user manual.
The above (1) (2) method is the most common. If a function needs to apply for dynamic memory in multiple places, the method (1) is insufficient (it is troublesome to release the memory) and should be handled in the way (2.
A lot of people cannot bear to use exit (1) and ask: "Do not write error handling programs, so that the operating system can solve the problem on its own? "
No. In case of a "memory depletion" event, generally applications are no longer saved. If you do not use exit (1) to kill the program, it may kill the operating system. The truth is: if you do not kill a gangster, the gangster will commit more crimes before he dies.
There is a very important phenomenon to tell you. For 32-bit applications, no matter how malloc and new are used, it is almost impossible to cause "memory depletion ". In Windows 98, I wrote a test program using Visual C ++. See example 7-9. This program will run endlessly and will not be terminated at all. Because the 32-bit operating system supports "virtual storage" and the memory is used up, the hard disk space is automatically replaced. I only heard the sound of the hard drive. Window 98 was so tired that it didn't respond to the keyboard or mouse.
I can conclude that for 32-bit or more applications, the "memory depletion" error handler is useless. Now, Unix and Windows programmers are happy with this: the error handler does not work, and I will not write it, saving a lot of trouble.
I don't want to mislead readers. I must emphasize that without error handling, the quality of the program will be poor, and never be compromised.

Void main (void)
{
Float * p = NULL;
While (TRUE)
{
P = new float [1000000];
Cout <"eat memory" <endl;
If (p = NULL)
Exit (1 );
}
}

Http://blog.csdn.net/is01sjjj/archive/2006/08/09/1041378.aspx

This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/digu/archive/2008/04/03/2246189.aspx

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.