Title: Future Notes
1Future Notes
- Ran about 58 minutes (bit long)
- Ended up skipping over array and foo, foo, foo
slide - spot look at feedback said pace was ok (maybe
slow) - Find more correct way to say C not return
aggregates - Mvanier points out it can in some cases
2CS1Introduction to Computation
- Day 19 December 2, 2002
- Interlude in C
3- On day 1
- We saw that we already knew most of the forms and
concepts needed to write Scheme code - We simply needed to
- Pick out the ones to use
- Be a bit more precise about how to specify a few
things
4Throughout the term
- Do arithmetic
- Work with types
- Numbers, booleans, pairs, procedures
- Perform conditional operations
- if, cond
- Write functions
- Call functions
- Build compound data types
- cons
- Change values
- set!, set-car!, set-cdr!
- Reason about the meaning of programs
- Build generic data types
- Message passing
- Manage program complexity
- abstraction
5Claim
- Now that you know all these things
- You know most of the concepts which arise in
computer languages - Mostly a matter of concept mapping and
transliteration to understand another language
6Today
- Transliteration to C
- Similar
- Rough analogs
- Two new concepts
- Static typing
- Explicit Memory Management
- Whats missing
7Simple Function
- int sum (int n)
-
- if (n0)
- return(0)
- else
- return(nsum(n-1))
- (define (sum-integers n)
-
- (if ( n 0)
- 0
- ( n (sum-integers
- (- n 1)))))
8Differences
- int sum (int n)
-
- if (n0)
- return(0)
- else
- return(nsum(n-1))
- (define (sum-integers n)
-
- (if ( n 0)
- 0
- ( n (sum-integers
- (- n 1))))
Variables are typed.
9Differences
- int sum (int n)
-
- if (n0)
- return(0)
- else
- return(nsum(n-1))
- (define (sum-integers n)
-
- (if ( n 0)
- 0
- ( n (sum-integers
- (- n 1))))
Infix vs. prefix notation for operations.
10Differences
- int sum (int n)
-
- if (n0)
- return(0)
- else
- return(nsum(n-1))
- (define (sum-integers n)
-
- (if ( n 0)
- 0
- ( n (sum-integers
- (- n 1))))
But prefix for function calls.
11Differences
- int sum (int n)
-
- if (n0)
- return(0)
- else
- return(nsum(n-1))
- (define (sum-integers n)
-
- (if ( n 0)
- 0
- ( n (sum-integers
- (- n 1))))
Return statement.
12Differences
- int sum (int n)
-
- if (n0)
- return(0)
- else
- return(nsum(n-1))
- (define (sum-integers n)
-
- (if ( n 0)
- 0
- ( n (sum-integers
- (- n 1))))
Braces and semicolons.
13Differences
- int sum (int n)
-
- if (n0)
- return(0)
- else
- return(nsum(n-1))
- (define (sum-integers n)
-
- (if ( n 0)
- 0
- ( n (sum-integers
- (- n 1)))))
All differences here are superficial!
14C Expressions
- Infix expressions
- Parentheses optional
- E.g.
- 23 ( 2 3)
- axx bx c ( ( a x x) ( b x) c)
- (ax b)x c ( ( ( ( a x) b) x) c)
- ((agt0) (alt10)) (and (gt a 0) (lt a 10))
15Static Typing
- Must declare types of values
- When introduced as procedure formals
- For procedure return values
- When introduce temporaries
- Types must match
- to get correct results
- (C compilers notoriously forgiving)
16Basic Integer Type
- int finite size word
- Size traditionally determined by primitive
machine word - Typically 32 bits
- Actually 31b 1 sign bit (positive vs. negative)
- Two types
- int
- unsigned int
17Finite Integers
- int arithmetic
- May wrap around
- Can have
- agt0, bgt0
- and get (ab) lt 0
- unsigned int arithmetic
- Can get (ab) lt a, (ab) lt b
- Remember 20013478 (8b ALU)
18Statements
- In addition to operations, have statements which
exist entirely for their side effects. - Statements end with a semi-colon.
- abc (set! a ( b c))
- dsum(n) (set! d (sum n))
- return(nsum(n-1)) ( n (sum (- n 1))
19Procedure Calls
- pname(arg1,arg2,arg3)
- Scheme (pname arg1 arg2 arg3 )
- C comma delimited list of arguments
- Are expressions
- f (xfoo(qrs))z
- compare (set! f ( ( x (foo ( q ( r s))) z)))
- Can be used as statements for their side-effects
- bar(x,y)
20Procedure Definition
- return-type pname(type1 arg1,
- type2 arg2, .)
-
- type varinitial-value
- type var2initial-value
-
- statement1
- statement2
-
21Sample Procedure Definition
- int aquad(int a, int b, int c, int x)
-
- int res0
- resresc
- resresbx
- resresaxx
- return(res)
-
(define (aquad a b c x) (let ((res 0))
(set! res ( res c)) (set! res ( res ( b
x))) (set! res ( res ( a x x)))
res)))
22Comments
- Traditionally C comments marks the beginning and
end - / this is a C comment /
- / comments can span
- multiple lines /
- Open with /
- Closed with /
- Can be almost anywhere
- ab / why are you using c here? / c
23C Comments
- C added comments similar to Scheme
- Open with //
- Comment extends to the end of the line
- // this is a comment
- abc // why are you using c here?
- Many C compilers will accept C comments these
days
24Other Types
- float single precision IEEE floating point (32b
total) - double double precision IEEE floating point
(64b total) - char traditionally, a signed byte used to
represent a character
25Statement if
- if is a statement in C
- Not an expression as it is in Scheme
- Does not return a value
- else is an explicit keyword
- if (cond-expression)
- if-clause
- else
- else-clause
26if Example
- int max (int a, int b)
-
- int res
- if (agtb)
- resa
- else
- resb
- return (res)
(define (max a b) (let ((res 0)) (if
(gt a b) (set! res a)
(set! res b)) res) )
27Statements
- Actually, anywhere C has a statement, it can
introduce a code block - not entirely unlike a Scheme let or begin
- Block
-
- variable declarations
- statement1
- statement2
-
28Block example
-
- int tmp1max(a,b)
- int tmp2min(a,b)
- c(c-tmp2)
- d(d-tmp2)
- ec/(tmp1-tmp2)
- fd/(tmp1-tmp2)
-
- (let (
- (tmp1 (max a b))
- (tmp2 (min a b)) )
- (set! c (- c tmp2))
- (set! d (- d tmp2))
- (set! e (/ c (- tmp1 tmp2)))
- (set! f (/ d (- tmp1 tmp2)))
- )
-
29Blocks often used with ifs
(if (gt a b) (begin (set! max a)
(set! min b) (begin (set! max b)
(set! min a) )
- if (agtb)
- maxa minb
- else
- maxb mina
30return Statement
- return can occur anywhere in a procedure
- return value should match type of functions
returns - once executed, nothing else in the function is
executed
31Example
- int scalebc(int a, int b, int c)
-
- if (b0) return(LARGE) // avoid divide by
0 - if (c0) return(LARGE) // similar
- return(a/(bc))
-
-
32Iteration for loop
- for loop for iteration
- Vaguely similar to scheme do loop
- Continue-test not an exit-test
- for (init-var continue-test update-var)
- statement
33for Example
- int sum2(n)
-
- int res0
- int i
- for (i0 iltn ii1)
-
- resresi
- return(res)
- (define (sum2 n)
- (let (
- (res 0))
- (do ((i 0 ( i 1)))
- ((gt i n) done)
- (set! res ( res i))
- )
- res))
34for Example
- int sum2(n)
-
- int res0
- int i
- for (i0 iltn ii1)
-
- resresi
- return(res)
- (define (sum2 n)
- (let (
- (res 0))
- (do ((i 0 ( i 1)))
- ((gt i n) done)
- (set! res ( res i))
- )
- res))
35for with block
- int sum0
- int zeros0
- for (int i0iltni) // i means ii1
-
- sumf(i) // means sumsumf(n)
- if (f(i)0) zeros
-
36Interlude
37Admin
- Course Feedback Questionnaires
- Next lecture
- Lecture videos
38Labs
- All rework ends Wed. at midnight
- Solutions for all labs posted Thursday
39Final Preparation
- Last years final now linked from homepage
- Will link in solutions on Thursday
- Final Review
- Wed. 10pm, JRG 74
- Final out
- Saturday Dec. 7th
- Due Friday Dec. 13th, noon
40Final Content
- List Processing
- Tagged Data and Message Passing
- Side-effects and Mutation
- Environment model
- Draw environment diagrams
- Reason about sameness
- Remember stuff from first half
- Recursion, lambda, complexity, evaluation, .
- Read/understand code
- Decompose/design operations
41Final Format
- Two tracks
- Two opportunities to demonstrate knowledge of
each concept - Grade for each concept
- Max of score on each track
- Welcome to work only one if confident
- Overall
- Min of concept grades
42History of C
43History of C (dmr)
- Platform PDP-7 w/ 8K 18b words
- BCPL 1967 MIT Project MAC
- Used // for comments
- B 1970 BCPL in 8K (C w/out types)
- Interpreted, not machine code
- Added shorthands ,
- 4K (18b word) cross compiler
- C 1972, PDP-11
- Types
- Byte addressable memory model
- Compiled to machine code
44dmr on C
- C is quirky, flawed, and an enormous success.
While accidents of history surely helped, it
evidently satisfied a need for a system
implementation language efficient enough to
displace assembly language, yet sufficiently
abstract and fluent to describe algorithms and
interactions in a wide variety of environments. - Dennis M. Ritchie (co-inventor of C)
- http//cm.bell-labs.com/cm/cs/who/dmr/chist.html
45Return
46Compound Data
- In Scheme we found data compounds to be a key
ingredient - cons, lists, vector
- In C
- Arrays like vectors, but homogeneous
- Structs used where we often use constructs of
cons
47Two differences
- Exposed machine model
- Explicit allocation (deallocation) of memory
48C Machine Model
- To properly understand C compounds need to
understand - C is a higher level assembly language
- The raw memory model is exposed to us
- Need to reason about the raw memory model in many
cases
49Exposed Machine Model
- In particular
- C doesnt really deal with compound objects
- It only deals with machine addresses
- Pointers to blocks of memory
- Just like the raw machine model
50C struct
- Way of grouping a number of machine words
together and giving that a name. - Example
- typedef struct
- int first // type and name of 1st
comp. - int second // type/name of 2nd
- intpair // name of composite type
- Way of grouping a number of machine words
together and giving that a name.
51Create a new Pair
- intpair new_pair(int nvalue1, int nvalue2)
-
- intpair pair(intpair )malloc(sizeof(intpair))
- // explicitly ask for the space
- pair-gtfirstnvalue1 // like set-car!
- pair-gtsecondnvalue2 // like set-cdr!
- return(pair)
52Create a new Pair
- intpair new_pair(int nvalue1, int nvalue2)
-
- intpair pair(intpair )malloc(sizeof(intpair))
- // explicitly ask for the space
- pair-gtfirstnvalue1 // like set-car!
- pair-gtsecondnvalue2 // like set-cdr!
- return(pair)
Says this is an address C can only return
machine words.
53Notice
- Remember
- All C can really return (pass as an argument) is
a single machine word - It can pass an int/float/char
- Or it can pass an address
- Here
- We return an address
- indicated by type (intpair )
- We must allocate the memory outside the frame
- So it will still be around when the procedure
exits
54Create a new Pair
- intpair new_pair(int nvalue1, int nvalue2)
-
- intpair pair(intpair )malloc(sizeof(intpair))
- // explicitly ask for the space
- pair-gtfirstnvalue1 // like set-car!
- pair-gtsecondnvalue2 // like set-cdr!
- return(pair)
malloc(n) memory allocation Says please give
me the address of a memory block of size n
55Create a new Pair
- intpair new_pair(int nvalue1, int nvalue2)
-
- intpair pair(intpair )malloc(sizeof(intpair))
- // explicitly ask for the space
- pair-gtfirstnvalue1 // like set-car!
- pair-gtsecondnvalue2 // like set-cdr!
- return(pair)
Type cast tells C to treat the resulting
address as an address of an intpair.
56Create a new Pair
- intpair new_pair(int nvalue1, int nvalue2)
-
- intpair pair(intpair )malloc(sizeof(intpair))
- // explicitly ask for the space
- pair-gtfirstnvalue1 // like set-car!
- pair-gtsecondnvalue2 // like set-cdr!
- return(pair)
Given that pair is an address -gt says give me
the subelement following the -gt operator.
57Recall
- typedef struct
- int first // type and name for 1st
- int second // type/name of 2nd
- intpair // name of composite type
58Create a new Pair
- intpair new_pair(int nvalue1, int nvalue2)
-
- intpair pair(intpair )malloc(sizeof(intpair))
- // explicitly ask for the space
- pair-gtfirstnvalue1 // like set-car!
- pair-gtsecondnvalue2 // like set-cdr!
- return(pair)
Given that pair is an address -gt says give me
the subelement following the -gt operator.
59Extract Value
- int first(intpair pair)
-
- return(pair-gtfirst)
-
60Use
- intpair minmax(int a, int b)
-
- if (agtb)
- return(new_pair(a,b))
- else
- return(new_pair(b,a))
-
(define (minmax a b) (if (gt a b)
(cons a b) (cons b a)))
61Explicit Deallocation
- C will never automatically reclaim memory
allocated from malloc - You must explicitly ask for it to be removed
- free
62free Example
- int diff(int a, int b)
-
- intpair orderedminmax(a,b)
- int resfirst(ordered)-second(ordered)
- free(ordered) // deallocate memory
- return(res)
-
free says the memory allocated at this
address can be reused.
63Big source of bugs
- Premature deallocation of memory is a large
source of bugs - Not deallocating memory causes the memory space
to dimension - May run out of memory if dont reclaim
- An extra detail (headache) for the programmer to
keep track of
64Integer List in C
- typedef struct int_cons_cell
-
- int car
- struct int_cons_cell cdr
- int_cons
- // see handout for full set of code
65Naming addresses and values (potentially tricky)
- More generally
- In declarations
- int foo // says foo is a pointer to an int.
- If foo is an address
- foo is the value of foo
- If foo is a value
- foo is the address of foo
66Dangers to Watch
- Lack of abstraction around data objects
- Exposure of raw memory
- Is one of the trickier parts of C to deal with
- Need to get a clear model of memory and foo,
foo to reason about - Lack of abstraction or safeguards
- Make it very error prone
- Potentially much more obscure bugs than in Scheme
67Arrays
- Heavily used in C
- Closest analog is vector in Scheme (exposure)
- Arrays can be of any single type.
- All data in the array must be the same type.
- Arrays dont know how long they are.
- C doesnt prevent you from running off the end of
them.
68Whats Missing?
- C does not have lambda
- Procedures can be passed in as arguments
- But
- Procedures do not have their own environments
(their own state) - Procedures cannot return new procedures
69Best of both (all) Worlds
- Foreign Function Interface(s)
- Allow us to mix
- Scheme, C, Assembly, FORTRAN, Java, .
- Recode performance critical kernels at low level
- Only after you know things work
- and know which ones are performance critical
- While keeping main program in high-level language
bigloo Scheme compiler designed to make this
easy.
70Big Ideas
- Most of what you know can be transliterated to
other languages. - Will have to learn
- new syntax
- new rules
- But you know how to think about the rules and
constructs.