The Memory Landscape, Explained with Code Examples

UPI Study › Courses › C++ Programming › Free lessons › Lesson 10.6 Free with a free account

The Memory Landscape

C++ Programming · Chapter 10: File Access and MemorySection 10.6 · lesson 74 of 84video coming soon

← PointersNext lesson: Dynamic Allocation →

Earn 3 college credits for C++ ProgrammingRecommended for credit by NCCRS · Computer Science 242 · self-paced · one payment, lifetime access

In this lesson

Stack, heap, and how programs use each region.

  • Name the 4 regions and what lives in each
  • Explain why the stack is almost free to use
  • Compare stack and heap; know who manages each
  • Tell stack overflow from heap exhaustion

Transcript

Everything said in the video, section by section (1,662 words). Read along, or search it.

Opening Overview

Zooming Out to Four Regions

So far, you've followed single variables: where a local lives, and what happens to it when its function returns. This section zooms out to the whole picture. A running program occupies four distinct regions of memory. We'll see what goes in each, who is responsible for each, and what it looks like when one of them runs out.

Memory Map

How the Address Space Is Divided

When a program runs, the system gives it a range of memory addresses to work in. That range isn't uniform. It's divided into regions with quite different rules. At the bottom, the code region holds the compiled instructions. Above it sits the static region, for globals. Then comes the heap, which grows upward, and at the top, the stack, which grows downward. Between those two is free space, and the stack and heap grow toward each other through it.

Region Summary

The Four Regions at a Glance

Here are the four regions at a glance. Code holds instructions and text literals, set up by the system at load time, and fixed at build time. Static holds globals and static locals. It also lasts the whole run, with its size known at build time. The stack holds parameters, locals and return addresses. It's managed automatically, and each frame lasts until its call returns. Its maximum is set at start-up. And the heap holds anything whose size or lifetime is settled while the program runs. The programmer manages it, explicitly, and it grows as needed. Knowing which region something sits in tells you most of what you need to know about it.

Static Region

Globals and Static Locals Last All Run

The instructions themselves need somewhere to live, and they get a region of their own. Its size is settled when the program is built, and nothing is added to it while running. The static region holds variables declared outside every function, and locals marked to persist between calls. It's set up before main begins, and released after it ends. Two consequences follow. Anything here is zero-filled unless given another value: total processed prints zero, though nobody assigned it. And nothing here is recovered while running, so a large table occupies its space from start to finish. The static local calls survives between calls, so two calls print one and two. The last line is the banner, a text literal, and that's where we go next.

Code Region

Text Literals Live in Read-Only Memory

The system normally marks the code region read-only, which explains a rule you met much earlier. Text literals usually live here. So modifying a literal's characters doesn't just break a convention. It writes into protected memory, and the program is stopped. Here the compiler refuses the write outright, because of const. Force it past const with a cast and the program is stopped when it runs, so we don't run that.

Stack Detail

Why Stack Frames Are So Cheap

The stack is the region you already know in detail. Each call pushes a frame, and each return pops it. What's worth adding is why it's so cheap. Frames are always added and removed at one end, so the system only tracks where the top currently is. Reserving a whole frame is a single adjustment to that marker. Releasing it is the same adjustment, in reverse. There's no searching for a gap, and no bookkeeping, because last-in-first-out guarantees the answer. The price is inflexibility. Nothing on the stack can outlive the call that made it. And the region has a fixed maximum, commonly a few megabytes, which can't grow if you need more.

Heap Motivation

Three Jobs the Stack Cannot Do

The heap exists to lift both of those restrictions. There are three situations the stack simply can't serve. First, the size isn't known until the program runs. A frame's layout is fixed at compile time, so a collection sized by input has nothing to reserve. Second, the data must outlive the function that created it. Frames are popped on return, so anything in one is gone. Third, the data is large. A few megabytes won't hold a large image or dataset. So the heap lets a program request a block of any size, at any moment, and release it whenever it chooses, in any order.

Heap Cost

Flexibility Has a Price

That flexibility has to be paid for. Each request must be satisfied by finding a suitable free block, which takes real work. The blocks in use, and the gaps between them, must be tracked. And the part that matters most: the heap doesn't know when you've finished with a block. It stays reserved until it's released. So a program that keeps requesting, and never releases, will consume the heap steadily, until nothing is left.

Surprise

Vectors and Strings Already Use the Heap

Here's the surprise. You've been using the heap all along. A vector, or a string, holds its elements on the heap. That's exactly how it can grow to fit whatever it's given, when a fixed array can't. The vector object itself is small. It records where the elements are, how many there are, and how much room is spare. That object is an ordinary local, sitting in the stack frame.

Demo

Twenty-Four Bytes Regardless of Size

Run it: the vector object is twenty-four bytes on this machine, and the elements are not inside it. After a thousand items, it is still twenty-four bytes. The elements sit in a heap block that the object requested. When the object goes out of scope, it releases that block automatically. That's why most C plus plus programs, built from types that manage their own heap memory, need very little manual memory management.

Comparison

Stack and Heap Side by Side

Here are the two active regions, side by side. The stack is managed automatically, and released strictly in reverse. The heap is managed by the programmer, or a type acting for them, and released in any order. Stack sizes are decided at compile time, with a small, fixed maximum. Heap sizes are decided while running, limited only by the machine. Stack allocation is almost free. Heap allocation is slower, because a block must be found. A stack variable lives until its call returns; a heap block, until it's released. The stack can't leak, and can't fragment, because it's one contiguous run. The heap can do both. On the stack, correctness is structural. On the heap, it's a matter of the program doing what it promised.

