realloc Functions

Description

Reallocate memory blocks.

#include <stdlib.h> For ANSI compatibility (realloc only)  
#include <malloc.h> Required only for function declarations  

void *realloc( void *memblock, size_t size );

void __based( void ) *_brealloc( __segment seg,
void __based( void ) *memblock, size_t size );

void __far *_frealloc( void __far *memblock, size_t size );

void __near *_nrealloc( void __near *memblock, size_t size );

memblock Pointer to previously allocated memory block  
size New size in bytes  
seg Segment selector  

Remarks

The realloc family of functions changes the size of a previously allocated memory block. The memblock argument points to the beginning of the memory block. If memblock is NULL (_NULLOFF for _brealloc), realloc functions in the same way as malloc and allocates a new block of size bytes. If memblock is not NULL (_NULLOFF for _brealloc), it should be a pointer returned by a prior call to calloc, malloc, or realloc.

The size argument gives the new size of the block, in bytes. The contents of the block are unchanged up to the shorter of the new and old sizes, although the new block may be in a different location.

In large data models (that is, compact-, large-, and huge-model programs), realloc maps to _frealloc. In small data models (tiny-, small-, and medium-model programs), realloc maps to _nrealloc.

The various realloc functions reallocate memory in the heap as specified in the following list:

Function Heap

realloc Depends on data model of program
_brealloc Based heap specified by seg value
_frealloc Far heap (outside default data segment)
_nrealloc Near heap (inside default data segment)

Return Value

The realloc functions return a void pointer to the reallocated (and possibly moved) memory block.

The return value is NULL (_NULLOFF for _brealloc) if the size is zero and the buffer argument is not NULL (_NULLOFF for _brealloc), or if there is not enough available memory to expand the block to the given size. In the first case, the original block is freed. In the second, the original block is unchanged.

The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object. To get a pointer to a type other than void, use a type cast on the return value.

Compatibility

realloc

Standards:ANSI, UNIX

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

_brealloc, _frealloc, _nrealloc

Standards:None

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:None

See Also

calloc functions, free functions, malloc functions

Example

/* REALLOC.C: This program allocates a block of memory for buffer

* and then uses _msize to display the size of that block. Next, it

* uses realloc to expand the amount of memory used by buffer

* and then calls _msize again to display the new amount of

* memory allocated to buffer.

*/

#include <stdio.h>

#include <malloc.h>

#include <stdlib.h>

void main( void )

{

long *buffer;

size_t size;

if( (buffer = (long *)malloc( 1000 * sizeof( long ) )) == NULL )

exit( 1 );

size = _msize( buffer );

printf( “Size of block after malloc of 1000 longs: %u\n”, size );

/* Reallocate and show new size: */

if( (buffer = realloc( buffer, size + (1000 * sizeof( long )) )) == NULL )

exit( 1 );

size = _msize( buffer );

printf( “Size of block after realloc of 1000 more longs: %u\n”, size );

free( buffer );

exit( 0 );

}

Output

Size of block after malloc of 1000 longs: 4000

Size of block after realloc of 1000 more longs: 8000