5.1.24 · D1C Programming

Foundations — Unions — overlapping memory

1,867 words8 min readBack to topic

Before you can read a single line of the parent note, you must own every word and symbol it throws at you. Below, each item is built from the one before it. Nothing is used before it is drawn.


1. A byte — the smallest labelled box

Figure — Unions — overlapping memory

Look at the figure: eight tiny switches (bits) sit inside one box. Each switch is OFF (0) or ON (1). Eight switches give patterns, so one byte holds to .

Why the topic needs it: a union is described entirely in terms of how many bytes each member takes and which bytes overlap. If "byte" is fuzzy, everything after is fuzzy.


2. An address — the number written on the box

Figure — Unions — overlapping memory

In the figure the red box is at offset 0 — the very first box of the object. The parent note keeps saying "all members start at the same address (offset 0)". This picture is that sentence.


3. A type — the rule for reading a group of boxes

Type Boxes (bytes) How it reads them
char 1 one byte as a small number / ASCII letter
int usually 4 four bytes as a whole number
float 4 four bytes as a decimal, using a special bit layout
double 8 eight bytes as a more precise decimal

Why the topic needs it: the parent's key warning — reading a different member reinterprets the raw bits, it does not convert — is exactly "same boxes, different ruler."


4. sizeof — the tool that measures the boxes

Reading the notation: sizeof(union Mix) == 8 means "measuring the union gives 8 boxes." The == here is C's is-it-equal? test, giving true/false — not assignment.


5. max and the "worst case"

Figure — Unions — overlapping memory

The figure stacks each member's bytes from offset 0. The red bar is the largest member — its length sets the room size, because the room must survive the worst case (the biggest tenant).

Why and not (sum)? A struct adds member sizes because members sit in separate boxes (sum). A union overlaps them on the same boxes, so only the longest one matters (max). That single choice — sum vs max — is the whole difference. See Structs — separate memory for each member.


6. Alignment — why sizes get rounded up

Why the topic needs it: the parent writes "(plus padding for alignment)" and never explains it. Now you know: padding is the rounding-up so arrays of unions keep every element on a legal address. Full story in Memory alignment and padding.


7. Endianness — which byte is "first"

Figure — Unions — overlapping memory

The number 65 in one int is, in hex, 00 00 00 41 (only the last byte carries value). The figure shows little-endian laying it down as 41 00 00 00 — the red 41 landing in box 0. So reading box 0 as a char gives 0x41 = 'A'. That is exactly the parent's u.c[0] == 'A' example. Deeper: Endianness — byte order.

Why the topic needs it: the parent's type-punning example only makes sense once you know why the 'A' shows up at c[0] and not c[3].


8. enum — a name for a small set of choices


9. Little C syntax you'll meet

Why the topic needs it: the parent's mistakes hinge on = vs == and on {65} touching only member one. Now those aren't mysteries.


Prerequisite map

bit: one on off switch

byte: box holding 0 to 255

address and offset 0

type: rule to read boxes

sizeof: count the boxes

max: biggest member

alignment: round size up

endianness: which byte first

enum: named choices for the tag

UNION overlapping memory


Equipment checklist

A byte holds how many distinct values, and why?
256, because 8 bits give patterns.
What does "offset 0" mean and why do all union members share it?
The first box of the object; every union member starts there so they overlap on the same memory.
Two rulers over the same four boxes — what does that model?
Type punning: the same bytes read as different types give different values, with no conversion.
What question does sizeof(T) answer?
How many bytes one value of type T occupies on this machine.
In the size formula, what do and each do?
picks the biggest member's size; rounds it up to a legal multiple.
Why is a union's size max-of-members while a struct's is sum-of-members?
A union overlaps members on the same boxes (max); a struct gives each its own boxes (sum).
On little-endian, where does the least-significant byte of an int go?
In the lowest-address box (offset 0), so u.c[0] reads it.
What is an enum and why does a tagged union use one?
A named set of integer constants; it labels which union member is currently live.
Difference between = and == in C?
= assigns a value; == tests equality and yields true/false.

Connections

  • Structs — separate memory for each member
  • Memory alignment and padding
  • Endianness — byte order
  • Type punning and bit-level reinterpretation
  • Enums
  • sizeof operator