Analyze realloc Invalid Pointer, Invalid Pointer, and other errors!
The realloc function is used to re-allocate a size block of memory for the PTR. It seems very simple, but various errors will occur during use.
Function Form:
Void * realloc (void * PTR, size_t new_size );
Recently I checked some information on the Internet, glibc does not find the specific implementation (someone found can send to me, ladd.cn@gmail.com), found an open source project self-written reallocCode,Http://code.google.com/p/mallocspethmeniel/source/browse/trunk/realloc.c? R = 23, It should be the same as the implementation process in stdlib.
The following figure shows the function flowchart.
Workflow of this function:
1. Identify PTR. If PTR is null, the function is equivalent to malloc (new_size). Try to allocate a memory with the size of new_size. if the address is returned successfully, null is returned. If PTR is not null, enter 2
2. Check whether the PTR is in the heap. If not, an exception or error occurs, and the realloc Invalid Pointer will occur (the specific cause is described later ). If the PTR is in the heap, check the new_size. If new_size is 0, it is equivalent to free (PTR). When the PTR pointer is released, null is returned. If new_size is smaller than the original size, data in the PTR may be lost, and only the new_size data will be saved (important here). If the size is equal to the original size, nothing is done. If the size is greater than the original size, the position of PTR does not have enough continuous memory space. If so, allocate more space. The returned address is the same as that of PTR. If not, search in a larger space. If a size space is found, copy the old content to the new memory, and release the old memory, the new address is returned, otherwise, null is returned.
Here, I will explain why PTR should be in the heap. Anyone who has learned how to compile should know the malloc, realloc, and,The user-allocated memory such as calloc () is put in the heap zone, while the temporary variable is put in the stack zone, which is two different areas. The variables in the heap zone are released by the user through free, and the variables in the stack zone are automatically released after the function exits. Therefore, the variable survival time in these two regions is different, you cannot allocate memory to each other. Therefore, the PTR must be in the heap zone, that is, the PTR must be the return value of malloc, realloc, or calloc. Otherwise, the value can be null and the realloc can be changed to malloc.
Therefore, pay attention to the following points when using the realloc function:
1. PTR must be null orThe return value of malloc, realloc, or calloc. OtherwiseRealloc Invalid PointerError
2. If new_size is smaller than old_size, only the data of new_size will be saved and may occur.Data Loss, Use it with caution.
3. If new_size is greater than old_size, a new memory may be allocated. At this time, the memory directed by PTR will be released, and PTR will becomeWild pointer.
4. finally, do not assign the returned results to PTR, that is, PTR = realloc (PTR, new_size) is not recommended, because if the memory allocation fails, PTR will become null, if another value is not assigned to the PTR addressInaccessibleWe recommend that you use temp = realloc (PTR, new_size ).
ReferenceArticle:
1. Function reallocHttp://www.cplusplus.com/reference/clibrary/cstdlib/realloc/
2. realloc. c http://code.google.com/p/mallocspethmeniel/source/browse/trunk/realloc.c? R = 23
Transferred from:
Http://www.cnblogs.com/ladd/archive/2012/06/30/2571420.html