C Reference function realloc()

The function realloc() reallocates a memory block with a specific new size. If you call realloc() the size of the memory block pointed to by the pointer is changed to the given size in bytes. This way you are able to expand and reduce the amount of memory you want to use (if available of course.)

It is possible that the function moves the memory block to a new location, in which way the function will return this new location. If the size of the requested block is larger then the previous block then the value of the new portion is indeterminate.

If the pointer is NULL then the function will behave exactly like the function malloc(). It will assign a new block of a size in bytes and will return a pointer to it.

If the size is 0 then the memory that was previously allocated is freed as if a call of the function free() was given. It will return a NULL pointer in that case.

Usage of realloc():

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

Parameters:

A pointer (ptr) to a previously allocated block of memory.
(The memory was previously allocated with malloc(), calloc() or realloc() functions.)

A new size in bytes for the new memory block.

Return value:

Will return a pointer to the reallocated memory block. If the function fails then a NULL pointer is returned.

Source code example of realloc():


	#include<stdio.h>
	#include<stdlib.h>

	int main ()
	{
		int * buffer;

		/*get a initial memory block*/
		buffer = (int*) malloc (10*sizeof(int));
		if (buffer==NULL)
		{
			printf("Error allocating memory!");
			exit (1);
		}

		/*get more memory with realloc*/
		buffer = (int*) realloc (buffer, 20*sizeof(int));
		if (buffer==NULL)
		{
			printf("Error reallocating memory!");
			//Free the initial memory block.
			free (buffer);
			exit (1);
		}
		free (buffer);
		return 0;
	}

This entry was posted in C Reference stdlib.h Functions. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 6 responses to “C Reference function realloc()”

Why not let us know what you think by adding your own comment!

  1. Read text file with unknown number of rows in C on June 26th, 2011:

    […] 79 Sure, you need to do something along the lines of: 1. Open the file 2. Check if file opened successfully 3. while not EOF 4. reallocate space in array 5. read line into array 6. repeat 7. close file The file faq is here To dynamically size the array you need the realloc() function. […]

  2. neeraj on November 14th, 2011:

    thanks

  3. Richard Busch on August 30th, 2012:

    With the comment and printf removed from the example code, if the realloc fails, the following will be executed: if (buffer==NULL) free (buffer); that is, the address of the original block of memory will be lost and an attempt will be made to free something pointed to by NULL. If realloc does not free the original block of memory when it fails (and the description does not say what happens to the original block of memory upon failure), then it will be necessary to save the original value of buffer before calling realloc, so that the memory can be freed if the realloc fails.

  4. Rajat on June 26th, 2013:

    I have a question what is the difference between
    (int *)malloc(10*sizeof(int))
    and
    (int *)malloc(20*sizeof(int))

  5. mohit on July 27th, 2013:

    (int*)malloc(10*sizeof(int)) means 10*2=20 bytes will be allocated at the time of run in the heap.
    (int*)malloc(20*sizeof(int)) means 20*2=20 bytes will be allocated at the time of run in the heap.
    note:-sizeof int or other data type may different on defferent proceesor.

  6. Johnny Cage on September 16th, 2013:

    Thank you for taking the time to write these tutorials; very informative, very well done.

    One comment – realloc(p, 0) is not equivalent to free(p). The result of the allocation functions for zero size requests is implementation defined, and may be NULL or may be a pointer suitable for passing to free. In the later case, not calling free on the result is a memory leak. For compatibility all allocations should be explicitly freed.