Loading...

Dynamic Memory in C++

Imagine you’re organizing a school event. Sometimes you know exactly how many chairs you need, but other times the audience size keeps changing, 20 students today, 45 tomorrow, and maybe 100 next week.
A fixed number of chairs won’t work. You need flexible space, something that adjusts to your needs.

C++ works the same way. Sometimes you know the size of your data, but sometimes you only know it while the program is running.
To handle this, C++ gives you dynamic memory — the ability to create and remove memory on demand.

A Dynamic Score Tracker

You’ll build a small program that:

  1. Asks how many test scores the user wants to enter
  2. Dynamically creates an array of that size
  3. Stores all scores
  4. Prints them in one list

Output format:

text
1
2
3
4
5
6
7
8
9
10
How many scores? 4
Enter score 1: 85
Enter score 2: 90
Enter score 3: 88
Enter score 4: 92

========================
   SCORES ENTERED
   85 90 88 92
========================

This project teaches you how to grow memory while the program runs, just like adding more chairs when the audience grows.

What Is Dynamic Memory?

Dynamic memory is memory created during program execution, not before.
It lets your program adapt to user input or unpredictable data sizes.
This is essential for flexible, real-world applications.

new Operator (Allocate Memory)

The new keyword creates memory on the heap and returns its address. You decide how much memory to create at runtime. Think of it as reserving seats only when needed.

Example

cpp
1
int* ptr = new int;

delete Operator (Free Memory)

delete removes memory that was created using new. It prevents wasted memory and keeps your program efficient. Think of it as cleaning up chairs after the event ends.

Example

cpp
1
delete ptr;

Dynamic Arrays

Dynamic arrays let you create lists of any size at runtime. Instead of writing int arr[10], you can ask the user how many values they want. Useful for scores, names, or any flexible data.

Example

cpp
1
int* arr = new int[n];

Memory Leaks

A memory leak happens when you allocate memory but forget to free it. The program keeps using memory even when it's no longer needed. Always pair every new with a matching delete.

Good Practices

Always initialize pointers to null and delete memory when done. Avoid losing track of pointer addresses. Treat dynamic memory as a resource that must be cleaned up.

Let’s Build the Dynamic Score Tracker

cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "How many scores? ";
    cin >> n;

    int* scores = new int[n];  // dynamic array

    for (int i = 0; i < n; i++) {
        cout << "Enter score " << (i + 1) << ": ";
        cin >> scores[i];
    }

    cout << "\n========================" << endl;
    cout << "   SCORES ENTERED" << endl;

    for (int i = 0; i < n; i++) {
        cout << scores[i] << " ";
    }

    cout << "\n========================" << endl;

    delete[] scores;  // free memory

    return 0;
}

How It Works (2–3 Lines)

  • You ask the user how many scores they want to store.
  • new creates an array exactly that size during the program.
  • delete[] removes the array after use, preventing memory leaks.

Run this instantly in the DevsCall AI Code Runner.

Learn Together with AI

Try asking:

  • “Explain new and delete in C++ using a real-world analogy.”
  • “Show me how to create a dynamic 2D array with new.”
  • “Write a C++ program that demonstrates a memory leak and how to fix it.”
  • “Convert my static array into a dynamic array.”

AI will help reinforce the concepts visually and interactively.

Practice Time

Write a C++ program that:

  • Asks the user how many students are in a class
  • Dynamically creates an array of marks
  • Stores each mark
  • Calculates the total and average
  • Prints them in this format:
text
1
2
3
4
5
6
========================
   CLASS REPORT
   Marks: 75 82 90 88 79
   Total: 414
   Average: 82
========================

Use new and delete[] properly to avoid memory leaks.

Ask your AI helper:

“How do I calculate the total and average of a dynamic array in C++?”

Frequently Asked Questions

Dynamic memory is memory allocated during program execution, allowing your program to adjust to varying data sizes at runtime.

new allocates memory on the heap and returns its address. It lets you create variables or arrays whose size you only know while the program is running.

Memory created with new must be released using delete or delete[]. Without this, the program wastes memory and may crash, this is called a memory leak.

A memory leak happens when memory is allocated but never freed. This leads to slower programs and can eventually exhaust system memory.

After understanding arrays and pointers. Dynamic memory becomes essential as you build flexible, real-world programs.

Still have questions?Contact our support team