realloc

Reallocates a block of memory.

Library:LibC
Classification:ANSI
Service:Memory Management

Syntax

  #include <stdlib.h> 
   
  void *realloc (
     void     *oldBlk,   
     size_t    size);
  

Parameters

oldBlk

(IN) Points to a previously allocated memory block.

size

(IN) Specifies the size (in bytes) of the memory block.

Return Values

If successful, returns a pointer to the start of the reallocated memory. Otherwise, returns the following:

  • If insufficient memory is available to allocate a larger block of memory, returns NULL and the oldBlk remains unchanged.

  • If oldBlk is a NULL pointer and size is not zero, returns a pointer to a memory block of the specified size.

  • If size is zero, returns NULL.

Remarks

The realloc function calls the malloc function to enlarge a block of memory.

Memory allocation is not limited to 64 KB. The size parameter is 32 bits.

When the value of the oldBlk parameter is NULL, a new block of memory of size bytes is allocated. Otherwise, the realloc function reallocates space of an object of size bytes by either

  • Shrinking the allocated size of the allocated memory block oldBlk when size is sufficiently smaller than the size of oldBlk.

  • Allocating a new block and copying the contents of oldBlk to the new block when size is sufficiently larger than the size of oldBlk.

Because it is possible that a new block can be allocated, no other pointers should point into the memory of oldBlk. When a new block is allocated, these pointers point to freed memory, with possibly disastrous results.

See Also