Dynamic memory allocation operators in C++: new and delete

In C++ there are two operators for dynamic memory allocation new and delete. new is used to allocate memory and delete is used to free memory.


p_variable = new type;

delete p_variable;

where, p_variable is a pointer variable.

Both new and delete use a pointer to allocate and free memory. While using delete you have to make sure that you are using the same pointer variable used by new to allocate memory.

Advantages of new and delete:

1) You don’t have to worry about size of object when allocating new and no sizeof() operator is required as the memory required is calculated automatically.

2) new already has a pointer and no explicit type cast is required.

3) new and delete can be overloaded.

Leave a Reply