C Programming Language


With the function realloc, you can change the size of the allocated area once. Has the following form.

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

The first argument specifies the address of an area that is currently allocated to the size in bytes of the modified second argument. Change the size, the return value is returned in re-allocated address space. Otherwise it returns NULL.

Size may be smaller but larger than the original. If you have small, reduced what was written in part will be inaccessible. If you increase the portion of the region will remain an indefinite increase.

The address of the source address changed, but the same could possibly be different, even if the different areas of the old style, because it is automatically released in the function realloc, for the older areas it is not necessary to call the free function. However, if the function fails and returns NULL, realloc, the older area is to remain still valid. Therefore, the first pointer argument of the function realloc, both can be NULL pointer return value is not returned.

# Include
int main (void)
int * p1, * p2;
p1 = (int *) calloc (5, sizeof (int)); /* number of elements in an array of type 5 int */
/ * To do something * /
p2 = (int *) realloc (p1, sizeof (int)); * / re-acquire the space of one type / * int
if (p2 == NULL) * / check if successful * /
free (p1); if it fails to get re-/ *, the region remains valid since the original * / to free myself
return 0;
p1 = NULL; safety measure * /. p1 is realloc () because it is released inside, / * Keep assigning NULL to clear the other can not use / * To do something * /
free (p2);
return 0;

The realloc function is a very feature-rich functions. This is not the pros, cons and rather may be better. For example, if a NULL pointer to the first argument passed to the malloc function with the same behavior as the size specified in the second argument. Meanwhile, the second argument to zero, the operation to free the space pointed to by the free function specified in the first argument.

In other words, whatever the arguments, it is the sister supposed to mean something to the result, which may not have to be an error with the error should really be something. In particular, even in very common use, such as the following:

char * p2 = realloc (p1, size);

Happened, if the variable size is zero, this is not the same behavior as the function becomes free. If it is NULL p1, is the handling function malloc. NULL and 0 respectively when and what behavior is undefined.

In addition, the pointer argument to realloc function first must point to a secure area using one of the functions malloc / calloc / realloc (if a NULL pointer may be, in which case the above equal to the malloc function and so on).

Quite complex because, you have put a sample implementation of realloc function to aid understanding.

void * realloc (void * ptr, size_t size)
char * p;
if (ptr == NULL)
return malloc (size); if the first argument is NULL / * * / function which works the same as malloc }
if (size == 0)
free (ptr); second argument is 0 / * * / function which works the same as free
return NULL; * / return NULL because no new areas to be reserved * /
p = malloc (size); using the function / * malloc, * / a space equivalent to the size of the newer
if (p == NULL) {return NULL; if the function fails} / * malloc, return NULL. The original area * / remain
memcpy (p, ptr, size); the data in the source area / * * / to be copied to newly allocated space
free (ptr); source area / * the * / to be released
return p; * / return the address of the beginning of a new space * / }

Here are the points you need to remember.

  • The contents of the object will remain unaltered 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 zero and ptr isn't a null pointer, the object pointed to is freed.
  • If the space can't be allocated, the object remains unaltered.
  • If ptr is a null pointer, realloc() acts like malloc() for the specified size.

Here's an example ,learn this example and then try applying the same idea to write your own programs.

calloc Home free

c program stats