📘 Lesson · Lesson 76
new and delete
new and delete
Dynamic Memory in C++
💡 Note
In C++, new allocates memory on the heap and delete frees it — the modern replacement for malloc/free.
Example
C++
#include <iostream>
using namespace std;
int main() {
int* p = new int(10); // allocate
cout << *p << "\n"; // 10
delete p; // free single
int* arr = new int[3]; // allocate array
delete[] arr; // free array
return 0;
}Output:
10
10
Summary
newallocates,deletefrees a single object.- For arrays use
new[]anddelete[].
Dynamic Memory in C++
💡 Note
In C++, new allocates memory on the heap and delete frees it — the modern replacement for malloc/free.
Example
C++
#include <iostream>
using namespace std;
int main() {
int* p = new int(10); // allocate
cout << *p << "\n"; // 10
delete p; // free single
int* arr = new int[3]; // allocate array
delete[] arr; // free array
return 0;
}Output:
10
10
सारांश
newallocates,deletefrees a single object.- For arrays use
new[]anddelete[].
💻 Live Code Editor
Is page ke program yahan ready hain — chalाएं, badlें aur seekhें. Bina kuch install kiye.
Powered by OneCompiler. Editor mein code apne aap aa jata hai — Run dabaakर output dekhें.
Agar load na ho to naye tab mein kholें.