Skip to content

Undefined behaviour

Category: Hazards · Status: stub · Lessons: chapter 02, Shared state

One line: A program the language standard stops describing: once it has one, no requirement is placed on what it does, so a right answer on this build is not evidence of anything.

Also called: UB, undefined behavior.

How it connects

flowchart LR
  n_dangling_pointer["Dangling pointer"]
  n_data_race["Data race"]
  n_undefined_behaviour["Undefined behaviour"]
  n_dangling_pointer -->|can cause| n_undefined_behaviour
  n_data_race -->|can cause| n_undefined_behaviour
  classDef center stroke-width:3px
  class n_undefined_behaviour center
  classDef outside stroke-dasharray: 4 3
  class n_dangling_pointer,n_data_race outside

In each language

Rust Behavior considered undefined ↗ lists what unsafe code must not do; safe code cannot reach any of it
Go There is none of this kind for a racing program: the memory model ↗ defines what a racy read may observe rather than abandoning the program
C Undefined behaviour ↗: the standard imposes no requirements, and a data race or a dead-frame read is one
C++ Undefined behaviour ↗, with the same consequence
Java Also none: JLS 17.4 ↗ gives unsynchronized reads weak guarantees, not undefined ones

Where to read more