We use new and delete operators in C++ to dynamically allocate memory whereas malloc() and free() functions are also used for the same purpose in C and C++. The functionality of the new or malloc() and delete or free() seems to be the same but they differ in various ways.
The behavior with respect to constructors and destructors calls differ in the following ways:
malloc() vs new():
- malloc(): It is a C library function that can also be used in C++, while the “new” operator is specific for C++ only.
- Both malloc() and new are used to allocate the memory dynamically in heap. But “new” doesn’t call the destructor of a class whereas “malloc()” frees the memory by calling the destructor.
Below is the program to illustrate the functionality of new and malloc():
// C++ program to illustrate malloc() // and new operator in C++ #include "bits/stdc++.h" using namespace std; // Class A class A { int a; public: int* ptr; // Constructor of class A A() { cout << "Constructor was Called!" << endl; } }; // Driver Code int main() { // Create an object of class A // using new operator A* a = new A; cout << "Object of class A was " << "created using new operator!" << endl; // Create an object of class A // using malloc operator A* b = (A*)malloc(sizeof(A)); cout << "Object of class A was " << "created using malloc()!" << endl; return 0; } |
Constructor was Called! Object of class A was created using new operator! Object of class A was created using malloc()!
In the above program we can clearly see that while creating object using new operator Default Contructor was called and using malloc function Default Constructor was not called.
free() vs delete:
- free() is a C library function that can also be used in C++, while “delete” is a C++ keyword.
- free() frees memory but doesn’t call Destructor of a class whereas “delete” frees the memory and also calls the Destructor of the class.
Below is the program to illustrate the functionality of new and malloc():
// C++ program to illustrate free() // and delete keyword in C++ #include "bits/stdc++.h" using namespace std; // Class A class A { int a; public: int* ptr; // Constructor of class A A() { cout << "Constructor was Called!" << endl; } // Destructor of class A ~A() { cout << "Destructor was Called!" << endl; } }; // Driver Code int main() { // Create an object of class A // using new operator A* a = new A; cout << "Object of class A was " << "created using new operator!" << endl; delete (a); cout << "Object of class A was " << "deleted using delete keyword!" << endl; cout << endl; A* b = (A*)malloc(sizeof(A)); cout << "Object of class A was " << "created using malloc()!" << endl; free(b); cout << "Object of class A was " << "deleted using free()!" << endl; return 0; } |
Constructor was Called! Object of class A was created using new operator! Destructor was Called! Object of class A was deleted using delete keyword! Object of class A was created using malloc()! Object of class A was deleted using free()!
Below are the programs for more illustrations:
Program 1:
// C++ program to illustrate new, delete // malloc() and free() #include "bits/stdc++.h" using namespace std; // Class A class A { int a; public: int* ptr; // Constructor of class A A() { cout << "Constructor was Called!" << endl; } // Destructor of class A ~A() { cout << "Destructor was Called!" << endl; } }; // Driver Code int main() { // Object Created of class A A a; return 0; } |
Constructor was Called! Destructor was Called!
In the above program, the destructor is still called even though the delete operator is not used. The reason for the destructor call is the statement “return 0”. This statement when executed within the main function calls the destructor of each class for which object was created.
To avoid the Destructor calling we can replace the statement “return 0” with “exit(0)”. Below is the code for the same:
Program 2:
// C++ program to illustrate new, delete // malloc() and free() #include "bits/stdc++.h" using namespace std; // Class A class A { int a; public: int* ptr; // Constructor of class A A() { cout << "Constructor was Called!" << endl; } // Destructor of class A ~A() { cout << "Destructor was Called!" << endl; } }; // Driver Code int main() { // Object Created of class A A a; exit(0); } |
Constructor was Called!
Program 3:
// C++ program to illustrate new, delete // malloc() and free() #include "bits/stdc++.h" using namespace std; // Class A class A { int a; public: int* ptr; // Constructor of class A A() { cout << "Constructor was Called!" << endl; } // Destructor of class A ~A() { cout << "Destructor was Called!" << endl; } }; // Driver Code int main() { // Object Created of class A A *a = new A; return 0; } |
Constructor was Called!
There is no Destructor call even after using the statement “return 0”. The reason lies in the difference of allocating an object of a class. When we create an object with class_name object_name within a block is created, the object has an automatic storage duration, i.e., it will automatically be destroyed on going out of scope. But when we use new class_name the object has a dynamic storage duration, which means one has to delete it explicitly using delete keyword.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
- delete and free() in C++
- malloc() vs new
- Function Interposition in C with an example of user defined malloc()
- CodeLobster IDE - free cross-platform PHP, HTML, CSS, JavaScript editor
- Data Structures and Algorithms Online Courses : Free and Paid
- delete() in C++
- "delete this" in C++
- C++ | new and delete | Question 4
- C++ | new and delete | Question 5
- C++ | new and delete | Question 2
- C++ | new and delete | Question 1
- How to delete last element from a map in C++
- SQL | DELETE Statement
- C++ | new and delete | Question 3
- Difference between Free Software and Open Source Software
- Difference between DELETE and TRUNCATE
- How to delete an element from the Set by passing its value in C++
- How to delete last element from a List in C++ STL
- Overloading New and Delete operator in c++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shivamtripathi91

