Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    C++C++

    C++ fundamentals

    • Your first C++ program
    • Variables and types
    • Operators and input
    • Conditions: if and switch
    • Loops

    Functions and collections

    • Functions
    • References: by value or by reference?
    • Strings and arrays
    • std::vector
    • map, set and unordered_map

    Memory and pointers

    • Pointers
    • Stack, heap, new and delete
    • Smart pointers and RAII

    Classes and OOP

    • struct and class
    • Constructors and destructors
    • Inheritance and virtual functions
    • Templates

    The STL and professional C++

    • Algorithms and lambdas
    • Exceptions
    • Recursion
    • Project: a grade book
    C++

    Memory and pointers · Lesson 12 of 21

    Stack, heap, new and delete

    Memory that frees itself, and memory you're responsible for.

    10 minutes3 games

    A program uses two areas of memory:

    • The stack: ordinary local variables. Very fast, freed automatically when the scope ends (}), but small (often a few megabytes).
    • The heap: a large area you allocate from at runtime with new, which stays until you free it with delete.
    StackHeap

    How

    int x = 5;

    new int(5)

    Who frees

    automatic at }

    you, with delete (or a smart pointer)

    Speed

    very fast

    slower

    Size

    small

    large

    Lifetime

    until the scope ends

    until you free it

    C++
    #include <iostream>
    
    int main() {
        int onStack = 10;                 // freed automatically
    
        int* single = new int(42);        // one int on the heap
        int* many = new int[5];           // an array of 5 on the heap
        for (int i = 0; i < 5; i++) many[i] = i * i;
    
        std::cout << onStack << " " << *single << " " << many[4] << "\n";
    
        delete single;                    // new   -> delete
        delete[] many;                    // new[] -> delete[]
        single = nullptr;                 // don't leave it dangling
        many = nullptr;
        return 0;
    }

    Important

    Every new needs exactly one delete, and every new[] a delete[]. Forget it and you have a memory leak: memory reserved that nobody will use. Free twice (double free) or use after freeing (use after free) and you get undefined behavior and well-known security holes.

    Spot the bug

    This function runs every second on a server, and after days memory runs out. Why?

    In modern C++ you rarely write new and delete yourself. std::vector and std::string use the heap internally and free it themselves; for a single heap object use std::make_unique (next lesson).

    Quick question

    A program needs an array of 50 million numbers. Where should it live?

    Interview question

    What's the difference between the stack and the heap? What causes a stack overflow?

    Exercise

    Allocate a heap array of n = 6 numbers, fill it with 1, 2, 4, 8, 16, 32 (each double the last), print their sum, and don't forget to free the memory.

    Expected output: 63

    C++
    #include <iostream>
    
    int main() {
        int n = 6;
        
        return 0;
    }

    Learn more from

    • learncpp: The stack and the heaplearncpp.com
    • learncpp: Dynamic memory allocation with new and deletelearncpp.com
    • cppreference: new expressionen.cppreference.com

    Played 0 of 3 games

    Sign in to save your progress and earn XP.

    PreviousPointersNextSmart pointers and RAII
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.