delete and free() in C++ Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 99 Likes Like Report delete and free() in C++ have similar functionalities but they are different. In C++, the delete operator should only be used for deallocating the memory allocated either using the new operator or for a NULL pointer, and free() should only be used for deallocating the memory allocated either using malloc(), calloc(), realloc() or for a NULL pointer.Difference between delete and free()deletefree()It is an operator.It is a library function.It de-allocates the memory dynamically.It destroys the memory at runtime.It should only be used for deallocating the memory allocated either using the new operator or for a NULL pointer.It should only be used for deallocating the memory allocated either using malloc(), calloc(), realloc() or for a NULL pointer.This operator calls the destructor before it destroys the allocated memory.This function only frees the memory from the heap. It does not call the destructor.It is comparatively slower because it invokes the destructor for the object being deleted before deallocating the memory.It is faster than delete operator.Note: The most important reason why free() should not be used for de-allocating memory allocated using new is that, it does not call the destructor of that object while delete operator calls the destructor to ensure cleanup and resource deallocation for objects.Example 1The below program demonstrates the usage of the delete operator. C++ // CPP program to demonstrate the correct and incorrect // usage of delete operator #include <cstdlib> #include <iostream> using namespace std; // Driver Code int main() { int x; int* ptr1 = &x; int* ptr2 = (int*)malloc(sizeof(int)); int* ptr3 = new int; int* ptr4 = NULL; // delete Should NOT be used like below because x is // allocated on stack frame delete ptr1; // delete Should NOT be used like below because x is // allocated using malloc() delete ptr2; // Correct uses of delete delete ptr3; delete ptr4; getchar(); return 0; } Example 2The below program demonstrates the usage of free() function. C++ // CPP program to demonstrate the correct and incorrect // usage of free() function #include <cstdlib> #include <iostream> using namespace std; // Driver Code int main() { int* ptr1 = NULL; int* ptr2; int x = 5; ptr2 = &x; int* ptr3 = (int*)malloc(5 * sizeof(int)); // Correct uses of free() free(ptr1); free(ptr3); // Incorrect use of free() // free(ptr2); return 0; } Related Articlesdelete keyword in C++new vs operator new in C++C++ malloc()Difference Between malloc() and calloc() with Examples Create Quiz Comment K kartik Follow 99 Improve K kartik Follow 99 Improve Article Tags : C++ cpp-pointer Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++3 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++12 min readFile Handling in C++8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++3 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL2 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like