What is memory segmentation?
Why Interviewers Ask This
Mid-level Operating Systems roles require deep understanding of this topic. Interviewers ask this to separate candidates who truly understand the mechanics from those who only know surface-level concepts.
Answer
Segmentation is a memory management scheme that divides a process's address space into variable-sized, logical segments corresponding to the logical structure of the program — code, data, stack, heap, etc. Each segment has a name/number and a length. Segment table: maps (segment number, offset) → (base, limit). Entry contains: base address (where segment starts in physical memory) and limit (segment's length). Address translation: logical address = (segment, offset). If offset ≥ limit → segmentation fault. Physical address = base[segment] + offset. Example segments: Segment 0 = main program code, Segment 1 = heap, Segment 2 = stack, Segment 3 = shared library. Advantages: logical organization matches programmer's view; different protection for different segments (code=read-only, stack=read-write); easy to share (e.g., two processes share the same code segment); no internal fragmentation. Disadvantages: external fragmentation (segments of varying sizes leave unusable holes between them); needs compaction or sophisticated allocation; variable-size allocation is complex. Segmentation vs Paging: segmentation is logical division (programmer's view); paging is physical division (hardware convenience). Segmentation visible to programmer; paging usually transparent. Combined Segmentation + Paging: Intel x86 supported both (GDT/LDT for segments, then page tables for each segment). Linux and Windows set segment base=0 and limit=max, using flat segmentation (effectively only paging). Pure segmentation: less common today. Modern use: segments still present conceptually (code, data, BSS, heap, stack sections in ELF) but implementation is through paging.
Pro Tip
This topic has Operating Systems-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.