wleci
AboutProjectsBlogContact
Contact
Back to blog
C++MemoryTutorialFeatured

Dynamic Memory Allocation in C++: Building and Demolishing in RAM

Understand the new and delete operators. We explain the difference between the stack and the heap, how to manage memory, and avoid memory leaks.

February 4, 20264 min read
Share:

Dynamic Memory Allocation: Your Own Construction Company in C++

Welcome to one of the most important programming lessons. Until now, when you created a variable like int x = 5;, C++ took care of everything. It reserved the space, put the number there, and cleaned up when the function ended. This is called automatic memory management on the Stack.

But what if you don't know how much space you'll need before running the program? What if you want the data to last longer than the function where it was created? This is where Dynamic Memory Allocation on the Heap comes in.


1. Stack vs. Heap – Where does your data live?

Imagine two types of storage:

  1. Stack: It's like a stack of plates. It's very organized and fast but has a limited size. When a function ends, C++ simply "takes off" the last plates and throws them away. Everything happens automatically.
  2. Heap: It's a large, open construction site. You can order an area of memory as large as you want at any time. But there's a catch: you are the construction manager. If you build something there, you must demolish it yourself. If you forget, the construction site will eventually fill up and there will be no room for others.

2. The new Operator – Ordering a Plot

When you want to create something on the heap, you use the new keyword. This operator does two things:

  1. Finds free space in memory (on the heap).
  2. Returns the address (pointer) to that space.

Example:

cpp
int* p = new int; // We reserve space for one int on the heap *p = 100; // We put the value 100 there

Notice that p is a regular pointer that lives on the stack, but what it "points to" is located on the distant heap.


3. The delete Operator – Cleaning up after the party

This is the moment where most programmers fail. Every piece of memory reserved by new must be freed by delete. If you don't do this, a Memory Leak occurs.

cpp
delete p; // We free the memory at address p p = nullptr; // Good practice: null the pointer so you don't use it by accident

The Golden Rule: For every single new, there must be exactly one delete.


4. Dynamic Arrays: new[] and delete[]

Sometimes you don't need a single int, but an entire array whose size is provided by the user while the program is running.

cpp
int size; std::cin >> size; int* array = new int[size]; // Array allocation // We use it like a regular array array[0] = 10; // WARNING: When freeing an array, we must use delete with brackets! delete[] array;

If you use a regular delete instead of delete[] for an array, you only free the first element, and the rest remains in memory forever as "ghosts."


5. Common Mistakes (How not to cause a disaster)

A. Memory Leaks

These occur when you lose the pointer to memory that you haven't freed.

cpp
void function() { int* data = new int[1000]; // ... do something ... // Forgot delete[] data! } // The function ends, the 'data' pointer disappears, but 1000 ints still sit in RAM.

B. Dangling Pointers

This is when you use a pointer after you've already freed the memory.

cpp
int* p = new int(5); delete p; std::cout << *p; // ERROR! You are knocking on the door of a house that was demolished.

C. Double Free

Attempting to perform delete on the same address twice. It usually ends with an immediate program crash.


6. Modern C++ (Smart Pointers) – Forget about delete?

You should know that in modern C++ (since the C++11 standard), we rarely use new and delete manually. We have Smart Pointers for this, such as std::unique_ptr or std::shared_ptr, which do the delete for you when they are no longer needed.

However, understanding manual allocation is crucial to know what these smart tools are doing under the hood.


Summary for the Monkey Step-by-Step:

  1. Need space on the construction site (Heap)? Use new.
  2. Got an address (Pointer)? Write it down, because without it, you won't find your data.
  3. Finished the work? You must demolish the building using delete (or delete[] for arrays).
  4. Lost the address note before demolishing? You have a memory leak. Your RAM "bloats."
  5. Demolish twice? The program will explode.

Would you like to learn how to create your own "data types" now—moving into the world of Classes and Objects?

You might also like

ReferencesC++

Stage 5: References and References as Function Parameters

Understanding the reference mechanism in C++: how variable aliases work and why they are key to efficient argument passing.

3 min read
C++Programming

C++ String and Number Conversion: Deep Dive into sprintf and strtof

A comprehensive guide to classic conversion methods in C++. Learn how to safely use sprintf and strtof, and explore their modern alternatives.

4 min read
C++Tutorial

C++ References: Meet the 'Nicknames' of Your Variables

Understand references once and for all. How do they differ from pointers? How to use the & operator in declarations? A step-by-step guide.

4 min read
Back to blog
wleci.pl

Full-stack Developer

I build modern web applications with passion for clean code and good design.

[email protected]
Poland

Navigation

  • Home
  • About
  • Projects
  • Blog
  • Contact

Services

  • Web applications
  • Websites
  • API & Backend
  • Consulting

Technologies

  • React / Next.js
  • TypeScript
  • Node.js
  • PostgreSQL

Social

© 2026 wleci.pl. All rights reserved.

Privacy policy•Terms of service

Made with in Poland