C uses
malloc() and
calloc()functions to allocate memory dynamical at run time. Similarly, it uses the
function
free() to free dynamicallyallocated memory. We use dynamic allocation techniques when it is not known in
advance how much of memory space is needed. Although C++ supports these
functions, it also defines two unary operators
new and
delete thatperform the task of allocating and freeing the memory in a better and easier
way. Since these operators manipulate memory on the free store, they are also
known as
free store operators.
An object can be created by using
new and destroyed by using
delete, as and when required. A dataobject created inside a block with
new,will remain in existence until it is explicitly destroyed by using
delete. Thus, the lifetime of an objectis directly under our control and is unrelated to the block structure of the
program.
The
new operator can be used to crateobjects of any type. It takes the following general form:
| Pointer-variable = new data-type |