---
title: "Dynamic memory allocation with new and delete"
subject: "Object Oriented Programming"
chapter: 3
key: "semester_3/object_oriented_programming:dynamic-memory-allocation"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/dynamic-memory-allocation
site: BCT Engineering
---

# Dynamic memory allocation with new and delete

Object Oriented Programming · Chapter 3: C++ Language Constructs

- Asked in: 2074 Chaitra, 5 marks; 2070 Chaitra, 3 marks (asked 2 times)
- Video: [C++ dynamic memory explained for beginners 🧠](https://www.youtube.com/watch?v=fc19HhHKtrA) — Bro Code
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/dynamic-memory-allocation
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-3

## Question

What is dynamic memory allocation? Explain how memory is allocated and deleted dynamically for a normal variable and for an array in C++ with example program.

## Short answer

Dynamic memory allocation uses new to obtain heap memory at runtime, and delete to release it. For a single variable: int *p = new int; delete p; For an array: int *a = new int[n]; delete[] a; The program reads n, allocates an array, computes sum, and frees memory.

## Answer

**Dynamic memory allocation** means allocating memory at run time from the free store (heap) while the program is running, instead of at compile time. In C++ the operator `new` allocates memory and the operator `delete` releases it. The amount of memory can be decided by the user during execution, and the memory stays until it is deleted.

#### For a normal variable

```cpp
int *p = new int;      // allocate one int
*p = 25;
delete p;              // release it
```

`new int(25)` allocates and initialises in one step.

#### For an array

```cpp
int *a = new int[n];   // allocate n ints
delete[] a;            // release the whole array (note the [])
```

If memory is not available, `new` throws `bad_alloc` (or returns `NULL` when `nothrow` is used).

#### Example program

```cpp
#include <iostream>
using namespace std;

int main()
{
    int *p = new int;            // single variable
    *p = 10;
    cout << "Value = " << *p << endl;

    int n;
    cout << "How many numbers? ";
    cin >> n;
    int *a = new int[n];         // array of n elements
    for (int i = 0; i < n; i++)
        cin >> a[i];
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum += a[i];
    cout << "Sum = " << sum << endl;

    delete p;                    // free single variable
    delete[] a;                  // free array
    return 0;
}
```

**Note:** every `new` must be matched by a `delete`, and `new[]` by `delete[]`, otherwise a memory leak occurs.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Dynamic allocation happens when?
   A. At run time from the heap
   B. At compile time
   C. At link time
   D. Only globals
   Answer: A. new at run time. (Key point: Definition)

2. How to free new int[n]?
   A. delete a;
   B. delete[] a;
   C. free(a);
   D. a = NULL;
   Answer: B. Array needs delete[]. (Key point: Array release)

3. What if new is not matched by delete?
   A. Compile error
   B. Auto freed
   C. Memory leak
   D. Crash always
   Answer: C. Leak. (Key point: Leak)

4. If memory unavailable, new?
   A. Returns 0 always
   B. Ignores
   C. Prints
   D. Throws bad_alloc
   Answer: D. Or NULL with nothrow. (Key point: Failure)

## Related in Object Oriented Programming

- [Function overloading; new/delete average program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-overloading-new-delete-average) — Object Oriented Programming, chapter 3
- [Dynamic memory for object and object array](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adynamic-memory-object-array) — Object Oriented Programming, chapter 3

## Related in other subjects

- [File system vs virtual memory; contiguous allocation](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Afs-vs-virtual-memory-contiguous-tape) — Operating System, chapter 5
- [Array representation vs dynamic list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Aarray-vs-dynamic-list) — Data Structure and Algorithm, chapter 3
- [Array list; static or dynamic choice](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Aarray-list-static-dynamic-reason) — Data Structure and Algorithm, chapter 3
- [Static and dynamic data structures; array list](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Astatic-dynamic-ds-array-list) — Data Structure and Algorithm, chapter 3
