www.delorie.com/djgpp/doc/libc/libc_648.html   search  
libc.a reference

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

realloc

Syntax

 
#include <stdlib.h>

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

Description

This function changes the size of the region pointed to by ptr. If it can, it will reuse the same memory space, but it may have to allocate a new memory space to satisfy the request. In either case, it will return the pointer that you should use to refer to the (possibly new) memory area. The pointer passed may be NULL, in which case this function acts just like malloc (see section malloc).

An application that wants to be robust in the face of a possible failure of realloc to enlarge a buffer should save a copy of the old pointer in a local variable, to be able to use the original buffer in case realloc returns NULL. See the example below for details.

Return Value

On success, a pointer is returned to the memory you should now refer to. On failure, NULL is returned and the memory pointed to by ptr prior to the call is not freed.

Portability

ANSI/ISO C C89; C99
POSIX 1003.2-1992; 1003.1-2001

Example

 
if (now+new > max)
{
  char *old = p;

  max = now+new;
  p = realloc(p, max);
  if (p == NULL)
    p = old;  /* retain the old pointer */
}


  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2004   by DJ Delorie     Updated Apr 2004