new and delete Operators
Dynamic Memory Allocation using new and delete in C++
C++ new and delete Operators Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
using namespace std;
int main() {
// Allocate single integer
int* ptr = new int(10);
cout << "Dynamically allocated integer: " << *ptr << endl;
// Allocate array
int* arr = new int[5];
cout << "\nEnter 5 integers: ";
for (int i = 0; i < 5; i++) {
cin >> arr[i];
}
cout << "Array elements: ";
for (int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
cout << endl;
// Allocate and initialize array
int* arr2 = new int[5]{1, 2, 3, 4, 5};
cout << "\nInitialized array: ";
for (int i = 0; i < 5; i++) {
cout << arr2[i] << " ";
}
cout << endl;
// Free memory
delete ptr; // Delete single variable
delete[] arr; // Delete array (use delete[])
delete[] arr2; // Delete array
cout << "\nMemory freed successfully" << endl;
// Set pointers to null after deletion
ptr = nullptr;
arr = nullptr;
arr2 = nullptr;
return 0;
}Dynamically allocated integer: 10 Enter 5 integers: 10 20 30 40 50 Array elements: 10 20 30 40 50 Initialized array: 1 2 3 4 5 Memory freed successfully
Understanding new and delete Operators
This program teaches you how to use the new and delete operators in C++ for dynamic memory allocation. The new operator allocates memory at runtime and returns a pointer, while delete frees the allocated memory. Proper use of new and delete is essential for managing dynamic memory and preventing memory leaks.
---
1. What This Program Does
The program demonstrates dynamic memory allocation:
Dynamic memory allows allocation at runtime when size is unknown at compile time.
---
2. Header File Used
This header provides:
---
#include <iostream>3. Understanding new Operator
new Operator Concept
:
Syntax
:
pointer = new dataType; // Single variable
pointer = new dataType(value); // With initialization
pointer = new dataType[size]; // Array
---
4. Allocating Single Variable
Basic Allocation
:
int* ptr = new int(10);
How it works
:
Accessing Value
:
*ptr // Accesses value 10
---
5. Allocating Arrays
Array Allocation
:
int* arr = new int[5];
How it works
:
Initialized Array
:
int* arr2 = new int[5]{1, 2, 3, 4, 5};
How it works
:
---
6. Understanding delete Operator
delete Operator Concept
:
Syntax
:
delete pointer; // Single variable
delete[] pointer; // Array
---
7. Freeing Single Variable
Deleting Single Variable
:
delete ptr;
How it works
:
---
8. Freeing Arrays
Deleting Arrays
:
delete[] arr; // Use delete[] for arrays
Important Rule
:
Why delete[]?
---
9. Setting Pointers to Null
After Deletion
:
delete ptr;
ptr = nullptr; // Good practice
Why?
---
10. When to Use new/delete
Best For
:
Example Scenarios
:
---
11. Important Considerations
Memory Leaks
:
Matching Operators
:
Exception Safety
:
---
12. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for learning dynamic memory allocation, understanding heap memory, and preparing for advanced memory management and data structures in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for new and delete Operators
This C++ program is part of the "Memory Management Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.