Recently learning C language, in the use of realloc function in addition to some problems, always find the problem, and then step by step debugging, finally found the problem, because the previous calloc function used to set the length of the string is wrong, resulting in the use of realloc at the end of the original string ' \ "was cleared , led to a series of problems, fortunately finally solved, now to summarize
ReAlloc Use Precautions (this is to summarize the experience of netizens)
1. When realloc fails, return null
2. ReAlloc failure, the original memory does not change, that is, no free or move, (this place is prone to error)
3. If there is enough memory behind the original memory, realloc memory = The original memory + the remaining memory, realloc or return the original memory address; If there is not enough memory behind the original memory, REALLOC will request new memory, and then copy the original memory data into the new memory, the original memory will be free, realloc return the address of the new memory
4. If size is 0, the effect is equal to free ()
5. The pointer passed to ReAlloc must have been previously assigned through malloc (), calloc (), or realloc ()
on MSDN
Return Value
ReAlloc returns a void pointer to the reallocated (and possibly moved) memory block.
If There is not enough available memory to expand the block to the given size, the original block was left unchanged, and C0>null is returned.
If size is zero, then the block pointed to by Memblock are freed; The return value is NULL, and Memblock is Left pointing at a freed block.
The return value points to a storage space which is guaranteed to being suitably aligned for storage of any type of object. To get a pointer to a type other than void, with a type cast on the return value
Remarks
The realloc function changes the size of an allocated memory block. The memblock argument points to the beginning of the memory block. If memblock is NULL, realloc behaves the same as malloc and allocates a new block of size bytes. I F Memblock is not NULL, it should are a pointer returned by a previous call to Calloc, malloc, or rea Lloc
Considerations for the use of realloc functions in C language