'&$%CSE 303:Concepts and Tools for Software DevelopmentHal PerkinsAutumn 2007Lecture 13— C: post-overview, function pointers; MakefilesCSE303 Autumn 2007, Lecture 13 1'&$%Where are WeToday:• Top-down view of C• Function pointers• The make program (not C-specific)Later:• Using function pointers more like objectsCSE303 Autumn 2007, Lecture 13 2'&$%Top-down post-overviewNow that we have seen most of C, let’s summarize/organize:• Preprocessing (text replacement; common conventions)– #include for declarations defined elsewhere– #ifdef for conditional compilation– #define for token-based textual substitution• Compiling (type-checking and code-generating)– A sequence of declarations– Each C file becomes a .o file• Linking (more later)– Take .o and .a files and make a program– libc.a in by default, has printf, malloc, ...• Executing (next slide)CSE303 Autumn 2007, Lecture 13 3'&$%Execution• O/S maintains the “big array” address-space illusion• Execution starts at main• Each stack-frame has space for arguments, locals, andreturn-address (last one shouldn’t be visible to you)• Library manages the heap via malloc/freeCSE303 Autumn 2007, Lecture 13 4'&$%C, the language• A file is a sequence of declarations:– Global variables (t x; or t x = e;)– struct (and union and enum) definitions– Function prototypes (t f(t1,...,tn);)– Function definitions– typedefs• A function body is a statement– Statements are much like in Java (+ goto, –exception-handling, ints for bools, ...)– Local declarations have local scope (stack space).• Left-expressions (locations) and right-expressions (values,including pointers-to-locations)– * for pointer dereference, & for address-of, . for field accessCSE303 Autumn 2007, Lecture 13 5'&$%C language continued“Convenient” expression forms:• e->f means (*e).f• e1[e2] means *(e1 + e2)– But + for pointer arithmetic takes the size of the pointed toelement into account!– That is, if e1 has type t* and e2 has type int, then , then(e1 + c) == (((int)e1) + (sizeof(t) * c))– The compiler “does the sizeof for you” – don’t double-do it!“Size is exposed”: In Java, “(just about) everything is 32 bits”. In C,pointers are usually the same size as other pointers, but not everythingis a pointer.New side point: padding, alignment may mean structs are “biggerthan expected”CSE303 Autumn 2007, Lecture 13 6'&$%C is unsafeThe following is allowed to do anything to your program (delete files,launch viruses, silently turn a 3 into a 2, ...)array-bounds violation (bad pointer arithmetic), dangling-pointerdereferences (including double-frees), dereferencing NULL, usingresults of wrong casts, using contents of uninitialized locations, linkingerrors (inconsistent assumptions), ...Pointer casts are not checked (no secret fields at run-time; all bits lookthe same)Often crashing is a “good thing” compared to continuing silently withmeaningless data.CSE303 Autumn 2007, Lecture 13 7'&$%NowC is a pretty small language, but we still skipped lots of features.For now, one idiom (returning error codes) and one useful feature(function pointers).CSE303 Autumn 2007, Lecture 13 8'&$%Error codesWithout exceptions, how can a callee indicate it could not do its job?• Through the return value; caller must remember to checkExamples:• fopen may return NULL– f=fopen("someFile","r"); if(!f) ...• scanf returns number of matched arguments– cnt=scanf("%d:%d:%d",&h,&m,&s); if(cnt!=3) ...• Often assign “real results” through pointer-arguments and resultis 0 for success and other values for errors (like in bash)– if(!someCall(&realAns,arg1,args)) ...CSE303 Autumn 2007, Lecture 13 9'&$%Function pointers“Pointers to code” are almost as useful as “pointers to data”.(But the syntax is more painful.)(Somewhat silly) example:void app_arr(int len, int * arr, int (*f)(int)) {for(; len > 0; --len)arr[len-1] = (*f)(arr[len-1]);}int twoX(int i) { return 2*i; }int sq(int i) { return i*i; }void twoXarr(int len, int* arr) { app_arr(len,arr,&twoX); }void sq_arr(int len, int* arr) { app_arr(len,arr,&sq); }CSE 341 spends a week on why function pointers are so useful; todayis mostly just how in C.CSE303 Autumn 2007, Lecture 13 10'&$%Function pointers, cont’dKey computer-science idea: You can pass what code to execute as anargument, just like you pass what data to process as an argument.Java: An object is (a pointer to) code and data, so you’re doing bothall the time.// Javainterface I { int m(int i); }void f(int arr[], I obj) {for(int len=arr.length; len > 0; --len)arr[len-1] = obj.m(arr[len-1]);}The m method of an I can have access to data (in fields).C separates the concepts of code, data, and pointers.CSE303 Autumn 2007, Lecture 13 11'&$%C function-pointer syntaxC syntax: painful and confusing. Rough idea: The compiler “knows”what is code and what is a pointer to code, so you can write less thanwe did on the last slide:arr[len-1] = (*f)(arr[len-1]);→ arr[len-1] = f(arr[len-1]);app_arr(len,arr,&twoX);→ app_arr(len,arr,twoX);For types, let’s pretend you always have to write the “pointer to code”part (i.e., t0 (*)(t1,t2,...,tn)) and for declarations the variableor field name goes after the *.Sigh.CSE303 Autumn 2007, Lecture 13 12'&$%Onto toolsThe language-implementation (preprocessor, compiler, linker,standard-library) is hardly the only useful thing for developing software.The rest of the course:• Tools (recompilation managers, version control, debuggers,profilers)• Software-engineering issues• A taste of C++• Concurrency• Societal implicationsCSE303 Autumn 2007, Lecture 13 13'&$%makemake is a classic program for controlling what gets (re)compiled andhow. Many other such programs exist (e.g., ant, “projects” in IDEs,...)make has tons of fancy features, but only two basic ideas:1. Scripts for executing commands2. Dependencies for avoiding unnecessary workTo avoid “just teaching make features” (boring and narrow), let’sfocus more on the concepts...CSE303 Autumn 2007, Lecture 13 14'&$%Build scriptingProgrammers spend a lot of time “building” (creating programs fromsource code)• Programs they write• Programs other people writeProgrammers automate repetitive tasks. Trivial example:gcc -Wall -g -o myprog foo.c bar.c baz.cIf you:• Retype this every time: “shame, shame”• Use up-arrow or history: “shame” (retype after logout)• Have an alias or bash script:
View Full Document