5.1.30 · D1C Programming

Foundations — Undefined behavior — comprehensive list, why to avoid

1,934 words9 min readBack to topic

This page builds every word and symbol the parent note leaned on. If a term in the parent felt like it appeared from nowhere, it is defined here from the ground up. Read top to bottom — each idea is a brick the next one stands on.


1. What is a program made of? Bytes and memory

Figure — Undefined behavior — comprehensive list, why to avoid

Why the topic needs this: almost every UB in the parent ("out of bounds", "use-after-free", "dangling pointer") is about touching a mailbox you were not allowed to touch. You cannot understand "out of bounds" without first seeing the street of mailboxes and where your fenced-off region begins and ends.


2. Variables and their lifetime

Figure — Undefined behavior — comprehensive list, why to avoid

Why the topic needs this: the parent's Example 3 (return &local;) is entirely about lifetime. The boxes are fine — the number might still be sitting there — but they no longer belong to local. Reading them is UB precisely because the lifetime ended, not because the memory vanished.


3. Pointers — the symbols *, &, and NULL

Everything in Category 1 of the parent uses pointers, so we earn every pointer symbol now. See Pointers and Memory in C for the full topic.

Figure — Undefined behavior — comprehensive list, why to avoid

Why these two and not something else: & is how you make a pointer (you need a house number to store), and * is how you use it (a house number is useless until you actually walk there). The parent's phrase "dereferencing a null pointer" literally means: doing *p when p holds a forbidden address.


4. Arrays and the meaning of a[i]

Figure — Undefined behavior — comprehensive list, why to avoid

Why the topic needs this: the parent's Example 4 reads a[3] from a size-3 array. Look at the figure — index steps past the fence into a box that belongs to something else. That step is out of bounds, and reading or writing it is UB. The valid range is to ; the box "one past the end" may be pointed at but never opened.


5. Integers, bits, and overflow

The parent's Category 2 (arithmetic UB) needs you to know how numbers are stored. Full topic: Integer Types and Overflow.

Figure — Undefined behavior — comprehensive list, why to avoid

Why the topic needs this: the parent's Example 1 deletes an overflow check because signed overflow is UB. The compiler is allowed to assume "this never happens". If overflow were merely "wrap-around" (as it is for unsigned), the check would survive. The whole example hinges on this asymmetry.


6. Sequence points — when does a change "count"?

Category 3 UB (i = i++) needs one more idea. See Sequence Points and Operator Precedence.


7. The compiler as a reasoner (the piece that makes UB scary)

Why the topic needs this: this single rule is the reason UB "poisons the whole program" (parent's mistake #2). The compiler reasons backwards and forwards from an impossible-by-assumption path, so a UB on line 50 can silently erase a check on line 10. Every worked example in the parent is a consequence of this one line.


Prerequisite map

Bytes and memory street

Variables and lifetime

Pointers: address-of and star

Arrays and index a of i

Integers width signed unsigned

Overflow signed vs unsigned

Side effects and sequence points

Sequencing UB

Compiler and optimizer assumption

Undefined Behavior topic

This feeds the parent: the UB comprehensive list.


Equipment checklist

Cover the right side and test yourself before moving on.

What is a byte and what range of values does it hold?
A single 8-bit box holding a number from 0 to 255, because patterns.
What is an address (versus the value at that address)?
A house number identifying a mailbox — its location, not its contents.
What does &x mean in plain words?
"The address of x" — give me the house number where x lives.
What does *p (dereference) do?
Go to the address stored in p and open that mailbox — read/write the thing p points at.
What is the lifetime of an automatic (local) variable?
From when the function starts using it until the function returns; then its boxes are reclaimed.
Why is a dangling pointer dangerous even though it holds a number?
The number is an old house address whose owner's lifetime ended; the box may now hold anyone's data.
For int a[3], which indices are valid to read?
Only 0, 1, 2 (that is 0 to n-1); a[3] is out of bounds and UB.
Which overflow is defined and which is UB?
Unsigned overflow is defined (wraps mod ); signed overflow is UB.
What is a sequence point?
A checkpoint where all earlier side effects must finish before later ones start.
State the optimizer's core assumption about UB.
It assumes UB never happens, so any path that would be UB is treated as unreachable and may be deleted.