Memory segmentation is a memory management scheme in which a program’s address space is divided into variable-sized logical units called segments, such as code, data and stack, each described by a base address, a limit and access rights. Every memory reference names a segment and an offset within it, and the hardware translates that pair into a linear or physical address while checking that the offset stays inside the limit.
In operating systems, segmentation was one of the first practical answers to relocation and memory protection. It survives mainly in the x86 architecture, where modern 64-bit systems have reduced it to a nearly flat model and rely on paging for most translation and protection work.
How segmented address translation works
A segmented address is a two-part logical address: a segment identifier and an offset. The processor uses the identifier to look up a segment descriptor in a table maintained by the operating system. The descriptor supplies the base, the limit and the permissions, and the hardware computes base plus offset to obtain the resulting address.
Before the addition, the hardware performs a bounds check. If the offset exceeds the limit, or if the access violates the descriptor’s rights, the processor raises a protection fault and the kernel usually terminates the program. This check gives every segment its own hardware-enforced boundary.
| Field | Purpose | Effect of a violation |
|---|---|---|
| Base address | Start of the segment in linear memory | Not applicable |
| Limit | Size of the segment | Fault when the offset is out of range |
| Type and rights | Code, data, read, write or execute permissions | Fault on a forbidden access |
| Privilege level | Ring required to use the segment | Fault on an unprivileged access |
| Present bit | Whether the segment is in memory | Segment-not-present fault |
Segmentation in the x86 architecture
The Intel 8086 introduced a simple form of segmentation with 16-bit segment registers named CS, DS, SS and ES. In this real mode, the processor shifts the segment value left by four bits and adds the offset, producing a 20-bit address that can reach 1 MB of memory. Segments overlap freely and offer no protection.
The 80286 and 80386 added protected mode, in which segment registers hold selectors that index the Global Descriptor Table or a Local Descriptor Table. Each descriptor carries a real base, a limit and a privilege level, which allowed operating systems to isolate programs and to separate the kernel in ring 0 from applications in ring 3.
| Processor mode | Segment register content | Practical role |
|---|---|---|
| Real mode | Paragraph address shifted by four bits | Reach 1 MB with 16-bit registers |
| 16-bit protected mode | Selector into a descriptor table | Protection and relocation |
| 32-bit protected mode | Selector with 4 GB limits | Mostly flat model with paging |
| 64-bit long mode | Base forced to zero except FS and GS | Thread-local storage and per-CPU data |
In 64-bit long mode, the processor ignores the base and limit of most segments. Only the FS and GS registers keep a usable base, which Linux, Windows and macOS use for thread-local storage and per-processor kernel data.
Segmentation compared with paging
Segmentation and paging solve the same problem in different ways. Segmentation follows the logical structure of a program, so a segment matches a meaningful unit such as a code module or a stack. Paging divides memory into fixed-size pages, typically 4 KB, which ignores program structure but makes allocation far simpler.
| Aspect | Segmentation | Paging |
|---|---|---|
| Unit size | Variable, set per segment | Fixed, commonly 4 KB |
| Visibility to programs | Visible in the logical address | Transparent to programs |
| Fragmentation type | External fragmentation | Internal fragmentation |
| Protection granularity | Per logical unit | Per page |
| Sharing | Natural for whole modules | Shared pages mapped into several spaces |
| Modern use | Minimal, mainly FS and GS | Primary mechanism in all major systems |
Some systems combined both approaches as segmented paging, in which each segment has its own page table. Multics is the best-known example, and 32-bit x86 also translated segmented addresses to linear addresses first and then paged them through the memory management unit.
Strengths and limitations of segmentation
Segmentation’s main strength is that protection matches program structure. A code segment can be marked execute-only, a stack segment can grow within its limit, and a library segment can be shared among processes with a single descriptor. Bounds checks also catch many out-of-range accesses in hardware.
Its main weakness is external fragmentation. Because segments vary in size, free memory breaks into gaps that may be too small for the next request even when their total is sufficient. Solving this requires compaction or swapping whole segments, both of which are expensive. Segment registers also complicate compilers and pointers, which is why the industry adopted the flat memory model.
- Advantage: protection and sharing align with logical program units.
- Advantage: a small number of descriptors can describe a large program.
- Limitation: variable sizes cause external fragmentation and costly compaction.
- Limitation: segmented pointers make programming and porting harder.
- Limitation: whole-segment swapping moves far more data than page-level swapping.
Segmentation and the segmentation fault
The common error name segmentation fault comes from this scheme, even though most modern systems no longer use segments for protection. Today a segmentation fault on Linux or macOS usually results from a page-level violation, such as touching an unmapped address or writing to a read-only page, which the kernel reports to the program as the SIGSEGV signal.
The table below separates what the name suggests from what actually happens on a current 64-bit system.
| Situation | Historical cause | Modern cause |
|---|---|---|
| Dereferencing a null pointer | Offset outside a segment limit | Access to an unmapped page |
| Writing to program code | Write to an execute-only segment | Write to a read-only page |
| Stack overflow | Offset past the stack segment limit | Hit on a guard page |
In simple terms
Memory segmentation is like storing a program in labeled boxes of different sizes: one box for the instructions, one for the data, one for temporary notes. Each box has a starting point and a size written on its label.
Whenever the program reaches into memory, the computer checks the label and refuses any reach beyond the edge of the box. This keeps programs from damaging each other. The downside is that boxes of many sizes leave awkward gaps, so modern computers mostly use equal-sized pages instead.
A practical example
A systems programmer maintains firmware for an industrial controller built on a 32-bit x86 processor running a small real-time operating system. The system defines separate segments for the kernel code, the kernel data and each of three control tasks, all described in the Global Descriptor Table.
During testing, one task writes past the end of a 2 KB buffer. Because the task’s data segment has a limit of exactly 64 KB, the stray write stays inside its own segment, but a later bug that computes a negative offset triggers a general protection fault. The kernel stops only that task and logs the faulting selector and offset.
The programmer reads the log, finds the faulty pointer arithmetic and fixes it. The segment limits contained the damage to one task, and the two other control loops kept running throughout the test.