The C Language the C Language C History BCPL C History C History
The C Language
The C Language Currently, the most commonly-used language for embedded systems
“High-level assembly”
Prof. Stephen A. Edwards Very portable: compilers exist for virtually every processor
Easy-to-understand compilation
Produces efficient code
Fairly concise
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
C History BCPL
Developed between 1969 and 1973 along with Unix Designed by Martin Richards (Cambridge) in 1967
Due mostly to Dennis Ritchie Typeless
Designed for systems programming • Everything an n-bit integer (a machine word) • Pointers (addresses) and integers identical
• Operating systems
• Utility programs Memory is an undifferentiated array of words
• Compilers Natural model for word-addressed machines • Filters
Local variables depend on frame-pointer-relative
Evolved from B, which evolved from BCPL addressing: dynamically-sized automatic objects not permitted
Strings awkward • Routines expand and pack bytes to/from word arrays
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
C History C History
Original machine (DEC PDP-11) Many language features designed to reduce memory was very small • Forward declarations required for everything • 24K bytes of memory, 12K used • Designed to work in one pass: must know everything for operating system • No function nesting
Written when computers were
big, capital equipment PDP-11 was byte-addressed • Group would get one, develop new language, OS • Now standard • Meant BCPL’s word-based model was insufficient
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
1 Hello World in C Hello World in C Program mostly a
collection of functions ¢¡¤£¦¥¨§¨© ¢ ¡¦¦ ¢¡¤£¦¥¨§¨© ¢ ¡¦¦ Preprocessor used to share information “main” function special:
among source files the entry point
¤¡ ¤¨¡£¢ ¤¡ ¤¨¡£¢
- Clumsy “void” qualifier indicates
function does not return
¡£¦ #$¦%& ¨§¤§¦')( §¨ +*-,.£¤/¦10 ¡£¦ #$¦%& ¨§¤§¦')( §¨ +*-,.£¤/¦10
" !¢" "
!¢" + Cheaply implemented anything 2 2 + Very flexible
I/O performed by a library function: not included in the language
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
Euclid’s algorithm in C Euclid’s algorithm in C ¡3£4¤¥ 5 ¡£¦67'8¡¤£9£¦
¡3£4¤¥ 5 ¡£¦67'8¡¤£9£¦ “New Style” function
declaration lists
number and type of Automatic variable
¡¤£¦ 0 ¡¤£¦ 0 "
" arguments
Storage allocated on
(5¡¨§ :; 6>:£¦?* (5¡¨§ :; 6>:£¦?*
<;@ "=< <;@
"=< Originally only listed stack when function
£A0 £A0 <
< return type. entered, released
£ 0 £ 0 <:"
<:" Generated code did when it returns. 2 2 not care how many All parameters,
arguments were
¨¤© £;£A0 ¨¤© £;£A0
" " " " Excess automatic variables
actually passed. arguments 2 2 accessed w.r.t. simply frame pointer. Arguments are call- n by-value ignored m Extra storage Frame ret. addr. needed while pointer Stack r evaluating large pointer expressions also placed on the stack
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
Euclid’s algorithm in C Euclid’s algorithm in C
¡3£4¤¥ 5 ¡£¦67'8¡¤£9£¦ ¡3£4¤¥ 5 ¡£¦67'8¡¤£9£¦
Expression: C’s
¡¤£¦ 0 ¡¤£¦ 0 "
" basic type of
(5¡¨§ :; 6>:£¦?* (5¡¨§ :; 6>:£¦?*
<;@ "=< <;@
"=< statement.
£A0 £A0 <
< Arithmetic and
£ 0 £ 0 <:"
<:" logical
2 2
Assignment (=) High-level control-flow
¨¤© £;£A0 ¨¤© £;£A0
" " "
" returns a value, so Each function statement. Ultimately 2 2 can be used in returns a single becomes a conditional expressions value, usually an branch. integer. Returned through a specific Supports “structured % is remainder register by programming” != is not equal convention.
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
2
Euclid Compiled on PDP-11 Euclid Compiled on PDP-11
¡£¢¥¤§¦¨¢ © ¡§ £ ¡£¢¥¤§¦¨¢ © ¡§
Very natural mapping from C into
! #" &%' )( *
$ $ §§
§§ PDP-11 instructions.
©£¡§ ,+ ©£¡§ ,+
- -
¤ 1* .*! &
.*! & Complex addressing modes make
/¥0£ /0£1 23£465 /¥7£12 §4¥§ /¥0£
89 89
¤ ¥*>' ? &?3@ + ¤ ¥*>' ?
+ frame-pointer-relative accesses
0;:=< 4 7 0;:=<
7 int gcd(int m, int n)
¡ &
& easy.
46¥§ £4 ¥§
¥§ {
% ¥*>' & @C&, &&? % ¥*>' &
< 71AB4 ¥0 <
¥0 int r; Another idiosyncrasy: registers
¤ ?' D¥? ¥*> E@ ¤ ?' D¥? ¥*>
0 < 76FB4 7 0 <
7 while ( (r = m % n) != 0) { were memory-mapped, so taking
- - 8§H
8§H m = n; G G address of a variable in a register
n = r;
¤ % ¥*>' ¥*> @ ¤ % ¥*>' ¥*>
0 < :=< 7 4 7 0 < :=< 7 is straightforward.
}
¤ ? ¥*>, I% ¥*> @J ¤ ? ¥*>, I% ¥*>
0 < < 4 7 0 < <
7 return n;
- -
8¥9 8¥9 ¦K
¦K }
8 H 8 H
+ ¤ % ¥*>' & + ¤ % ¥*>' &
7 0 < ¨L 4146¨4 7 0 <
- -
8 8
¦K ? ¦K ?
- -
8 8
?'+ § ¤M N ?'+ §
7¨2 4 3§212 §L 4 7¨2 4
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
Pieces of C C Types
Types and Variables Basic types: char, int, float, and double
• Definitions of data in memory Meant to match the processor’s native types
Expressions • Natural translation into assembly • Arithmetic, logical, and assignment operators in an • Fundamentally nonportable infix notation Declaration syntax: string of specifiers followed by a Statements declarator
• Sequences of conditional, iteration, and branching
instructions Declarator’s notation matches that in an expression
Functions Access a symbol using its declarator and get the basic type back • Groups of statements and variables invoked recursively
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
C Type Examples C Typedef
¡3£;¡ 0
Integer Type declarations recursive, complicated.
¡3£PO!Q+'CRA0
j: pointer to integer, int k Name new types with typedef
©3£ ¦¡ 43£¦ ¥ O¨¥¤10
" ch: pointer to unsigned char
#§¦¨¨;#TS U 0
@'V
Array of 10 floats Instead of
¥3 £¦ MW¤!X¢ ¡£'8¥¤ O¨ 0 ¡3£:¨O #¨©£¥=] . ¡
" " "
2-arg function
¡3£YS§Z S¨[ SU 0
V @\V
V Array of three arrays of five … use
¡3£PO #¤©¤£¥'U¢ #¨§ ¨¨¨+0
!^ ¨ ¨ ¨#;¡£¦ #©¤£¥!]¨5 3¡ ¨ 0 !
function returning int *
¡3£:¨O #¨©£¥=] . ¡ 10 #©¤£¦¥=] PO #¤©£¦¥!]+0 pointer to function returning int
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
3
C Structures Struct bit-fields
A struct is an object with named fields: Way to aggressively pack data in memory
©¨¥ ©¨¥
" "
¡
¥¤¦ O£¦ ¨ 0 ©¤£¨¡ 4£¦ ¨ ;¡¤£ © ¤£ [50
"
¡¤£¦ W+'^70 ©¤£¨¡ 4£¦ ¨ ;¡¤£ ¨¡ ]¥£ U 0
¡¤£¦ ')( 0 ©¤£¨¡ 4£¦ ¨ ;¡¤£;©¤ §¦¢ MW £¦¤§¨¦5¥ §¤¥,R©£ UA0
"
2¢¡ 2
=W0 #¨§ ¨43 0
Compiler will pack these fields into words Accessed using “dot” notation:
Very implementation dependent: no guarantees of
¡
=W+W [ 0
< ordering, packing, etc.
¡
=W+ ^ ]+0 < Usually less efficient • Reading a field requires masking and shifting
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
C Unions Alignment of data in structs
Can store objects of different types at different times Most processors require n-byte objects to be in
memory at address n*k
©3£¦¡¦¢£
Side effect of wide memory busses
¡¤£ ¡ ¤§ 0
#¨§¦¤¨# ¤§10
E.g., a 32-bit memory bus
¥¤ O¤ ¤§10
"
Read from address 3 requires two accesses, shifting
2 0 1
Useful for arrays of dissimilar objects 4 3 2
Potentially very dangerous
Good example of C’s philosophy 4 3 2 1 • Provide powerful mechanisms that can be abused
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
Linker-visible. Alignment of data in structs C Storage Classes Allocated at fixed
location
¡
¢¡¤£¦¥¨§¨© ¢ § ¡ Compilers add “padding” to structs to ensure proper Visible within file.
alignment, especially for arrays Allocated at fixed
¡
¤§§¦5¤ ¤¡ ¥+0
¡3£4¤§ location.
¦5 ¨¨¡¤¥10 Pad to ensure alignment of largest object (with ¤¡¨¥¡£¦#¨¡¤§ biggest requirement)
a
¤¡ ;# ¡£;¨© ¦ ¤
! "
©¨¥
" b b b b
¥¤¦ +0
" c
¦5¦ ¤¨¡ ¥ 0
¨¨¤¡ ¥;¡£;#¨©£¦¥ Visible within func.
¡
¡¤£¦ 0
¦¢¡1'8¤©¨ ¦¢TS§U 0
Pad ¡¤£¦;¨© Allocated at fixed @\V
¥¤¦ ¥ 0
" location.
¡ ¡
© § O ¨©¨ ¦¢ ¨§ §¦¥5¦¡ ¨# © § ¤=O [ 10 <
2 a 2 b b b b c
Moral: rearrange to save memory
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
4
C Storage Classes malloc() and free()
¡
¢¡¤£¦¥¨§¨© ¢ § ¡
Library routines for managing the heap
¡
¡3£4¤§ ¤§§¦5¤ ¤¡ ¥+0
¦5 ¨¨¡¤¥10 ¤¡¨¥¡£¦#¨¡¤§ int *a;
a = (int *) malloc(sizeof(int) * k);
¡£;¨© ¦ ¤
¤¡ ;# Space allocated on
! "
a[5] = 3; stack by caller.
free(a);
¦5¦ ¤¨¡ ¥ 0
¨¨¤¡ ¥;¡£;#¨©£¦¥ Space allocated on
¡¤£¦;¨© ¦¢¡1'8¤©¨ ¦¢TS§U 0
@\V
stack by function.
¡ ¡
§ O ¨©¨ ¦¢ ¨§ §¦¥5¦¡ ¨# © § ¤=O [ 10
© Allocate and free arbitrary-sized chunks of memory <
2 in any order
Space allocated on heap by library routine.
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
malloc() and free() malloc() and free()
More flexible than automatic variables (stacked) Memory usage errors so pervasive, entire successful
company (Pure Software) founded to sell tool to track More costly in time and space them down • malloc() and free() use complicated non-constant-time algorithms Purify tool inserts code that verifies each memory • Each block generally consumes two additional words access
of memory Pointer to next empty block Reports accesses of uninitialized memory,
Size of this block unallocated memory, etc.
Common source of errors Publicly-available Electric Fence tool does something • Using uninitialized memory similar • Using freed memory • Not allocating enough • Neglecting to free disused blocks (memory leaks)
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
Dynamic Storage Allocation Dynamic Storage Allocation
What are malloc() and free() actually doing? Rules:
Pool of memory segments: • Each segment contiguous in memory (no holes) • Segments do not move once allocated
Free malloc() • Find memory area large enough for segment • Mark that memory is allocated malloc( )
free() • Mark the segment as unallocated
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
5
Dynamic Storage Allocation Simple Dynamic Storage Allocation
Three issues: Three issues:
How to maintain information about free memory How to maintain information about free memory
• Linked list
The algorithm for locating a suitable block The algorithm for locating a suitable block
• First-fit
The algorithm for freeing an allocated block The algorithm for freeing an allocated block • Coalesce adjacent free blocks
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
Simple Dynamic Storage Allocation Simple Dynamic Storage Allocation
First large-enough Appropriate Next Next free block selected position in free list identified Size Size Size Free block divided free(a) Free block Allocated block into two Newly-freed region added to adjacent malloc( ) Previous next free regions pointer updated Newly-allocated region begins with a size value
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
Dynamic Storage Allocation Memory Pools
Many, many variants An alternative: Memory pools
Other “fit” algorithms Separate management policy for each pool
Segregation of objects by sizes
• 8-byte objects in one region, 16 in another, etc. Stack-based pool: can only free whole pool at once
More intelligent list structures • Very cheap operation • Good for build-once data structures (e.g., compilers)
Pool for objects of a single size • Useful in object-oriented programs
Not part of the C standard library
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
6
Arrays Multidimensional Arrays
Array: sequence of identical Array declarations read right-to-left objects in memory
Filippo Brunelleschi,
SU 0
¡£¦ int a[10][3][2];
@\V Ospdale degli Innocenti, means space for ten Firenze, Italy, 1421
integers “an array of ten arrays of three arrays of two ints”
By itself, is the address of the first integer In memory
O S
@'V
and mean the same thing 3 3 3 The address of is not stored in memory: the compiler inserts code to compute it when it appears ...
Ritchie calls this interpretation the biggest 2 2 2 2 2 2 2 2 2 conceptual jump from BCPL to C 10
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved Seagram Building, Ludwig Mies van der Rohe,1957
Multidimensional Arrays Multidimensional Arrays
Passing a multidimensional array as an argument Use arrays of pointers for variable-sized requires all but the first dimension multidimensional arrays
You need to allocate space for and initialize the
¡3£YS U S§Z S] 0
'V V V
@ arrays of pointers
¡3£PO=O=O+0
2
¤¡ ; !W¨ ¡£ 5=YS S Z S]
V V V
YS§Z S£[ S£¢ O.£O.¨O5 Z [¦ ¢
V V V
¡ ¡ ¡
expands to
YS ¡ SQ S R
V V V int ***a Address for an access such as is
The value
R ]=O5 Q Z O¤¡
¡ ¡ ¡
int ** int * int Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
C Expressions C Expression Classes
O§¦ >
¡¥¤
Traditional mathematical expressions arithmetic:
* ;
<¤< < < <
comparison:
y = a*x*x + b*x + c; bitwise logical: ¨ © ;
shifting:
*
¨¨ ©© lazy logical:
Very rich set of expressions
£
conditional:
Able to deal with arithmetic and bit manipulation
< < < ¡
assignment:
¡¡
increment/decrement: '
sequencing:
O S
V
pointer: ¨
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
7
Bitwise operators Lazy Logical Operators
and: & or: | xor: ^ not: ~ left shift: << right shift: >> “Short circuit” tests save time
Useful for bit-field manipulations if ( a == 3 && b == 4 && c == 5 ) { … } #define MASK 0x040 equivalent to if (a & MASK) { … } /* Check bits */ if (a == 3) { if (b ==4) { if (c == 5) { … } } } c |= MASK; /* Set bits */ c &= ~MASK; /* Clear bits */ Evaluation order (left before right) provides safety d = (a & MASK) >> 4; /* Select field */ if ( i <= SIZE && a[i] == 0 ) { … }
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
Conditional Operator Side-effects in expressions
c = a < b ? a + 1 : b – 1; Evaluating an expression often has side-effects
a++ increment a afterwards
Evaluate first expression. If true, evaluate second, a = 5 changes the value of a otherwise evaluate third. a = foo() function foo may have side-effects
Puts almost statement-like behavior in expressions.
BCPL allowed code in an expression: a := 5 + valof{ int i, s = 0; for (i = 0 ; i < 10 ; i++) s += a[I]; return s; }
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
Pointer Arithmetic C Statements
From BCPL’s view of the world Expression
Pointer arithmetic is natural: everything’s an integer Conditional
PO ' O¡ 0 ¡3£ •
! if (expr) { … } else {…}
• switch (expr) { case c1: case c2: … }
O$ [ S§[
! ! V ¡
equivalent to Iteration
! ¤ If p and q point into same array, is number of • while (expr) { … } zero or more iterations elements between p and q. • do … while (expr) at least one iteration
• for ( init ; valid ; next ) { … }
Accessing fields of a pointed-to structure has a shorthand: Jump
• goto label
¨#¤¡ ¤§ §O 1 #¨¡¨ ¨§
! ! means • continue; go to start of loop • break; exit loop or switch • return expr; return from function
Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved
8
The Switch Statement Switch Generates Interesting Code 5
Performs multi-way branches Sparse case labels tested sequentially
¢¤£¦¥¨§ ©¤ ¥¦
6!798 :<;¦;>= ?@!A!B¦A9C =ED
(¢¡¤¥ !W
¢¤£¦¥¨§§¢¤¤
!¢"
:FHG:¡6¦7I8 :$;¦;>=JH?<@¦A!B!AKCLMD
¥¨ U £
© © ¢¤£!¥"§¤§$#%¢¤#
© © ¢¤£!¥"§¤§ &¢¤ &
¡
:FHG:¡6¦7I8 :$;¦;>=J¦J4?$@ AB A>C!NOD
¨,R 0
"
*+
© ©'¢)(© ¢,
¥¨ [ £
¤.-0/
¥¨ ¡ £
*!3
5
¤¢¤1 2©
¡
¨,R 0
" Dense cases use a jump table
#4-¤
¤ # ¤©¨§ £
&,-0/
BHP!Q¦F¦:<;>RSC =OTUCLVT"W!:¦7HP!X¦F¦BYT'CZYT'C[]\ED
¡
¨,R 0
"
*!3
¤¢¤1 2©
6!798 :<^¦;>=]P_ `<: 2 ¤* + (© ¢,-0/ *!3 ¤¢¤1 2© 5 *¦3 - 1¦ 2© Clever compilers may combine these Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved setjmp/longjmp The Macro Preprocessor 5 A way to exit from deeply nested functions 5 Relatively late and awkward addition to the language 5 A hack now a formal part of the standard library Space for a return 5 6H_f!F!X¦`¦: e address and registers Symbolic constants } { | © ©~ g #% &¦#¤# h!iHjm.Q!X¦7 5 Macros with arguments for emulating inlining } }H { | AH6 `SB¦AHjo8 AH6 `?pR © ©'£ O U K- n n Stores context, returns 0 G%q.6¦BHfHl>8G:¦BHh ij.8h ij¦QHX 7!?!?KR 5 f P4G:$JsrtfHl6HF `,8 ?YD"Qgu¤:¦PvED Conditional compilation "¦ { | © (¤ f P4G:p=ErxwySFAg_@Hh ijSf P!F!F¦:!`¡ywpQou :¦P4vED 5 \S\ File inclusion for sharing of declarations + * Returns to context, making it } { | | ©2£!© ©H . appear setjmp() returned 1 AH6 `S`!:¦:Hj!Fzg_:gGB!:¦`O8 ?pRFAg_@!h i4j.8 h i4j¦Q¦X¦7YT= ?MD\ n Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved Macro Preprocessor Pitfalls Macro Preprocessor Pitfalls 5 5 Header file dependencies usually form a directed Macros with arguments do not have function call acyclic graph (DAG) semantics 5 How do you avoid defining things twice? 5 Function Call: 5 Convention: surround each header (.h) file with a • Each argument evaluated once, in undefined order, before function is called conditional: 6 7_`!:!7m!m4.2gW4!%mg¤mHm e 5 Macro: `¦:¦7H6_:m!m4.2gW4!%mg¤mHm e • Each argument evaluated once every time it appears in wgyW!:Hf!F PouP!B!6A._¦Gy w expansion text :_`H6¦7 e Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved 9 Macro Preprocessor pitfalls Macro Preprocessor Pitfalls > 5 Example: the “min” function Text substitution can expose unexpected groupings ¢¡¤£¦¥¢ ¢¡¨§© ¡¤£¦ ¢¡¤£ © ! #"$&%' ¤() *"¢+¤,&"- ."$%¨ /(0*21 ? @BABCED0F'AHG0IKJMLON¢PRQ¤SBTUPEVBS "&¢(/")5 ¢(¨© 67& © 89 :© <;=9 & 3¢4 G IBJMLWNMX&Y[Z\Q¤]'YK^ET > X_Y`ZaVU]aYb^ Identical for min(5,x) > Expands to > Different when evaluating expression has side-effect: > Operator precedence evaluates this as min(a++,b) 5 + (3*2) + 4 = 15 not (5+3) * (2+4) = 48 as intended • min function increments a once > • min macro may increment a twice if a < b Moral: By convention, enclose each macro argument in parenthesis: ? @BABCED0F'AHG0IKJMLON¢PRQ¤SBTcN&PKTEV[N&SKT Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved Nondeterminism in C Nondeterminism in C > > Library routines Uninitialized variables • malloc() returns a nondeterministically-chosen address • Automatic variables may take values from stack • Address used as a hash key produces • Global variables left to the whims of the OS nondeterministic results > Reading the wrong value from a union > Argument evaluation order • union { int a; float b; } u; u.a = 10; printf(“%g”, u.b); • myfunc( func1(), func2(), func3() ) > Pointer dereference • func1, func2, and func3 may be called in any order • *a undefined unless it points within an allocated array > Word sizes and has been initialized int a; • Very easy to violate these rules a = 1 << 16; /* Might be zero */ • Legal: int a[10]; a[-1] = 3; a[10] = 2; a[11] = 5; a = 1 << 32; /* Might be zero */ • int *a, *b; a - b only defined if a and b point into the same array Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved Nondeterminism in C Summary > > How to deal with nondeterminism? C evolved from the typeless languages BCPL and B • Caveat programmer > Array-of-bytes model of memory permeates the > Studiously avoid nondeterministic constructs language • Compilers, lint, etc. don’t really help > Original weak type system strengthened over time > Philosophy of C: get out of the programmer’s way > C programs built from > “C treats you like a consenting adult” • Variable and type declarations • Created by a systems programmer (Ritchie) • Functions > • Statements “Pascal treats you like a misbehaving child” • Expressions • Created by an educator (Wirth) > “Ada treats you like a criminal” • Created by the Department of Defense Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved 10 Summary of C types Summary of C expressions > > Built from primitive types that match processor types Wide variety of operators > char, int, float, double, pointers • Arithmetic + - * / • Logical && || (lazy) > Struct and union aggregate heterogeneous objects • Bitwise & | > Arrays build sequences of identical objects • Comparison < <= > • Assignment = += *= Alignment restrictions ensured by compiler • Increment/decrement ++ -- > Multidimensional arrays • Conditional ? : > Three storage classes • global, static (address fixed at compile time) > Expressions may have side-effects • automatic (on stack) • heap (provided by malloc() and free() library calls) Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved Summary of C statements Summary of C > > Expression Preprocessor > Conditional • symbolic constants • inline-like functions • if-else switch • conditional compilation > Iteration • file inclusion • while do-while for(;;) > Branching > Sources of nondeterminsm • goto break continue return • library functions, evaluation order, variable sizes > Awkward setjmp, longjmp library routines for non- local goto Copyright © 2001 Stephen A. Edwards All rights reserved Copyright © 2001 Stephen A. Edwards All rights reserved The Main Points > Like a high-level assembly language > Array-of-cells model of memory > Very efficient code generation follows from close semantic match > Language lets you do just about everything > Very easy to make mistakes Copyright © 2001 Stephen A. Edwards All rights reserved 11