C Tutorial Nondeterminism in C
The C Language
C History
Euclid’s Algorithm in C
Euclid on the PDP-11
Pieces of C
C Types
Struct bit-fields
Code generated by bit fields
C Unions
Layout of Records and Unions
C Storage Classes
malloc() and free()
Dynamic Storage Allocation
Simple Dynamic Storage Allocation
malloc() and free() variants
Fragmentation and Handles
Automatic Garbage Collection
Lazy Logical Operators
The Switch Statement
setjmp/longjmp: Sloppy exceptions
Nondeterminism in C





Library routines

  • malloc() returns a nondeterministically-chosen address 

  • Address used as a hash key produces nondeterministic results

Argument evaluation order


myfunc( func1(), func2(), func3() ) 

func1, func2, and func3 may be called in any order


Word sizes


int a;







/* Might be zero */






/* Might be zero */


Uninitialized variables 

Automatic variables may take values from stack 

Global variables left to the whims of the OS?


Reading the wrong value from a union 

union  int a; float b;  u; u.a = 10; printf(”%g”, u.b); Pointer dereference

*a undefined unless it points within an allocated array


and has been initialized


Very easy to violate these rules 


Legal: int a[10]; a[-1] = 3; a[10] = 2; a[11] = 5;


int *a, *b; a - b only defined if a and b point into the same array

How to deal with nondeterminism? Caveat programmer Studiously avoid nondeterministic constructs 

Compilers, lint, etc. don’t really help


Philosophy of C: get out of the programmers way 

  • C treats you like a consenting adult Created by a systems programmer (Ritchie)

  • Pascal treats you like a misbehaving child Created by an educator (Wirth)

  • Ada treats you like a criminal created by the Department of Defense

Contact for more learning: webmaster@freehost7com