What happens if we mix new and free in C++?

As we know that new is used to create memory dynamically and it is the programmer’s responsibility to delete the memory location explicitly and it is deleted by using the delete keyword. The syntax for creating memory at runtime using new keyword:

int *ptr = new int;

It will create a memory location for type int and returns its address. free() should only be used either for the pointers pointing to the memory allocated using malloc() or for a NULL pointer.

What happens if we mix new and free in C++?

We know that memory created by new keyword is deleted by using the delete keyword and memory created by malloc() is deleted by free(). new calls the constructor and delete calls the destructor for memory deallocation. Now, creating memory using new keyword and try to delete it using free() then the destructor will not be called and because of that memory and resources will not be free. And it will lead to memory and resources leak. Below is the program to analyze how free() and delete behaves:






// C++ program to illustrate the working
// of memory allocation if new and free
// are mixed
#include <iostream>
using namespace std;
class A {
    int* p;
    // Default Constructor
        cout << "Constructor is executed"
             << endl;
        p = new int;
        *p = 5;
    // Destructor
        cout << "Destructor is executed"
             << endl;
        // resource clean-up
    // Member Function
    void cleanup()
        cout << "Resource clean-"
             << "up completed" << endl;
    // Function to display the value
    // of class variables
    void display()
        cout << "value is: "
             << *p << endl;
// Driver Code
int main()
    // Create Object of class A
    A* ptr = new A();
    // Destructor will be called
    delete ptr;
    A* ptr1 = new A();
    // No destructor will be called
    // hence no resource clean-up
    return 0;



Constructor is executed
value is: 5
Destructor is executed
Resource clean-up completed
Constructor is executed
value is: 5


In the above code, an object is created using new Keyword, and we are trying to delete this object using free(). It will not call the destructor for this object, and memory and resources of this object will not release. So, It is always suggested avoiding extra efforts and time by not mixing new and free in C++ program.

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.

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.