First look at the following C program piece:
#include
Char * p;
P = (char *) malloc (10);
P = (char *) Realloc (p, 20);
..............................
This program means very simple, only people who are slightly C foundation can understand. The function first defines a character type pointer P, and then assigns a 10-byte size memory space for the pointer P, then increases the size of this memory block to 20 bytes.
Is there any problem here? Run it up, it seems that there is no problem!
Yes, this is not a problem, but there is a hidden danger that we don't pay attention to this! Where is hidden dangers? This is the realloc () function I have to explain in this article.
Look at the next paragraph from MSDN:
realloc returns a void pointer to the reallocated (and possibly moved) memory block. The return value is NULL if the size is zero and the buffer argument is not NULL, or if there is not enough available memory to expand the block to the given size . In the first case, the original block is freed. In the second, the original block is unchanged. The return value points to a storage space that is guaranteed to be suitably aligned for storage of any type of object. to get a pointer to A Type Other Than Void, Use a Type Cast On The Return Value.
This e-article is still difficult to understand, so I will not translate, and the general meaning is about the return value of Realloc. But here is a few cases to his return value:
1. Return to the VOID * pointer, call success.
2, return NULL, when the size of the extended (second parameter) is 0 and the first parameter is not null, at this time, the original memory becomes "freed".
3, return NULL, when there is not enough space available, at this time, the size of the original memory space remains unchanged.
The first case tells us that we need to do type conversion after getting the memory space required;
The second case may only be used with fools!
In the third case, it will maintain the future size of the future when the memory space is not enough.
The MSDN said that the memory space will not expand the size of the original memory space when the memory space is not enough. This is not wrong, but it seems to be omissioned! We know that Realloc is allocated from the heap. When an enlarges of a memory space, the realloc () tries to get additional bytes from those bytes of existing data on the pile, if they can be met, natural world peace; If the byte behind the data is not enough, the problem will come out, then use the first free block on the pile, the existing data is copied, and the old block is copied to the new position, and the old block is returned to the pile on. An important information passed in this sentence is that the data may be moved! Seeing this, maybe we have found the problem of the program I gave up. To more clearly explain the problem, you can change the above program to the following form: #include
Char * p, * q;
P = (char *) malloc (10);
Q = P;
P = (char *) Realloc (p, 20);
..............................
This program may have no way to pass in the compiler, because the compiler may eliminate some hidden dangers for us! Here we just add a pointer Q that recorded the original memory address, then record the original memory address P, if unfortunate, the data moves, then the memory space points pointed to by the recorded original memory address actually Let go back to the pile! In this way, we should finally realize the problem and terrible!
This problem seems to have a bit of the horn tip, because we may never encounter, but we should understand that this is always existed. Only in this way, we will have a consciousness when we encounter us. Otherwise, once such a hidden dangers, the program crashes, I am afraid that it is not an easy task!
In the "deep-in-depth MFC", the Junjie quoted a word in the "Zhu Gate" of Lin Yutang, I was very touching, although I can't have his feelings, but holding the mentality of learning to the seniors, so it is also end:
"I only use the same thing, I don't understand his truth, it is really unhappy."