User Tools

Site Tools


Sidebar

Programming Reference/Librarys

Question & Answer

Q&A is closed







c:stdlib.h:realloc

Table of Contents

realloc

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

description

The realloc() function shall change the size of the memory object pointed to by ptr to the size specified by size.
The contents of the object shall remain unchanged up to the lesser of the new and old sizes.
If the new size of the memory object would require movement of the object, the space for the previous instantiation of the object is freed.
If the new size is larger, the contents of the newly allocated portion of the object are unspecified.
If size is 0 and ptr is not a null pointer, the object pointed to is freed. If the space cannot be allocated, the object shall remain unchanged.

If ptr is a null pointer, realloc() shall be equivalent to malloc() for the specified size.

If ptr does not match a pointer returned earlier by calloc(), malloc(), or realloc() or if
the space has previously been deallocated by a call to free() or realloc(), the behavior is undefined.

The order and contiguity of storage allocated by successive calls to realloc() is unspecified.
The pointer returned if the allocation succeeds shall be suitably aligned so that it may be assigned
to a pointer to any type of object and then used to access such an object in the space allocated
(until the space is explicitly freed or reallocated). Each such allocation shall yield a pointer to an object disjoint from any other object.
The pointer returned shall point to the start (lowest byte address) of the allocated space. If the space cannot be allocated, a null pointer shall be returned.

return value

Upon successful completion with a size not equal to 0, realloc() shall return a pointer to the (possibly moved) allocated space.
If size is 0, either a null pointer or a unique pointer that can be successfully passed to free() shall be returned.
If there is not enough available memory, realloc() shall return a null pointer and set errno to [ENOMEM].

realloc c Example

/* 
 * realloc example code
 * http://code-reference.com/c/stdlib.h/realloc
 */
#include <stdio.h> /* including standard library */
//#include <windows.h> /* uncomment this for Windows */
 
#include <stdlib.h>
 
int testit ( int *x )
{
     if (x != NULL) {
         printf("malloc / realloc of memory was successfully.\n");
     }
     else {
          printf("allocation of memory failed.\n");
     }
return 0;
}
 
int main ( void )
{
  int *buffer;
  printf("set buffer to 100 * sizeof(int)\n");
  buffer = (int*) malloc (10*sizeof(int));
  testit(buffer); 
 
  printf("set buffer to 10 * sizeof(int)\n");
  buffer = (int*) realloc (buffer,10*sizeof(int));
  testit(buffer);
 
  printf("try to free the buffer\n");
  free (buffer);
  return 0;
}

Output

  user@host:~$  ./realloc 
  set buffer to 100 * sizeof(int)
  malloc / realloc of memory was successfully.
  set buffer to 10 * sizeof(int)
  malloc / realloc of memory was successfully.
  try to free the buffer
advertising
c/stdlib.h/realloc.txt · Last modified: 2013/01/22 22:02 (external edit)

Impressum Datenschutz