Failure Modes

Telling Stack Overflow from Heap Exhaustion

Both regions can run out, and telling the two failures apart from their symptoms is a genuinely useful skill. Stack overflow usually comes from recursion that's too deep, or has no stopping case, or from a very large local array. The compiler can even warn about the first: depth calls itself on every path. It happens almost instantly. The system stops the program, usually with no chance to react, and the same input fails the same way every time. Heap exhaustion is gradual. A leaking program, like handle request, may run for hours, or days, before an allocation fails. Its memory use climbs steadily and never comes back down, and the failure is often intermittent. So check stopping cases and local arrays for the first. For the second, check that every request has a matching release, on every path out.

Leaks

The Signature of a Memory Leak

A leak is a heap-only fault, and its signature is a memory figure that only ever rises. Picture a program that handles ten thousand requests correctly, then fails on the next one. It's almost never running out of work to do. It's holding on to something from each of the previous ten thousand. Watching memory use over time distinguishes this from every other kind of failure, in a few minutes.

Predict

Place Every Name in a Region

Let's put it together with a snapshot. Here's a small program, paused inside the average function, on the line that sets sum to zero. There's a global counter, total processed, and a global pointer, banner, referring to the text Report v two. Main builds a vector called marks, pushes seventy-two and sixty-five, and calls average, passing marks by reference. Before reading on, try placing every named thing into one of the four regions. Pause and predict.

Walkthrough

Where Each Named Thing Lives

Total processed is declared outside all functions, so it's in the static region, for the whole run. Banner is split across two regions. The pointer itself is a global, in static. The characters it refers to are a literal, in the read-only code region. Marks is a local of main, so it sits in main's stack frame. But seventy-two and sixty-five are on the heap, even though the source never says so. Avg has its storage reserved in main's frame, though its value hasn't arrived yet. Values is a reference parameter in average's frame, so no elements were copied. Sum is a local in the frame on top. And the return address sits in average's frame, recording where in main to resume. Run it: the average is sixty-eight point five, read through that reference from the heap.

Key Terms

Vocabulary of the Memory Landscape

The address space is the range of addresses a running program may use, divided into regions. The code region holds instructions and literals. The static region holds variables that last the whole run. The stack is last-in-first-out, with one frame per active call, managed automatically. The heap hands out blocks on demand, released in any order. Fragmentation is free heap space broken into gaps too small to use. And a leak is heap memory never released, lost for the rest of the run.

Quick Check

The Pointer Vanishes, the Ints Leak

Quick check. A function sets a local pointer, scores, to a new array of fifty ints, and returns without releasing it. Where do the pointer and the fifty ints live, and what happens to each when it returns? Pause and think. The pointer is a local, in the function's stack frame, so it's gone the moment the frame is popped. The fifty ints are on the heap, and stay reserved. Nothing refers to them any more, so they've leaked.

Key Takeaways

Summing Up the Four Regions

To summarize. Code and static regions are fixed at build time, and last the whole run. The stack is fast because only one marker moves, but nothing on it outlives its call. The heap gives any size and any lifetime, at the price of searching, bookkeeping, and responsibility. A vector's handle is on the stack, and its elements are on the heap. Stack overflow fails at once. A leak climbs, then fails.

About C++ Programming at UPI Study

C++ Programming (Computer Science 242) is a self-paced online course of 84 video lessons in 11 chapters, from your first program to decisions, loops, arrays, functions, classes, files, pointers and inheritance. It carries a NCCRS credit recommendation for 3 semester credits.

Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Why take it here

Why students take C++ Programming with UPI Study

Your first real programming languageC++ teaches how programs really work: variables, control flow, functions, memory and objects. It is the base for data structures, algorithms, systems and game programming, and for most computer-science degrees.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Where the credit goes

Universities that work with NCCRS

C++ Programming carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.

FAQ

Questions students ask us

About The Memory Landscape

What are the four regions of memory in a running C++ program and what goes in each?
A running program uses four regions: code, static, stack, and heap. Code holds the compiled instructions and text literals. Static holds globals and static locals for the whole run. The stack holds parameters, locals, and return addresses, one frame per active call. The heap holds anything whose size or lifetime is settled while the program runs.
Why is allocating memory on the stack so fast compared to the heap?
Stack allocation is almost free because frames are added and removed at one end, so the system only tracks where the top is. Reserving a frame is one adjustment to that marker, and releasing it is the same adjustment in reverse. The heap must search for a suitable free block and track used blocks and gaps.
What is the difference between stack and heap memory, and who manages each?
The stack is managed automatically and released in reverse order when each call returns. The heap is managed by the programmer, or by a type such as a vector acting for them, and blocks can be released in any order. Stack sizes are fixed at compile time with a small maximum. Heap sizes are decided at run time.
How can I tell a stack overflow from heap exhaustion?
A stack overflow happens almost instantly, usually from recursion that is too deep or has no stopping case, or from a very large local array, and the same input fails the same way each time. Heap exhaustion is gradual: a leaking program's memory use climbs steadily and never comes back down, until an allocation eventually fails, often intermittently.

About the C++ Programming course

How many college credits is C++ Programming?
C++ Programming (Computer Science 242) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does C++ Programming cost and what is included?
$250, paid once, with lifetime access: all 84 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does C++ Programming take?
It is self-paced with no deadlines. The 84 lessons total about 0 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

Ready for the full course? Earn 3 credits in C++ Programming.

84 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.

C++ Programming
3 credits · NCCRS
Enroll →