C++ free()

The free() function in C++ deallocates a block of memory previously allocated using calloc, malloc or realloc functions, making it available for further allocations.

The free() function does not change the value of the pointer, that is it still points to the same memory location.

free() prototype

void free(void *ptr);

The function is defined in <cstdlib> header file.


free() Parameters

  • ptr: A pointer to a memory block previously allocated with malloc, calloc or realloc. The pointer may be null or may not point to a block of memory allocated by calloc, malloc or realloc functions.
    • If ptr is null, the free() function does nothing.
    • If ptr does not point to a memory block allocated by calloc, malloc or realloc functions, it causes undefined behavior.

free() Return value

The free() function returns nothing. It simply makes the memory block available for us.


Example 1: How free() function works with malloc()?

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

int main()
{
	int *ptr;
	ptr = (int*) malloc(5*sizeof(int));
	cout << "Enter 5 integers" << endl;

	for (int i=0; i<5; i++)
	{
	// *(ptr+i) can be replaced by ptr[i]
		cin >> *(ptr+i);
	}
	cout << endl << "User entered value"<< endl;

	for (int i=0; i<5; i++)
	{
		cout << *(ptr+i) << " ";
	}
	free(ptr);

	/* prints a garbage value after ptr is free */
	cout << "Garbage Value" << endl;

	for (int i=0; i<5; i++)
	{
		cout << *(ptr+i) << " ";
	}
	return 0;
}

When you run the program, the output will be:

Enter 5 integers
21 3 -10 -13 45
User entered value
21 3 -10 -13 45
Garbage Value
6690624 0 6685008 0 45

Example 2: How free() function works with calloc()?

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

int main()
{
	float *ptr;
	ptr = (float*) calloc(1,sizeof(float));
	*ptr = 5.233;

	cout << "Before freeing" << endl;
	cout << "Address = " << ptr << endl;
	cout << "Value = " << *ptr << endl;

	free(ptr);

	cout << "After freeing" << endl;
	/* ptr remains same, *ptr changes*/
	cout << "Address = " << ptr << endl;
	cout << "Value = " << *ptr << endl;
	return 0;
}

When you run the program, the output will be:

Before freeing
Address = 0x6a1530
Value = 5.233
After freeing
Address = 0x6a1530
Value = 9.7429e-039

Example 3: How free() function works with realloc()?

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

int main()
{
	char *ptr;
	ptr = (char*) malloc(10*sizeof(char));

	strcpy(ptr,"Hello C++");
	cout << "Before reallocating: " << ptr << endl;

	/* reallocating memory */
	ptr = (char*) realloc(ptr,20);
	strcpy(ptr,"Hello, Welcome to C++");
	cout << "After reallocating: " <<ptr << endl;

	free(ptr);
	/* prints a garbage value after ptr is free */
	cout << endl << "Garbage Value: " << ptr;

	return 0;
}

When you run the program, the output will be:

Before reallocating: Hello C++
After reallocating: Hello, Welcome to C++
Garbage Value: @↨/

Example 4: free() function with other cases

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

int main()
{
	int x = 5;
	int *ptr1 = NULL;

	/* allocatingmemory without using calloc, malloc or realloc*/
	int *ptr2 = &x;
	if(ptr1)
	{
		cout << "Pointer is not Null" << endl;
	}
	else
	{
		cout << "Pointer is Null" << endl;
	}

	/* Does nothing */
	free(ptr1);
	cout << *ptr2;

	/* gives a runtime error if free(ptr2) is executed*/
	// free(ptr2);

	return 0;
}

When you run the program, the output will be:

Pointer is Null
5