allocate, reallocate or free a block of memory
#include <stdlib.h> /* For ANSI compatibility (realloc only) */ #include <malloc.h> /* Required for other function prototypes */ void * realloc( void *old_blk, size_t size ); void __based(void) *_brealloc( __segment seg, void __based(void) *old_blk, size_t size ); void __far *_frealloc( void __far *old_blk, size_t size ); void __near *_nrealloc( void __near *old_blk, size_t size );
When the value of the old_blk argument is NULL, a new block of memory of size bytes is allocated.
If the value of size is zero, the corresponding free() function is called to release the memory pointed to by old_blk.
Otherwise, the realloc() function re-allocates space for an object of size bytes by doing one of the following:
The function returns NULL when the memory pointed to by old_blk cannot be re-allocated. In this case, the memory pointed to by old_blk is not freed, so care should be exercised to maintain a pointer to the old memory block.
buffer = (char *) realloc( buffer, 100 );
In the above example, buffer will be set to NULL if the function fails, and will no longer point to the old memory block. If buffer is your only pointer to the memory block, then you'll have lost access to this memory.
Each function reallocates memory from a particular heap, as listed below:
In a small data memory model, the realloc() function is equivalent to the _nrealloc() function; in a large data memory model, the realloc() function is equivalent to the _frealloc() function.
The realloc() functions return a pointer to the start of the re-allocated memory. The return value is NULL if there is insufficient memory available, or if the value of the size argument is zero. The_brealloc() function returns _NULLOFF if there is insufficient memory available, or if the requested size is zero.
calloc(), _expand(), free(), halloc(), hfree(), malloc(), _msize(), sbrk()
#include <stdlib.h> #include <malloc.h> void main() { char *buffer; char *new_buffer; buffer = (char *) malloc( 80 ); new_buffer = (char *) realloc( buffer, 100 ); if( new_buffer == NULL ) { /* not able to allocate larger buffer */ } else { buffer = new_buffer; } }