new and delete operator in C++ with examples

new and delete operator in c++

new Operator:

new and delete operator in c++:- the new operator is used to allocating 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 the 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 a 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 the memory is allocated with the new operator, it cannot be de-allocated automatically. It the pointer (that points to the allocated memory) goes out the 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 the 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 Examples:

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++


Related Article:

Pointer Variables in C++ with Examples

Recommended For You

About the Author: Fawad

My name is Shahzada Fawad and I am a Programmer. Currently, I am running my own YouTube channel "Expertstech", and managing this Website. My Hobbies are * Watching Movies * Music * Photography * Travelling * gaming and so on...

Leave a Reply

%d bloggers like this: