Malloc(), Calloc(), Realloc(), Free() in Cpp

Malloc(), Calloc(), Realloc(), Free() in Cpp



When users work with dynamic memory allocation, that time memory is allocated at runtime from heap segment. To allocate dynamic memory at run time using heap, we have 4 functions at “stdlib.h” header file. Those 4 functions are :

In this tutorial you will learn :

 
Let's discuss those 4 functions in details.


1. malloc() function in C++ :

Full form of malloc is “memory allocation”. This function reserves a single block of memory with specified amount of bytes. It returns a pointer to the memory, usually the return type of the pointer is void, means now user can assign malloc to any pointer. It initializes all the values that are allocated at allocation time with default garbage value. When this function faced insufficient space, that time your allocation of memory will fail and it'll returns a NULL pointer.

Syntax :
ptr = (ptr_type*)  malloc(size_in_bytes);
Example :
1. ptr = (int*) malloc(211*sizeof(int));
2. ptr = (float*) malloc(211*sizeof(float));

Program :
//Write a C++ program to print a user define array using malloc() function.

#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
	int* ptr;
	int size;
	cout <<"Enter the size of Your array: " <<endl;
	cin >>size;
	ptr = (int*) malloc(size*sizeof(int));
	for(int i=0;i<size;i++)
	{
		cout <<"Enter the values of the array: " <<endl;
		cin >>ptr[i];
	}
	for(int i=0;i<size;i++)
	{
		cout << endl <<"Your values of the array is: " <<ptr[i] <<endl;
	}
	return 0;
}

Output :
 



2. calloc() function in C++ :

Full form of calloc is “contiguous allocation”. This function reserves n blocks of memory with specified amount of bytes. It returns a pointer to the memory, usually the return type of the pointer is void, means now user can assign calloc to any pointer. It initializes all the values that are allocated at allocation time with 0. When this function faced insufficient space, that time your allocation of memory will fail and it will return a NULL pointer.

Syntax :
ptr = (ptr_type*)  calloc(n,size_in_bytes);

Example :
3. ptr = (int*) calloc(47,sizeof(int));
4. ptr = (float*) calloc(56, sizeof(float));

Program :
//Write a C++ program to print a user define array using calloc() function.

#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
	int* ptr;
	int size;
	cout <<"Enter the size of Your array: " <<endl;
	cin >>size;
	ptr = (int*) calloc(size,sizeof(int));
	for(int i=0;i<size;i++)
	{
		cout <<"Enter the values of the array: " <<endl;
		cin >>ptr[i];
	}
	for(int i=0;i<size;i++)
	{
		cout << endl <<"Your values of the array is: " <<ptr[i] <<endl;
	}
	return 0;
}

Output :
 



3. realloc() function in C :

Full form realloc is “reallocation”. If users understand that previously allocated memory is not sufficient and that allocated memory has excess space, then users can change the size of previously allocated memory using realloc() function.

Syntax :
ptr = (ptr_type*)  realloc(ptr, new_size_in_bytes);

Program 1 :
//Write a C++ program to print a user define array using malloc() and realloc() function.
#include<iostream> #include<stdlib.h> using namespace std; int main() { int* ptr; int size; cout <<"Enter the size of Your array: " <<endl; cin >>size; ptr = (int*) malloc(size*sizeof(int)); for(int i=0;i<size;i++) { cout <<"Enter the values of the array: " <<endl; cin >>ptr[i]; } for(int i=0;i<size;i++) { cout << endl <<"Your values of the array is: " <<ptr[i] <<endl; } //***using realloc*** cout <<endl <<"Enter the NEW size of Your array: " <<endl; cin >>size; ptr = (int*) realloc(ptr,size*sizeof(int)); for(int i=0;i<size;i++) { cout <<"Enter the NEW values of the array: " <<endl; cin >>ptr[i]; } for(int i=0;i<size;i++) { cout << endl <<"Your NEW values of the array is: " <<ptr[i]; } return 0; }

Output :
 
 
Program 2 :

//Write a C++ program to print a user define array using calloc() and realloc() function.

#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
	int* ptr;
	int size;
	cout <<"Enter the size of Your array: " <<endl;
	cin >>size;
	ptr = (int*) calloc(size,sizeof(int));
	for(int i=0;i<size;i++)
	{
		cout <<"Enter the values of the array: " <<endl;
		cin >>ptr[i];
	}
	for(int i=0;i<size;i++)
	{
		cout << endl <<"Your values of the array is: " <<ptr[i] <<endl;
	}

//***using realloc***

	cout <<endl <<"Enter the NEW size of Your array: " <<endl;
	cin >>size;
	ptr = (int*) realloc(ptr,size*sizeof(int));
	for(int i=0;i<size;i++)
	{
		cout <<"Enter the NEW values of the array: " <<endl;
		cin >>ptr[i];
	}
	for(int i=0;i<size;i++)
	{
		cout << endl <<"Your NEW values of the array is: " <<ptr[i];
	}
	return 0;
}

Output :




4. free() function in C :

Free function is used, to de-allocate memory or to free the used memory, which is occupied by malloc or calloc function.

Using free function is a very good practice for a programmer otherwise malloc or calloc will consume memory until the program exited.

Syntax :
Free(ptr);

Program 1 :
//Write a C++ program to print a user define array using calloc(),realloc() and free() function.

#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
	int* ptr;
	int size;
	cout <<"Enter the size of Your array: " <<endl;
	cin >>size;
	ptr = (int*) calloc(size,sizeof(int));
	for(int i=0;i<size;i++)
	{
		cout <<"Enter the values of the array: " <<endl;
		cin >>ptr[i];
	}
	for(int i=0;i<size;i++)
	{
		cout << endl <<"Your values of the array is: " <<ptr[i] <<endl;
	}

//***using realloc***

	cout <<endl <<"Enter the NEW size of Your array: " <<endl;
	cin >>size;
	ptr = (int*) realloc(ptr,size*sizeof(int));
	for(int i=0;i<size;i++)
	{
		cout <<"Enter the NEW values of the array: " <<endl;
		cin >>ptr[i];
	}
	for(int i=0;i<size;i++)
	{
		cout << endl <<"Your NEW values of the array is: " <<ptr[i];
	}
	free(ptr);
	return 0;
}


Output :
 
 

Program 2 :
//Write a C++ program to print a user define array using malloc(), realloc() and free() function.

#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
	int* ptr;
	int size;
	cout <<"Enter the size of Your array: " <<endl;
	cin >>size;
	ptr = (int*) malloc(size*sizeof(int));
	for(int i=0;i<size;i++)
	{
		cout <<"Enter the values of the array: " <<endl;
		cin >>ptr[i];
	}
	for(int i=0;i<size;i++)
	{
		cout << endl <<"Your values of the array is: " <<ptr[i] <<endl;
	}

//***using realloc***
 
 	cout <<endl <<"Enter the NEW size of Your array: " <<endl;
	cin >>size;
	ptr = (int*) realloc(ptr,size*sizeof(int));
	for(int i=0;i<size;i++)
	{
		cout <<"Enter the NEW values of the array: " <<endl;
		cin >>ptr[i];
	}
	for(int i=0;i<size;i++)
	{
		cout << endl <<"Your NEW values of the array is: " <<ptr[i];
	}
	free(ptr);
	return 0;
}

Output :