Most times programmers are not aware beforehand of the amount of memory that they will need to store particular information in a defined variable, and though the size of required memory can be determined during run time. in other words programmers can allocate memory at run time using a special operator in C++ within the heap for a variable of a given type, and this operator is called new operator.
However, in cases where dynamically allocated memory is not needed, you can use the delete operator which de-allocates memory previously allocated by a new operator.
In C++ programs, objects are likened to simple data types. Check out the example below where an array of objects is used:
Example:
#include <iostream>
class Box
{
public:
Box() {
cout << "Constructor is called!" <<endl;
}
~Box() {
cout << "Destructor is called!" <<endl;
}
};
void main( )
{
Box* myBoxArray = new Box[4];
delete [] myBoxArray; // Delete array
getch();
}