Chapter 12: Miscellaneous Functions

realloc

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

The precise operation of realloc( ) is slightly different in C99 than it is in C++ and C89, although the net effect is the same. For C++ and C89, realloc( ) changes the size of the previously allocated memory pointed to by ptr to that specified by size. The value of size can be greater or less than the original. A pointer to the memory block is returned because it may be necessary for realloc( ) to move the block in order to change its size. If this occurs, the contents of the old block (up to size bytes) are copied into the new block.

For C99, the block of memory pointed to by ptr is freed and a new block is allocated. The new block contains the same contents as the original block (up to the length passed in size). A pointer to the new block is returned. It is permissible, however, for the new block and the old block to begin at the same address. That is, the pointer returned by realloc( ) might be the same as the one passed in ptr.

If ptr is null, realloc( ) simply allocates size bytes of memory and returns a pointer to it. If size is zero, the memory pointed to by ptr is freed.

If there is not enough free memory in the heap to allocate size bytes, a null pointer is returned and the original block is left unchanged.

Related functions are free( ), malloc( ), and calloc( ).




C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net