Allocate, reallocate or free a block of memory
#include <malloc.h> void *_srealloc( void* ptr, size_t old_size, size_t new_size );
Use the -l c option to qcc to link against this library. This library is usually included automatically.
|This function is in libc.a, but not in libc.so (in order to save space).|
When the value of the ptr argument is NULL, a new block of memory of new_size bytes is allocated.
If the value of new_size is zero, the corresponding _sfree() function is called to release old_size bytes of memory memory pointed to by ptr.
Otherwise, the _srealloc() function reallocates space for an object of new_size bytes by doing one of the following:
|Because it's possible that a new block will be allocated, don't maintain any pointers into the old memory after a successful call to this function. These pointers will point to freed memory, with possible disastrous results when a new block is allocated.|
The function returns NULL when the memory pointed to by ptr can't be reallocated. In this case, the memory pointed to by ptr isn't freed, so be sure to keep a pointer to the old memory block.
buffer = (char *) _srealloc( buffer, 100, 200 );
In the above example, buffer is set to NULL if the function fails, and no longer points to the old memory block. If buffer is your only pointer to the memory block, then you've lost access to this memory.
The _srealloc() function reallocates memory from the heap.
You must use _sfree() to deallocate the memory allocated by _srealloc().
A pointer to the start of the reallocated memory, or NULL if there's insufficient memory available, or if the value of the new_size argument is zero.
calloc(), free(), realloc(), _scalloc(), _sfree(), _smalloc()