Coupon Accepted Successfully!


Memory Management Operators

In ‘C’ language, we have studied the function malloc(), calloc(), and realloc() to allocate memory dynamically at run-time in the program. The free() function is used to release the resources allocated by these functions. C++ allows us to use these functions. In additional, C++ provides operators which help us to allocate and release the memory in easy way than these functions. These new operators are new and delete. The new operator creates an object and delete destroys the object. These operators are easy in writing as compared to malice() and calloc(). The syntaxes for new and delete are illustrated with suitable programs.

Following are the advantages of new operator over the function malloc():

  1. The new operator itself calculates the size of the object without the use of sizeof() operator.
  2. It returns the pointer type. The programmers need not take care of its type casting.
  3. The new operator allocates memory and initializes the object at once.
  4. The new and delete operators are simple in syntax. They can be overloaded.

new Operator

Format of new operator is as follows:


pointer memory variable = new operator – data type[size];

Here, pointer memory variable is a pointer to the data type. The new operator allocates memory of specified type and returns back the starting address to the pointer memory variable. Here, the element size is optional and used when we allocation of memory space is required for user-defined data types such as arrays, classes, and structures. If the new operator fails to allocate the memory it returns NULL, which can be used to detect failure or success of new operator.



  1. pv = new int;
  2. int *pv = new int (50);
  3. *p = new int [3]

In example (a), pv is a pointer variable of integer type. Once allocated, the pv contains the starting address. In example (b), 50 is assigned to pointer variable pv. In example (3), memory for 3 integers, that is, 6 bytes are assigned to pointer variable p. The example of new operator with arrays are as follows:

pv= new int [5] [2]; // valid

pv= new int [8][k][2] // invalid

pv= new int [ ] [ 2 ] [ 2 ] // invalid

4.27 Program to illustrate the simple example of new operator.


Explanation: In the above program, x is an integer pointer variable. The new operator allocates memory required for 10 integers, i.e. 20 bytes to pointer x as each integer occupies 2 bytes. new operator is used to replace the sizeof() operator.

delete Operator

The delete operator frees the memory allocated by the new operator. This operator is used when the memory allocated is no longer used in the program. The following syntax is used for the delete operator.

Syntax Example

a) delete <pointer memory a) delete p;


b) delete [element size] b) delete [5 ]p or delete [ ]p;

<pointer memory variable>

In example (a), the delete operator releases the memory allocated to pointer p. Example (b) is advantageous when we want to free the dynamically allocated memory of array. The new C++ compilers do not require element size.

4.28 Program to illustrate the simple example of new operator.


Explanation: The above program shows allocation of memory by using new operator. The delete operator frees the memory allocated by the new operator.

4.39 Write a program to allocate memory using new operator.

Explanation: In the above program, p is an integer pointer variable. The new operator allocates memory required for three integers, i.e. 6 bytes to pointer p. The first for loop reads integer through the keyboard and stores the number at memory location pointed by p as shown in Figure. Each time, pointer p is incremented and it shows the next location of its type. The second for loop displays the number by applying the same logic. Before that the pointer is again set to the starting location by decrementing by 3. The delete operator releases the memory allocated by the new operator. Figure shows the memory allocation of the different variable.

In the output of the program, only starting memory location numbers are displayed.

Description: 144482.png

Memory allocated by the different variables


The sizeof() operator is used to return size occupied in bytes in memory by the variable. The sizeof() operator in C++ displays different values as compared to C. The following program illustrates this.

4.30 Write a program to display number of bytes occupied by char data type.


Explanation: In the above program, a character constant is used, sizeof( ) operator. The size determined is 1 byte. The same program ‘C’ will display the size 2 bytes, because C and C++ reacts differently with data types. In C, ‘a’ character is considered as integer. Hence the size displayed is 2 whereas in C++, it is considered as a character.

The size is the space occupied in memory in bytes by the variable. It depends upon the data type of variable. Figure describes the space occupied in the memory by the variable integer and float.



int x = 5;

float f = 3.14;

The integer variable occupies two bytes and float variable occupies four bytes in memory.


Description: 144580.png
Space occupied by the int and float

Test Your Skills Now!
Take a Quiz now
Reviewer Name