new vs malloc() and free() vs delete in C++

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():

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:



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():

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
}

chevron_right


Output:

Constructor was Called!

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

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.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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