delete and free() in C++ Last Updated : 20 Jan, 2025 Comments Improve Suggest changes Like Article 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 Comment More infoAdvertise with us Next Article delete and free() in C++ K kartik Follow Improve Article Tags : C Language C++ cpp-pointer Practice Tags : CPP Similar Reads "delete this" in C++ Ideally delete operator should not be used for this pointer. However, if used, then following points must be considered.1) delete operator works only for objects allocated using operator new (See this post). If the object is created using new, then we can do delete this, otherwise behavior is undefi 1 min read C Program to Delete a File C language allows the manipulation of the files present in the accessible storage directory allowing programmers to create, read, write and delete files. In this article, we will learn how to delete a file using a C program.A file stored in the system can be deleted from a C program using the standa 2 min read Overloading New and Delete operator in c++ The new and delete operators can also be overloaded like other operators in C++. New and Delete operators can be overloaded globally or they can be overloaded for specific classes. If these operators are overloaded using member function for a class, it means that these operators are overloaded only 5 min read deque::clear() and deque::erase() in C++ STL Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation 5 min read delete keyword in C++ delete is an operator that is used to destroy array and non-array(pointer) objects which are dynamically created by the new operator.delete can be used by either using the delete operator or delete [ ] operator.The new operator is used for dynamic memory allocation which stores variables on heap mem 4 min read Like