Allocate, reallocate or free a block of memory
#include <malloc.h> void *_srealloc( void* ptr, size_t old_size, size_t new_size );
- NULL, or a pointer to the block of memory that you want to reallocate.
- The current size of the block, in bytes.
- The size of the block to allocate, in bytes.
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:
- Shrinking the allocated size of the allocated memory block
ptr when new_size is sufficiently smaller than
- Extending the allocated size of the allocated memory block
ptr if there is a large enough block of unallocated memory
immediately following ptr.
- Allocating a new block, and copying the contents of ptr to the new block.
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.
Last modified: 2013-12-23