Question: I recently read a C-language book. It seems quite good, but there are some mistakes in the book. At the same time, I found some things that are easy to be ignored by myself or everyone, so I will write down and share them with you. If there is any error in the text, I hope you can help me to point it out. Thank you! I. Anyone who has learned the C language for memory allocation and release knows that after memory allocation is used up, they will be released. They will all go to the malloc, calloc, and free functions. Is it really as easy as free (pointer) after memory allocation? Note that the pointer parameter must be a pointer returned after the malloc or calloc function is called. Passing other values to the free function may cause a crash or the result may be disastrous. The focus is on the pointer value, rather than the pointer itself used to apply for dynamic memory. You can check the Code. For example, if void * p = malloc (sizeof (double) * 6) or double * dp = (double *) (malloc (sizeof (double) * 6); then, if free (dp) is used at the moment, unexpected errors will occur. free (p) is correct. If p = dp, (or p = (void *) dp), and then free (p) is also correct. The so-called catastrophic thing is to release the memory and release things that should not be released, and then caused some problems. So how can we verify that free (dp) is incorrect? This may be a problem of Memory leakage. You can try the following code: for (;) {double * p = malloc (sizeof (double) * 6); free (p) ;}then, check if your memory overspending (not enough) is reached? Let's look at the realloc function, which can be used to re-allocate the memory allocated by m, c, and r. So what is a new address for reallocation? In fact, this is not the case. r has two parameters: a pointer that references the previously allocated memory. The re-allocated memory is based on the original size, which is determined by the second parameter. That is to say, if your total household income is 6000 yuan, the Manager (usually the mother's) allocates 1000 yuan of pocket money to your son. Now, due to some force majeure factors, you need to re-allocate money, then, pass the parameter realloc (1000 yuan address, newsize), newsize <= 1000U. In essence, it is to extract some of the money from the son's hand according to newsize, and then the rest will do some processing. Some principles of dynamic memory allocation: 1. When needed, it is released when used up, especially on the stack (limited resources ). 2. Avoid allocating a large number of small pieces of memory. Because the memory allocation on the heap has system overhead, allocating many small memories is much larger than allocating several pieces of memory, which is not easy to release and manage. 3. During programming, the user's limited memory is always at the heart, and the allocation should consider where to release it. 4. Exercise caution when allocating memory in a loop. 5. before releasing the memory, make sure that the memory address allocated on the stack is not overwritten. Otherwise, memory leakage may occur.