Memory allocation, de-allocation using new and delete with examples

The new operator is used to create the object dynamically. Using the new operator the memory is allocated and the constructor of the class gets called for that memory.
Using the delete the destructor of the class gets called and the memory is released which is allocated by new.

The syntax for the new operator:

Allocating the memory for the array of class objects:

The syntax for the delete:
delete pcClass;
delete []pcClass;

delete pcClass – frees the memory for an individual object allocated by new.
delete []pcClass – frees the memory for an array of objects allocated by new.

How to allocate and de-allocate the memory for the array of objects:  
For arrays, a constructor must be called for each object in the array.

The code calls operator new[] to allocate memory for 10 pcClass object, then call the default MyCls constructor for each array element.

In this way, when the delete operator is used on an array, it calls a destructor for each array element and then calls operator delete[] to de-allocate the memory.

It calls the string destructor for each array element, then calls operator delete[] to de-allocate the array’s memory.

The above statement delete array created on the heap.
Let us have a look at the below example to understand more about the new and delete operators.

The output of the program is shown below:

The problem that we get if we do not delete the array of elements, some compilers, only the destructor for the 0th element of the array will be called because the compiler only knows that you are deleting a pointer to an object If we do not use the array.
The destructors are only called if the elements of the array are plain objects, however, if we have an array of pointers, we will still need to delete each element individually just as you allocated each element individually, as shown in the following code:

Initializing Dynamically Allocated Objects

What is the Free Store?

What is the difference between the C & C++ free store?

In C there is no concept of new and delete operators.
To use the free store it uses malloc / calloc/ realloc functions.

Refer the topic of

malloc (), calloc() and realloc()

Example of malloc(), calloc(), and realloc()  

Why does malloc() return a void*?

The malloc() has no idea which type of object we want to put in that memory.
In case of C++, after we define a constructor, we can write: