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?
Double-click all code
| 1 2 3 4 5 6 7 8 9 10 |
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: applying for dynamic memory with pointer Parameters
The fault lies 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" instead. See example 4.2.
Double-click all code
| 1 2 3 4 5 6 7 8 9 10 11 12 |
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); } |
In this example, a pointer pointing to a pointer is used to request dynamic memory.
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.
Double-click all code
| 1 2 3 4 5 6 7 8 9 10 11 12 13 |
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); } |
The example uses 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 the "stack memory", because the function will automatically die when the function ends, as shown in the example.
Double-click all code
| 1 2 3 4 5 6 7 8 9 10 11 |
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; } |
Return the pointer to "stack memory" in the return statement.
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.
Double-click all code
| 1 2 3 4 5 6 7 8 9 10 11 |
char *GetString2(void) { char *p = "hello world"; return p; } void Test5(void) { char *str = NULL; str = GetString2(); cout<< str << endl; } |
Return a constant string using the return statement.
Although the function Test5 runs without errors, the design concept of the function GetString2 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 returns the same read-only memory block.
Eliminate "wild pointer"
The "wild pointer" is not a NULL pointer, but a pointer to the "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
Double-click all code
| 1 2 |
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.
(3) pointer operations go beyond the scope of variables. This situation is hard to prevent. The example program is as follows:
Double-click all code
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class A { public: void Func(void){ cout << “Func of class A” << endl; } }; void Test(void) { A *p; { A 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.
With malloc/free, why new/delete?
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 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 6.
Double-click all code
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 19 20 21 22 |
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 } |
How to Use malloc/free and new/delete to manage the 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 internal data type "object" does not have a process of construction and analysis, malloc/free and new/delete are equivalent to 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 you use free to release the "new Dynamic Object", this object may cause program errors because it cannot execute the destructor. If you use delete to release the "dynamic internal storage requested by malloc", theoretically the program will not go wrong, but the program will be poorly readable. Therefore, new/delete must be paired, and the same applies to malloc/free.