C++ Dynamic Memory (new/delete)

Dynamic memory is memory that a C++ program requests while it is running. It is useful when a program needs storage whose size or lifetime is not known until the program runs.

C++ uses pointers to work with dynamic memory. The new operator allocates memory, and delete releases it when the program no longer needs it.

Allocating One Value

Use new with a type to create one object dynamically. The result is a pointer to the new object.

#include <iostream>

int main(void) {
    int* score = new int;

    *score = 95;

    std::cout << "Score: " << *score << std::endl;

    delete score;
    score = nullptr;

    return 0;
}

Output:

Score: 95

The statement new int creates space for one int. The pointer score stores the address of that space, so *score reads or changes the value stored there.

The statement delete score; releases the memory. After deleting, assigning nullptr makes it clear that the pointer no longer points to a valid object.

Initialize With new

You can give a dynamic object a starting value at the same time you allocate it. This avoids reading an uninitialized value.

#include <iostream>

int main(void) {
    int* lives = new int(3);

    std::cout << "Lives: " << *lives << std::endl;

    *lives = *lives - 1;
    std::cout << "After hit: " << *lives << std::endl;

    delete lives;

    return 0;
}

Output:

Lives: 3
After hit: 2

Here, new int(3) allocates one int and stores 3 in it. The pointer itself is still just an address; the value is accessed with *lives.

Dynamic Arrays

Use new[] when you need several objects in one dynamic block. Use delete[] to release a block that was allocated with new[].

#include <iostream>

int main(void) {
    int count = 4;
    int* scores = new int[count];

    for (int i = 0; i < count; i++) {
        scores[i] = (i + 1) * 10;
    }

    for (int i = 0; i < count; i++) {
        std::cout << "Score " << i + 1 << ": " << scores[i] << std::endl;
    }

    delete[] scores;
    scores = nullptr;

    return 0;
}

Output:

Score 1: 10
Score 2: 20
Score 3: 30
Score 4: 40

A pointer returned by new int[count] can be used with array indexing, such as scores[0] and scores[1]. The valid indexes are still from 0 to count - 1.

Match new With delete

The cleanup form must match the allocation form.

Allocation Cleanup
new int delete pointer;
new int[count] delete[] pointer;

Using the wrong cleanup form is an error. For example, memory allocated with new[] must be released with delete[], not plain delete.

Common Mistakes

  • Forgetting to call delete or delete[], which causes a memory leak.
  • Using a pointer after deleting it.
  • Calling delete twice on the same pointer.
  • Mixing new with delete[], or new[] with delete.
  • Reading dynamic memory before giving it a value.

Modern C++ programs often prefer std::vector for dynamic arrays and smart pointers for owned objects, but understanding new and delete helps explain how manual memory management works.

Takeaway: new gives your program memory at run time, and every successful manual allocation must be matched with the correct cleanup.