Q1.
Is the essence of every computer bug a logical contradiction?
A1.
Yes, at an abstract level, every software bug is a mismatch between two formal specifications:
The Intended Logic (what the programmer believed or designed the system to do).
The Actual Logic (what the exact syntax, hardware architecture, and execution environment strictly mandate).
Computers are deterministic state machines; they never make "mistakes" or behave randomly on their own. They follow their instructions faithfully.
(The only edge cases that sit outside pure logical contradiction are physical hardware defects—like cosmic ray bit-flips, overheating circuits, or electrical faults—where physical reality violates the machine's underlying physical assumptions.)
Q2.
Is a computer a deterministic state machine?
Then why can Undefined Behavior occur randomly(?) in C language?
A2.
A physical computer is indeed a deterministic state machine (barring hardware faults or external noise), but in C, Undefined Behavior (UB) exists at the level of the programming language specification, not the silicon.
The reason UB appears "random" or erratic comes down to a fundamental clash between 3 layers:
1) the C Language Standard, 2) the Optimizing Compiler, and 3) the Physical Hardware.
Summary: Pseudo-Randomness vs. Strict Determinism
1) The CPU is deterministic:
Given the exact same machine code, register values, memory contents, cache states, and OS scheduling, it will produce the exact same result every microsecond.
2) Undefined Behavior breaks the translation:
the optimizer operates under an absolute axiom: "The programmer never wrote Undefined Behavior."
Because the compiler assumes UB is impossible, if a branch of code can only be reached via UB, the compiler concludes that branch is dead code and eliminates it entirely.
3) The environment provides invisible inputs:
Memory layout, compiler optimization levels (-O0 vs -O3), compiler versions, stack residue from prior calls, and OS memory paging all act as hidden inputs.
To the programmer looking only at the C text,
it looks like magic or bad luck ("it worked on my machine!").
In reality, the machine is executing a deterministic path through an unintended, corrupted state.