5.1.15 · D1C Programming

Foundations — Memory layout — text, data, BSS, heap, stack segments

1,942 words9 min readBack to topic

Before you can say "this variable lives in BSS," you must already be fluent in a stack of tiny ideas: what a byte is, what an address is, what "read-only" means, what "grows up" means. The parent note quietly assumes all of them. This page builds each one from absolute zero, in the order they depend on each other.


1. A byte — the atom of memory

Why start here? Because everything in the topic — a variable, an instruction, a pointer — is ultimately just a run of bytes. If you don't picture the box, "4 MB in BSS" is meaningless noise.

Figure — Memory layout — text, data, BSS, heap, stack segments

The picture shows memory as a vertical ruler of boxes. Each box is one byte. To the left of each box is its position number — that number is the whole game, and it has a name: the address.


2. An address — the box's house number


3. Hexadecimal — why addresses look like 0x7ffe


4. A variable — a name glued to some boxes


5. Initialized vs uninitialized — the Data/BSS split, from zero

This distinction is the heart of why the parent has two separate segments for globals. Build it carefully.

Figure — Memory layout — text, data, BSS, heap, stack segments

The picture makes the split obvious: a box holding 5 must physically carry that 5 in the file on disk (→ Data segment). A box that is just "zero" can be described by a sticky note — "reserve N boxes, all zero" — costing almost nothing on disk (→ BSS segment). This is the entire reason Static and global variables — storage duration separates Data from BSS.


6. Read-only vs writable — the permission stamp


7. Growth direction — "up" and "down" on the ruler

Figure — Memory layout — text, data, BSS, heap, stack segments

Look at the arrows in the figure. The heap (burnt-orange) starts in the middle and its top edge climbs upward every time you malloc. The stack (teal) starts near the ceiling and its bottom edge sinks downward every time a function is called. They rush toward the empty gap between them — this is exactly the parent's "they share the gap in the middle." If they ever touch, you get a stack-overflow / heap-overflow, which connects to Buffer overflow and stack smashing.


8. Two special markers — program break and stack pointer


9. Static storage duration — the word "static" means two things


Prerequisite map

byte = one box

address = box number

hexadecimal 0x notation

variable = name on boxes

initialized vs zero

Data vs BSS split

read-only permission

Text segment shared

grows up vs grows down

program break and stack pointer

storage duration

static vs automatic

Memory layout topic

Read it top-down: the byte feeds address, address feeds every notion of position and direction, and permission + duration decide which segment a value lands in. All four streams pour into the Memory Layout topic.


Equipment checklist

Recall Self-test: can you answer each before peeking?

What is a byte, in one sentence? ::: The smallest named chunk of memory, holding one number from 0 to 255. What does "address" mean? ::: The position number of a byte on the memory ruler. What does &x give you? ::: The address (box number) where x starts, not its value. Why is 0x2000 "higher" than 0x1000? ::: It is a bigger number, so it is a box further down the ruler. How many bytes is a typical int? ::: 4. Why does an initialized global cost disk space but a zero one doesn't? ::: The non-zero value is real information stored in the file; all-zeros is described by a size note only. What does "read-only" let you do and forbid? ::: Read the bytes, but never write them (writing crashes). "Heap grows up" means what about addresses? ::: Each new heap block lands at a higher address. "Stack grows down" means what about addresses? ::: Each new call frame lands at a lower address. Which marker tracks the heap's top edge? ::: The program break. Which marker tracks the stack's top edge? ::: The stack pointer (e.g. rsp). Do all globals have static storage duration? ::: Yes, even without the static keyword.


Connections

  • यह Hinglish में पढ़ो →
  • Static and global variables — storage duration — the Data vs BSS rule these foundations lead to.
  • malloc, calloc, realloc and free — how the program break moves the heap.
  • Stack frames and the calling convention — what the stack pointer pushes.
  • Pointers and dangling pointers — addresses when the box outlives its name.
  • Virtual memory and paging — how this ruler maps to real hardware.
  • Buffer overflow and stack smashing — what happens when growth collides.