Prof. Stephen A. Edwards
Prof. Stephen A. Edwards
Edwards
The C Language
Currently, the most commonly-used language for
embedded systems High-level assembly Very portable: compilers exist for virtually every processor Easy-to-understand compilation Produces efficient code Fairly concise
C History
Developed between 1969 and 1973 along with Unix
Due mostly to Dennis Ritchie Designed for systems programming Operating systems Utility programs Compilers Filters Evolved from B, which evolved from BCPL
BCPL Typeless
operating system
new language, OS
C History
Many language features designed to reduce memory Forward declarations required for everything Designed to work in one pass: must know everything No function nesting
PDP-11 was byte-addressed Now standard Meant BCPLs word-based model was insufficient
Hello World in C
#include <stdio.h>
- Clumsy
+ Cheaply implemented
Hello World in C
#include <stdio.h>
Euclids algorithm in C
int gcd(int m, int n) Originally only listed { return type. int r; Generated code did while ( (r = m % n) != 0) {not care how many arguments were m = n; actually passed. n = r; Arguments are call} by-value return n; }
{All parameters,
automatic variables accessed w.r.t. frame pointer. Extra storage needed while evaluating large expressions also placed on the stack
Euclids algorithm in C
int gcd(int m, int n) { int r; while ( (r = m % n) m = n; Each function n = r; returns a single value, usually an } integer. Returned through a specific return n; register by }
convention.
!= 0) { control-flow High-level
statement. Ultimately becomes a conditional branch. Supports structured programming
L2 6(r5),r0 L1 rretrn
Very natural mapping from C into PDP-11 instructions. Complex addressing modes make frame-pointer-relative accesses easy. Another idiosyncrasy: registers were memory-mapped, so taking address of a variable in a register is straightforward.
Pieces of C
Types and Variables Definitions of data in memory Expressions Arithmetic, logical, and assignment operators in an infix notation Statements Sequences of conditional, iteration, and branching instructions Functions Groups of statements and variables invoked recursively
C Types
Basic types: char, int, float, and double
Meant to match the processors native types Natural translation into assembly Fundamentally nonportable Declaration syntax: string of specifiers followed by a
declarator Declarators notation matches that in an expression Access a symbol using its declarator and get the basic type back
C Type Examples
Integer int i; int *j, k; j: pointer to integer, int k unsigned char *ch; ch: pointer to unsigned char float f[10]; Array of 10 floats char nextChar(int, 2-arg function char*); Array of three arrays of five int a[3][5][10]; int *func1(float); function returning int * int (*func2)(void);pointer to function returning int
C Typedef
Type declarations recursive, complicated.
Name new types with typedef Instead of
C Structures
A struct is an object with named fields:
box.x = 5; box.y = 2;
Struct bit-fields
Way to aggressively pack data in memory
struct { unsigned int baud : 5; unsigned int div2 : 1; unsigned int use_external_clock : 1; } flags; Compiler will pack these fields into words Very implementation dependent: no guarantees of ordering, packing, etc. Usually less efficient
Reading a field requires masking and shifting
C Unions
Can store objects of different types at different times
memory at address n*k Side effect of wide memory busses E.g., a 32-bit memory bus 1 Read from address 3 requires two accesses, shifting
4 3 2
alignment, especially for arrays Pad to ensure alignment of largest object (with biggest a requirement)
b b b
b c
Pad a b c
C Storage Classes
#include <stdlib.h>
int global_static; static int file_static; void foo(int auto_param) { static int func_static; int auto_i, auto_a[10]; double *auto_d = malloc(sizeof(double)*5); }
C Storage Classes
#include <stdlib.h>
int global_static; static int file_static;
Space allocated on stack by caller.
Space allocated on void foo(int auto_param) stack by function. { static int func_static; int auto_i, auto_a[10]; double *auto_d = Space allocated on malloc(sizeof(double)*5); by library routine. heap }
any order
Common source of errors Using uninitialized memory Using freed memory Not allocating enough Neglecting to free disused blocks (memory leaks)
company (Pure Software) founded to sell tool to track them down Purify tool inserts code that verifies each memory access Reports accesses of uninitialized memory, unallocated memory, etc. Publicly-available Electric Fence tool does something similar
malloc(
Memory Pools
An alternative: Memory pools Separate management policy for each pool
Stack-based pool: can only free whole pool at once 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
Arrays in memory
integers
Array: sequence of identical objects int a[10]; means space for ten
Filippo Brunelleschi, Ospdale degli Innocenti, Firenze, Italy, 1421
By itself, a is the address of the first integer *a and a[0] mean the same thing
The address of a is not stored in memory: the compiler inserts code to compute it when it appears
Ritchie calls this interpretation the biggest conceptual jump from BCPL to C
Multidimensional Arrays
Array declarations read right-to-left
int a[10][3][2]; an array of ten arrays of three arrays of two ints
3 In memory 3 ... 2 2 2 2 2 2 10
Seagram Building, Ludwig Mies van der Rohe,1957
2 2 2
Multidimensional Arrays
Passing a multidimensional array as an argument
requires all but the first dimension int a[10][3][2]; void examine( a[][3][2] ) { }
Address for an access such as a[i][j][k] is
a + k + 2*(j + 3*i)
Multidimensional Arrays
Use arrays of pointers for variable-sized
multidimensional arrays You need to allocate space for and initialize the arrays of pointers int ***a; int ***a a[3][5][4] expands to *(*(*(a+3)+5)+4)
The value
int **
int *
int
C Expressions
Traditional mathematical expressions
y = a*x*x + b*x + c;
Very rich set of expressions Able to deal with arithmetic and bit manipulation
C Expression Classes
arithmetic: + * / % comparison: == != < <= > >= bitwise logical: & | ^ ~ shifting: << >> lazy logical: && || ! conditional: ? : assignment: = += -= increment/decrement: ++ -sequencing: , pointer: * -> & []
Bitwise operators
and: & or: | xor: ^ not: ~ left shift: << right shift: >>
Useful for bit-field manipulations
#define MASK 0x040 if (a & MASK) { } c |= MASK; c &= ~MASK; d = (a & MASK) >> 4;
Conditional Operator
c = a < b ? a + 1 : b 1;
Evaluate first expression. If true, evaluate second,
Side-effects in expressions
Evaluating an expression often has side-effects
increment a afterwards changes the value of a function foo may have side-effects
Pointer Arithmetic
From BCPLs view of the world
Pointer arithmetic is natural: everythings an integer
int *p, *q; *(p+5) equivalent to p[5] If p and q point into same array, p q is number of elements between p and q. Accessing fields of a pointed-to structure has a shorthand: p->field means (*p).field
C Statements
Expression Conditional if (expr) { } else {} switch (expr) { case c1: case c2: } Iteration while (expr) { } zero or more iterations do while (expr) at least one iteration for ( init ; valid ; next ) { } Jump goto label continue; go to start of loop break; exit loop or switch return expr; return from function
if (e == 1) goto L1; else if (e == 10) goto L2; else if (e == 100) goto L3;
Dense cases use a jump table
table = { L1, L2, Default, L4, L5 }; if (e >= 1 and e <= 5) goto table[e];
Clever compilers may combine these
setjmp/longjmp
A way to exit from deeply nested Space for a return functions A hack now a formal part of the standard and registers address library (including stack pointer, frame pointer) #include <setjmp.h>
jmp_buf jmpbuf;
void top(void) { switch (setjmp(jmpbuf)) { case 0: child(); break; case 1: /* longjmp called */ break; Returns to context, making it } } appear setjmp() returned 1 void deeplynested() { longjmp(jmpbuf, 1); }
semantics
Function Call: Each argument evaluated once, in undefined order, before function is called
Macro: Each argument evaluated once every time it appears in expansion text
Identical for min(5,x) Different when evaluating expression has side-effect: min(a++,b) min function increments a once min macro may increment a twice if a < b
#define mult(a,b) a*b mult(5+3,2+4) Expands to 5 + 3 * 2 + 4 Operator precedence evaluates this as 5 + (3*2) + 4 = 15 not (5+3) * (2+4) = 48 as intended Moral: By convention, enclose each macro argument in parenthesis: #define mult(a,b) (a)*(b)
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; a = 1 << 16; /* Might be zero */ a = 1 << 32; /* Might be zero */
Nondeterminism in C
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
Nondeterminism in C
How to deal with nondeterminism? Caveat programmer Studiously avoid nondeterministic constructs Compilers, lint, etc. dont 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
Summary
C evolved from the typeless languages BCPL and B
Array-of-bytes model of memory permeates the
language Original weak type system strengthened over time C programs built from
Variable and type declarations Functions
Statements
Expressions
Summary of C types
Built from primitive types that match processor types char, int, float, double, pointers Struct and union aggregate heterogeneous objects Arrays build sequences of identical objects Alignment restrictions ensured by compiler Multidimensional arrays Three storage classes
global, static (address fixed at compile time)
automatic (on stack) heap (provided by malloc() and free() library calls)
Summary of C expressions
Wide variety of operators Arithmetic + - * / Logical && || (lazy) Bitwise & | Comparison < <= Assignment = += *= Increment/decrement ++ - Conditional ? :
Expressions may have side-effects
Summary of C statements
Expression Conditional if-else switch Iteration while do-while for(;;) Branching goto break continue return
Awkward setjmp, longjmp library routines for non-
local goto
Summary of C
Preprocessor symbolic constants inline-like functions conditional compilation file inclusion
Sources of nondeterminsm library functions, evaluation order, variable sizes
semantic match
Language lets you do just about everything Very easy to make mistakes