📘 Lesson  ·  Lesson 76

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

Summary

  • new allocates, delete frees a single object.
  • For arrays use 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

सारांश

  • new allocates, delete frees a single object.
  • For arrays use new[] and delete[].
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.