Segmentation Faults

What Is Memory Segmentation? Explained With an Example

Memory segmentation divides a program's memory into regions such as code, data, heap and stack. See how each region works in C++, with a short example.

On this page
  1. The short answer
  2. Where the name comes from
  3. The usual segments of a C++ program
  4. Seeing the segments with an example
  5. Crossing a boundary
  6. How this ties to segmentation faults
  7. The stack and heap in more detail
  8. In short

The short answer

Memory segmentation is the idea of dividing memory into separate parts, called segments, that serve different purposes and follow different access rules. In a C++ program, the parts people usually mean are the code, global and static data, the heap, and the stack. Each part sits in its own range of addresses. When a program reaches memory it isn’t allowed to use, the operating system can stop it with a segmentation fault.

Where the name comes from

Some older processors supported hardware segmentation, in which memory was split into variable-sized segments, each with a base address and a length limit. Most modern systems instead rely mainly on paging, which splits memory into fixed-size blocks called pages. The name “segmentation fault” is a holdover from the older design, even though protection today comes mostly from paging.

The usual segments of a C++ program

These are informal labels rather than formal parts of the C++ standard, but they match how most systems lay out a running program:

  • Code: the compiled machine instructions. It is normally readable and executable, but not writable.
  • Global and static data: variables that exist for the whole run. Many systems keep initialized and zero-initialized globals in separate areas.
  • Heap: memory you request while the program runs, with new or malloc. It stays allocated until you free it.
  • Stack: space for function calls, parameters and local variables. It grows when functions are called and shrinks when they return.

Seeing the segments with an example

The following program prints an address for a global variable, a local variable and a heap object:

#include <iostream>

int global_value = 1;  // Lives in the data segment

int main() {
    int local_value = 2;           // Lives on the stack
    int* heap_value = new int(3);  // Lives on the heap

    std::cout << "global: " << static_cast<void*>(&global_value) << '\n';
    std::cout << "stack:  " << static_cast<void*>(&local_value) << '\n';
    std::cout << "heap:   " << static_cast<void*>(heap_value) << '\n';

    delete heap_value;
    return 0;
}

The exact numbers change between runs and between systems, so don’t compare them directly. Usually the stack and heap addresses fall in different ranges, and the global sits in yet another range. The point is that each object lives in a separate area that the operating system tracks.

Crossing a boundary

Segments have rules. Code and string literals are typically stored in read-only memory, so changing them isn’t allowed. Consider this:

const char* message = "hello";                  // Points into read-only data
char* writable = const_cast<char*>(message);
writable[0] = 'H';                              // Undefined behavior; often a segfault

The const_cast removes the protection the type system offers, but it doesn’t remove the rule at the memory level. On Linux and similar systems, this write usually crashes. The safe version is char message[] = "hello";, which makes a writable copy in an array, or a std::string.

How this ties to segmentation faults

Every memory access is checked against the rules for the region it touches. Reaching an address that isn’t mapped fails the check, and so does writing to a read-only region. Recursion that never ends fails when the stack runs into the limit set aside for it. A segmentation fault is simply the program running into one of these rules.

The specific checks that decide when a crash happens are explained in what triggers segmentation fault.

The stack and heap in more detail

The stack and heap are the two segments most programmers meet first. Stack objects are released automatically when their scope ends, while heap memory stays allocated until you free it. Mistakes such as returning a pointer to a stack object are a common source of crashes. The c heap vs stack comparison covers the difference in lifetime and size in more detail.

In short

A memory segment is a region of a program’s memory with its own purpose and access rules. The idea comes from older segmented designs, and most systems now use paging for protection. A segfault happens when a program breaks one of these rules, such as writing to read-only memory or touching memory it doesn’t own.

The complete guide to segmentation faults

What Is a Segmentation Fault in C++?
Read the guide

Keep exploring

Guides from other topics

All topics →

Sets in C++: How std::set Works

Learn how C++ std::set stores unique values in sorted order, with examples of insert, find, erase, custom comparators, and how it differs from hash sets.

std::set 5 min read