Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is the C++ New Operator?
2.1.
Syntax for the new operator
2.2.
C++ code for new operator
2.3.
C++
3.
What is a C++ Delete Operator?
3.1.
Syntax for delete operator
3.2.
C++ code for delete operator
3.3.
C++
4.
Differences between New and Delete Operators in C++
5.
C++ code for arrays using new and delete
5.1.
C++
6.
C++ code for objects using new and delete
6.1.
C++
7.
Frequently Asked Questions
7.1.
What is the new and delete operator in C++?
7.2.
What is new and delete in C++ constructor?
7.3.
How to delete an operator in C++?
7.4.
What is new int in C++?
8.
Conclusion
Last Updated: May 18, 2024
Easy

New and Delete operator in C++

Introduction

In C++, we can allocate memory for a variable or an array at run time. This is called dynamic memory allocation. In C++, we have to deallocate the dynamically allotted memory manually after using a variable. Deallocation and allocation of memory can be done by using new and delete.

In C++you could allocate memory at runtime using the “new” operator. Once you don’t need the variable you have declared dynamically, you could deallocate the memory occupied through the variable by using the “delete” operator in C++.

Some of the use of dynamic memory allocation in data structures are: new is used in a linked list or a tree to allocate memory. You also can use the new and delete operators to allocate memory to the array at runtime.

New and Delete operator in C++

What is the C++ New Operator?

The new operator is used to allocate memory to a variable, arrays, objects, etc. If a large amount of memory is available on the heap, the memory will be initialized by the new operator, and And will return the address of that memory. The use of pointers can store that memory address.

Syntax for the new operator

<dataType pointerName> = new <dataType>

Ex: int * p = new int;

We can also initialize value.

Ex: int * p = new int(7);

C++ code for new operator

  • C++

C++

#include <bits/stdc++.h>

using namespace std;

int main() {

// pointer initialized
int * Ninjas = new int(5);

//value printed
cout << * Ninjas << endl;
}

Output:

5

What is a C++ Delete Operator?

The C++ delete operator is used to deallocate memory that was previously allocated using the new operator. When dynamically allocated memory is no longer needed, the delete operator frees up the memory so that it can be reused by other parts of the program or by the operating system. This helps prevent memory leaks and ensures efficient memory usage in C++ programs. It is important to pair each new operator with a corresponding delete operator to avoid memory leaks and potential program instability.

Syntax for delete operator

delete pointerVariable

Ex: delete p;

C++ code for delete operator

  • C++

C++

#include <bits/stdc++.h>

using namespace std;

int main() {

// pointer initialized
int * Ninjas = new int(5);

//value printed
cout << * Ninjas << endl;

//pointer deleted
delete Ninjas;
}

Output:

5

 

You can try and compile with the help of online c++ compiler.

Differences between New and Delete Operators in C++

Aspectnew Operatordelete Operator
PurposeAllocates memory on the heap for a new object or array.Deallocates memory allocated by the new operator.
Syntaxpointer_variable = new data_type;delete pointer_variable;
Exampleint *ptr = new int;delete ptr;
Memory LeakCan lead to memory leaks if not paired with delete.Proper use prevents memory leaks by freeing memory.
Array AllocationSupports dynamic array allocation with new[].Used with delete[] to deallocate dynamic arrays.
Exception SafetyMay throw std::bad_alloc on failure.Does not throw exceptions.
Custom AllocatorSupports custom memory allocators.Does not support custom memory deallocation strategies.

C++ code for arrays using new and delete

  • C++

C++

#include <bits/stdc++.h>

using namespace std;

int main() {
int * Ninjas = new int[5];
for (int i = 0; i < 5; i++) {
*(Ninjas + i) = i;
}
for (int i = 0; i < 5; i++) {
cout << * (Ninjas + i) << " ";
}
delete[] Ninjas;
}

Output:

0 1 2 3 4 

C++ code for objects using new and delete

  • C++

C++

#include <bits/stdc++.h>

using namespace std;

class Ninjas {
public:

//Constructor
Ninjas() {
cout << "inside Ninjas constructor" << endl;
}

//Destructor
~Ninjas() {
cout << "inside Ninjas destructor" << endl;
}
};

int main() {
//Dynamic memory Allocation to array of objects of type Ninjas
Ninjas * s = new Ninjas[5];

// Delete array which was created dynamically
delete[] s;

return 0;
}

Output:

inside Ninjas constructor
inside Ninjas constructor
inside Ninjas constructor
inside Ninjas constructor
inside Ninjas constructor
inside Ninjas destructor
inside Ninjas destructor
inside Ninjas destructor
inside Ninjas destructor
inside Ninjas destructor

 

Also check out this article - Pair in C++

Frequently Asked Questions

What is the new and delete operator in C++?

In C++, the new operator dynamically allocates memory for objects or arrays on the heap, while the delete operator deallocates memory previously allocated with new, preventing memory leaks.

What is new and delete in C++ constructor?

In C++, new is used in combination with constructors to dynamically allocate memory for objects and initialize them, while delete is used to destroy objects and deallocate memory.

How to delete an operator in C++?

In C++, the delete operator is used to deallocate dynamically allocated memory. It is followed by the pointer variable pointing to the memory to be deallocated. For example: delete ptr;

What is new int in C++?

In C++, new int dynamically allocates memory on the heap to store an integer. It returns a pointer to the allocated memory, allowing for dynamic memory allocation for integer values.

Conclusion

This article is about the New and Delete operator in C++. These operators in C++ provide powerful mechanisms for dynamic memory allocation and deallocation. Proper understanding and usage of these operators are crucial for effective memory management, preventing memory leaks and ensuring efficient utilization of system resources.

Recommended Readings:


It’s excellent to apply dynamic memory allocation because of its flexibility and different usages that are primarily based on programmer requirements, as defined within the above article. So, there are instances wherein the memory desires can most effectively be decided at runtime. For example, the memory desires rely upon the programmer's input. In such varieties of instances, programmers can use dynamic memory allocation.

We hope that this blog has helped you enhance your knowledge regarding Firewalls and if you would like to learn more, stay tuned for more blogs code studio. Do upvote our blog to help other ninjas grow. Happy Coding!"

Live masterclass