Memory Management-high-quality C ++

Source: Internet
Author: User

 

Memory Management ---- high quality c ++

Chapter 4 Memory Management

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 thunder, the only solution
It is to discover all the hidden mines and eliminate them. Hiding is impossible. The content of this chapter is better than that of general textbooks.
In-depth, readers need to read carefully to truly understand memory management.
7.1 Memory Allocation Method
There are three memory allocation methods:
(1) distribution from the static storage area. It is allocated when the program is compiled.
It exists throughout the running period. For example, global variables and static variables.
(2) create a stack. When executing a function, all the storage units of local variables in the function can be created on the stack.
These storage units are automatically released when execution ends. Stack memory allocation operations are embedded in the processor's Instruction Set
Medium, high efficiency, but the allocated memory capacity is limited.
(3) allocate from the stack, also known as dynamic memory allocation. When the program is running, use malloc or new to apply for any number
With less memory, the programmer is responsible for releasing the memory with free or delete. Lifetime of dynamic memory
It is up to us to decide that it is very flexible to use, but there are also the most problems.
7.2 memory errors and Countermeasures
Memory Errors are very troublesome. The compiler cannot automatically detect these errors, usually in the program
It can be captured only during 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:
... Memory allocation failed, but it was used.
New programmers often make this mistake because they do not realize that memory allocation will fail. The common solution is,
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.
... The memory allocation is successful, but it is referenced before initialization.
There are two main causes for this mistake: first, there is no idea of initialization, and second, the default initial values of memory.
If all values are zero, the reference initial value is incorrect (for example, an array ).
There is no uniform standard for the default initial values of the memory. Although sometimes the default values are zero, we prefer to trust them.
High quality c ++/C Programming Guide, V 1.0
2001 page 45 of 101
All of them are credible. Therefore, no matter which method is used to create an array, do not forget to assign the initial value, even if it is null.
Can be omitted.
... The 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 than 1" is often performed. Especially in the For Loop
In a sentence, the number of loops is easy to make a mistake, resulting in array operations out of bounds.
... Forget 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 enough memory. You
You cannot 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, no
Then there must be an error (the same is true for new/delete ).
... The memory is released, but it continues to be used.
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 been released
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. Be sure not to return the "Pointer" or "Reference" pointing to "stack memory ",
This is 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, a "wild pointer" is generated ".
.. [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.
.. [Rule 7-2-2] Do not forget to assign initial values to the array and dynamic memory. Prevents the uninitialized memory from being used as the right
Value.
.. [Rule 7-2-3] avoid the subscript of the array or pointer from crossing the border, especially when "more 1" or "Less 1" occurs"
Operation.
.. [Rule 7-2-4] dynamic memory application and release must be paired to prevent memory leakage.
.. [Rules 7-2-5] after the memory is released with free or delete, the pointer is set to null immediately to prevent production
Generate "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
Not pointing to) a piece of memory, its address and capacity remain unchanged during the life cycle, only the content of the array can be changed.
A pointer can point to any type of memory block at any time, and its feature is "variable", so we often use pointers to operate
Dynamic memory. Pointers are far more flexible than arrays, but they are more dangerous.
High quality c ++/C Programming Guide, V 1.0
2001 page 46 of 101
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, the content is World/0 ).
The content of the string cannot be modified. In terms of syntax, the compiler does not think that the statement P [0] = 'X' has any
However, this statement attempts to modify the content of a 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 the number
Group B. Statement B = A is not allowed; otherwise, a compilation error occurs. 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.
.
Statement P = A does not copy the content of A, but assigns the address of A to P. To copy
You can use the library function malloc to apply for a memory with a capacity of strlen (A) + 1 characters for P, and then use
Strcpy to copy strings. Similarly, the statement if (P = A) compares not the content but the address.
Number strcmp to compare.
// Array...
Char A [] = "hello ";
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
High quality c ++/C Programming Guide, V 1.0
2001 page 47 of 101
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), sizeof ()
The value 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) is the number of bytes of a pointer variable, which is equivalent to sizeof (char *) rather
Storage capacity. 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. Example
In 7-3-3 (B), sizeof (a) is always equal to sizeof (char *) regardless of the size of array *).
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 test function's statement getmemory (STR, 200) 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
High quality c ++/C Programming Guide, V 1.0
2001 page 48 of 101
The fault lies in the getmemory function. The compiler always needs to make a temporary copy for each parameter of the function, pointer
The copy of parameter P is _ p, and the compiler makes _ p = P. If the program in the function body modifies the content of _ p
Modify the content of parameter P. This is why pointers can be used as output parameters. In this example, _ P applies
The new memory is changed, but the memory address indicated by _ p is not changed at all. So the getmemory Function
It cannot output anything. In fact, each execution of getmemory exposes a piece of memory, because it is useless.
Free releases memory.
If you need to use the pointer parameter to request memory, you should use "pointer to Pointer" instead. 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
High quality c ++/C Programming Guide, V 1.0
2001 page 49 of 101
Although it is easy to use the function return value to pass dynamic memory, some people often use the return statement in an error.
. It is emphasized that the return statement should not be used to return the pointer pointing to the "stack memory", because there is a function end time in it.
Automatically disappears. 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,
However, 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 the function test5 runs without errors, the design concept of the function getstring2 is incorrect. Because
The "Hello World" in getstring2 is a constant string located in the static storage zone. It is within the lifetime of the program.
Constant. No matter when getstring2 is called, it returns the same read-only memory block.
High quality c ++/C Programming Guide, V 1.0
2001 page 50 of 101
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 give the memory indicated by the pointer
Released, but the pointer is not killed.
Use the debugger to track Example 7-5. After the pointer P is free, its address remains unchanged (non-null),
The memory corresponding to this address is junk, and p becomes a "wild pointer ". If P is not set to null at this time, it will be mistaken.
Assume 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.
Normally, if (P! = NULL. Sorry, the IF statement cannot 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 example 7-6 is correct. Management
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 the pointer has some characteristics similar to none:
(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. Maybe someone is not convinced. Be sure to find out.
Reasons for rashness:
If the program stops running, all pointers will die and dynamic memory will be recycled by the operating system. In this case,
High quality c ++/C Programming Guide, V 1.0
2001 page 51 of 101
Before the program ends, you do not have to release the memory or set the pointer to null. Finally, you can be lazy.
An error occurred, right?
Think beautiful. What should I do if someone else pulls the program and uses it elsewhere?
7.7 eliminate "wild pointer"
The "wild pointer" is not a null pointer, but a pointer to the "junk" memory. Generally, null is not used incorrectly.
Pointer, because the if statement is 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.
The default value is random. Therefore, the pointer variable should be initialized at the same time, or
Set the pointer to null or point it to a valid 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 a "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.
High quality c ++/C Programming Guide, V 1.0
2001 page 52 of 101
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 ++. Both
Used to apply for dynamic memory and release memory.
For non-Internal data objects, maloc/free alone cannot meet the requirements of dynamic objects. Object
The constructor must be automatically executed when the object is created, and the Destructor must be automatically executed before the object is extinct. Because
Malloc/free is a library function rather than an operator. It is not controlled by the compiler and cannot execute constructor.
And destructor tasks are imposed on malloc/free.
Therefore, the C ++ language requires a new operator that can complete dynamic memory allocation and initialization, and
The 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 functions of the destructor.
Yes. In usemallocfree, because malloc/free cannot execute constructor and destructor, you must call
The member functions initialize and destroy to complete initialization and cleanup. The usenewdelete function is much simpler.
High quality c ++/C Programming Guide, V 1.0
2001 page 53 of 101
Therefore, we should not attempt to use malloc/free to manage the memory of dynamic objects. We should use new/Delete.
Because the internal data type "object" does not have a process of construction and structure, for them, malloc/free and new/delete
Is equivalent.
Since the new/delete function completely covers malloc/free, why does C ++ not use malloc/free for Taobao?
What about elimination? This is because C ++ programs often call C functions, and C Programs can only use malloc/free to manage
Memory.
If "New Dynamic Object" is released with free, this object may be unable to execute the destructor.
Cause program errors. If you use Delete to release the "dynamic memory requested by malloc", theoretically, the program will not go wrong,
However, the program has poor readability. 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,
An error occurred while declaring the memory application. 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, you can use the _ set_new_hander function in Visual C ++.
Set the user-defined exception handling function for new, and enable malloc to enjoy the same exception location as new.
High quality c ++/C Programming Guide, V 1.0
2001 page 54 of 101
Functions. For more information, see the C ++ user manual.
The above (1) (2) method is the most common. If a function needs to request dynamic memory in multiple places
Type (1) is insufficient (it is troublesome to release the memory) and should be handled in method (2.
A lot of people cannot bear to use exit (1). They asked, "Can the operating system solve the problem by itself without writing error handling programs ?"
No. In case of a "memory depletion" event, generally applications are no longer saved. If
You do not need to exit (1) to kill the bad program. It may kill the operating system. The truth is: if you do not kill a gangster
More crimes will be committed before the old man dies.
There is a very important phenomenon to tell you. For 32-bit or higher applications
Malloc and new are almost impossible to cause "memory depletion ". I wrote it in Visual C ++ on Windows 98.
For the test procedure, see Example 7-9. This program will run endlessly and will not be terminated at all. Because 32-bit operations
The system supports "virtual storage". When the memory is used up, the hard disk space is automatically replaced. I only heard the hard drive scream,
Window 98 is so tired that it does not respond to the keyboard or mouse.
I can conclude that for 32-bit or more applications, the "memory used up" error handler
It is useless. Now, UNIX and Windows programmers are happy: the error handler does not work,
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 small.
Loss.
Void main (void)
{
Float * P = NULL;
While (true)
{
P = new float [1000000];
Cout <"Eat memory" <Endl;
If (P = NULL)
Exit (1 );
}
}
Example 7-9 try to exhaust the operating system memory
7.10 usage of malloc/free
The following is a prototype of the malloc function:
Void * malloc (size_t size );
Use malloc to apply for an integer-type memory with a length. The program is as follows:
Int * P = (int *) malloc (sizeof (INT) * length );
High quality c ++/C Programming Guide, V 1.0
2001 page 55 of 101
We should focus on two elements: "type conversion" and "sizeof ".
... The type returned by malloc is void *, so you must explicitly convert the type when calling malloc.
Void * is converted to the required pointer type.
... The malloc function does not recognize the type of memory to be applied for. It only cares about the total number of bytes of memory. Me
We usually cannot remember the exact number of bytes of data types such as int and float. For example, if the int variable is in a 16-bit system
2 bytes, 4 bytes in 32 bits, and 4 bytes in 16 bits.
The 32-bit value is also 4 bytes. It is best to use the following program for a test:
Cout <sizeof (char) <Endl;
Cout <sizeof (INT) <Endl;
Cout <sizeof (unsigned INT) <Endl;
Cout <sizeof (long) <Endl;
Cout <sizeof (unsigned long) <Endl;
Cout <sizeof (float) <Endl;
Cout <sizeof (double) <Endl;
Cout <sizeof (void *) <Endl;
Using the sizeof operator in the "()" of malloc is a good style, but be careful that sometimes we will faint
Write a program such as P = malloc (sizeof (p.
.. The prototype of the function free is as follows:
Void free (void * memblock );
Why isn't the free function as complicated as the malloc function? This is because the pointer p type and
The memory capacity is known in advance, and the statement free (p) can correctly release the memory. If P is a null pointer,
Therefore, no matter how many times the free P operation will fail. If P is not a null pointer, free
Two consecutive operations may cause program running errors.
7.11 usage highlights of new/delete
The new operator is much easier to use than the malloc function, for example:
Int * P1 = (int *) malloc (sizeof (INT) * length );
Int * P2 = new int [length];
This is because new has built-in sizeof, type conversion, and type security check functions. For non-Internal Data Types
New completes initialization while creating a dynamic object. If the object has multiple constructors,
The new statement can also be in multiple forms. For example
Class OBJ
{
Public:
OBJ (void); // a constructor without Parameters
OBJ (int x); // constructor with a parameter
High quality c ++/C Programming Guide, V 1.0
2001 page 56 of 101
...
}
Void test (void)
{
OBJ * A = new OBJ;
OBJ * B = new OBJ (1); // The initial value is 1.
...
Delete;
Delete B;
}
If you use new to create an object array, you can only use the non-parameter constructor of the object. For example
OBJ * objects = new OBJ [100]; // create 100 Dynamic Objects
Cannot be written
OBJ * objects = new OBJ [100] (1); // create 100 dynamic objects and assign initial value 1
When releasing an object Array Using Delete, do not lose the symbol '[]'. For example
Delete [] objects; // correct usage
Delete objects; // incorrect usage
The latter is equivalent to delete objects [0], and 99 Other objects are missing.
7.12 experiences
I know many c ++/C programmers with good technology. Few people can pat their chests and say they are familiar with pointer and memory management.
(Including myself ). When I first learned the C language, I was especially afraid of pointers. As a result, I developed the first application software (about 1
10 million lines of C Code). If no pointer is used, it is too stupid to replace the pointer with arrays. Escape pointer
No way. Later I changed the software and reduced the code size to half of the original one.
My lessons are:
(1) The more you are afraid of pointers, the more you need to use pointers. If pointer is not used correctly, it is definitely not a qualified programmer.
(2) You must develop the habit of gradually tracing programs using the debugger. Only in this way can you discover the essence of the problem.

Posted on Redlight reading (78) Comments (0) EDIT favorite Reference Category: C ++ kung fu

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.