new and delete operator

new operator is used to allocate memory in runtime. This memory is allocated from heap segment of a process. To understand how new operator works, let us see with the help of an example.

#include <iostream>
using namespace std;

class employee {
    unsigned short m_age;
    unsigned int m_salary;
public:
    employee() {}
    employee(unsigned short age, unsigned int salary) {
        m_age = age;
        m_salary = salary;
    }
};

int main() {
    employee* emp = new employee(30, 40,000);
    delete emp;
    return 0;
}

Here new is called for the employee object by passing the value as per the constructor defined for the employee class.

Below steps are performed when new operator is used to allocate memory:
1) size is calculated as per the employee class and memory allocation happens for employee object from heap memory.
2) employee constructor is called for proper initialization of an object before using it. This is called for static allocation as well.
3) pointer of type employee is returned.

This is called dynamic object creation because this allocation happens at run time.
To delete or free the memory allocated using new, user has to take care of explicitly deleting the memory by using delete operator as shown in the above example.

Below steps are performed by compiler when delete operator is used to delete the memory
1) Destructor of that class will be called.
2) Memory is freed from the heap memory.

delete and new operator must be used together i.e. if the memory is allocated using new, then it should be free using delete only otherwise it will lead to undefined behavior.

Let us try to understand new and delete operator for arrays with the help of same employee class:

employee* empArray = new employee [100];

Now for above statement to work, employee class must have default constructor i.e. constructor with zero arguments otherwise, it won’t compile. That is why we have added default constructor also in the employee class to support this behavior.

100 employee objects are created and the default constructor is called for 100 objects. To delete above allocated memory, below statement is used:

delete []empArray;

This tells to compiler that memory needs to be free for an array of 100 employee object. This 100 i.e. number of objects is saved when the memory is allocated for an array.

  • If we delete the pointer which is already 0(NULL), then nothing will happen. That is why it is recommended to set the pointer to 0 immediately after deleting the pointer to prevent deleting it twice.
  • Deleting a void pointer can result into some issue like memory leak as it only free the memory but it won’t call the destructor.
  • In case, operator new cannot find contiguous memory then it will call new-handler. The default behavior of new-handler is throw an exception. But we can replace with our own function using below code:
void memory_unavailable() {
    cout  << “Memory is not available” << endl;
    exit(1);
}

int main() {
    set_new_handler(memory_unavailable);
    while (1) {
        employee* empArray = new employee[100];
   }
}



Related posts