new and delete operator in C++ with examples

(Last Updated On: November 23, 2020)

new and delete operator in C++:

new and delete operator in C++:- new operator is used to allocate memory location during program execution. This operator allocates memory for the specified object and returns its memory address (i.e. starting address of the allocated memory) to the pointer variable. The allocated memory has n name. the data into the allocated memory location is accessed with the reference of the pointer. The new operator can be used to allocate memory for simple variables, arrays, or user-defined objects etc.

The general syntax of new operator is as for follows:

Ptr= new data_type;

Where

Ptr: it represents the pointer to an object.

Data_type: it represents the data type. It may be the standard data type or name of a defined class or structure. It must be the same as type of pointer variable.

For example:

Float *p;

P=new float;

In these statements, the first statement declares pointer variable of float type. In the second statement, the new operator allocates memory of size 4 bytes to store float type data. The address of allocated memory is assigned to pointer ‘p’.

new and delete operator in C++

We can also allocate a consecutive block of memory to store same type of data such as an array. In this case, the memory address of first element of array is assigned to the pointer variable. For example, to allocate memory for n array of ten elements, the statements are written as:

Int *p;

P= new int[10];

new and delete operator in C++



Delete operator:

Once memory is allocated with new operator, it cannot be de-allocated automatically. It the pointer (that points to the allocated memory) goes out scope, the allocated memory becomes unavailable.

The delete operator is used to de-allocated memory occupied by an object. The de-allocated memory can be used by operating system for other purposes.

The general syntax of delete operator to de-allocated memory occupied by an array is as follows:

Delete[] ptr;

Where

Ptr: it represents the pointer variable which holds the memory address of the object which is to deleted from memory.

Programming Example: How to use new and delete operator in C++ that allocates memory location for two integer values and computes the sum of these values:

new and delete operator in C++


How to use new and delete operator in C++ that allocates memory location for an array of int type and find the largest value in the array:

new and delete operator in C++


How to use new and delete operator in C++ that allocates memory location for four integer values and store the addresses of allocated memory and find out the smallest value from the four values:

new and delete operator in C++

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply