C++ realloc()

The realloc() function in C++ reallocates a block of memory that was previously allocated but not yet freed.

The realloc() function reallocates memory that was previously allocated using malloc(), calloc() or realloc() function and yet not freed using the free() function.

If the new size is zero, the value returned depends on the implementation of the library. It may or may not return a null pointer.

realloc() prototype

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

The function is defined in <cstdlib> header file.


realloc() Parameters

  • ptr: A pointer to the memory block to be reallocated.
  • new_size: An unsigned integral value which represents the new size of the memory block in bytes.

realloc() Return value

The realloc() function returns:

  • A pointer to the beginning of the reallocated memory block.
  • Null pointer if allocation fails.

While reallocating memory, if there is not enough memory, then the old memory block is not freed and a null pointer is returned.

If the old pointer (i.e. ptr) is null, calling realloc() is same as calling malloc() function with the new size as its parameter.

There are two possible ways of reallocating memory.

  1. Expanding or contracting the same block: The memory block pointed by the old pointer (i.e. ptr) is expanded or contracted, if possible. The contents of the memory block remains unchanged up to the lesser of the new and old sizes. If the area is expanded, the contents of the newly allocated block are undefined.
  2. Moving to new location: A new memory block of size new_size bytes is allocated. In this case also, the contents of the memory block remains unchanged up to the lesser of the new and old sizes and if the memory is expanded, the contents of the newly allocated block are undefined.

Example 1: How realloc() function works?

#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
	float *ptr, *new_ptr;
	ptr = (float*) malloc(5*sizeof(float));
	if(ptr==NULL)
	{
		cout << "Memory Allocation Failed";
		exit(1);
	}

	/* Initializing memory block */
	for (int i=0; i<5; i++)
	{
		ptr[i] = i*1.5;
	}

	/* reallocating memory */
	new_ptr = (float*) realloc(ptr, 10*sizeof(float));
	if(new_ptr==NULL)
	{
		cout << "Memory Re-allocation Failed";
		exit(1);
	}
	
	/* Initializing re-allocated memory block */
	for (int i=5; i<10; i++)
	{
		new_ptr[i] = i*2.5;
	}
	cout << "Printing Values" << endl;
	
	for (int i=0; i<10; i++)
	{
		cout << new_ptr[i] << endl;
	}
	free(new_ptr);
	
	return 0;
}

When you run the program, the output will be:

Printing Values
0
1.5
3
4.5
6
12.5
15
17.5
20
22.5

Example 2: realloc() function with new_size zero

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
	int *ptr, *new_ptr;
	ptr = (int*) malloc(5*sizeof(int));
	
	if(ptr==NULL)
	{
		cout << "Memory Allocation Failed";
		exit(1);
	}

	/* Initializing memory block */
	for (int i=0; i<5; i++)
	{
		ptr[i] = i;
	}

	/* re-allocating memory with size 0 */
	new_ptr = (int*) realloc(ptr, 0);
	if(new_ptr==NULL)
	{
		cout << "Null Pointer";
	}
	else
	{
		cout << "Not a Null Pointer";
	}

	return 0;
}

When you run the program, the output will be:

Null Pointer

Example 3: realloc() function when ptr is NULL

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;

int main()
{
	char *ptr=NULL, *new_ptr;

	/* reallocating memory, behaves same as malloc(20*sizeof(char)) */
	new_ptr = (char*) realloc(ptr, 50*sizeof(char));
	strcpy(new_ptr, "Welcome to Programiz.com");
	cout << new_ptr;

	free(new_ptr);
	return 0;
}

When you run the program, the output will be:

Welcome to Programiz.com