DOC PREVIEW
UW CSE 303 - Function Pointers

This preview shows page 1-2-3-4 out of 12 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

'&$%CSE 303:Concepts and Tools for Software DevelopmentHal PerkinsAutumn 2008Lecture 13— C: post-overview, function pointersCSE303 Autumn 2008, Lecture 13 1'&$%Where are WeToday:• Top-down view of C• Function pointers (lite, if time. . . )Later:• Using function pointers more like objectsCSE303 Autumn 2008, 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 2008, 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 2008, 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 m uch 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 acces sCSE303 Autumn 2008, 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 s ame size as other pointers, but not everythingis a pointer.New side point: padding, alignment may mean structs are “biggerthan e xpected”CSE303 Autumn 2008, Lecture 13 6'&$%C is unsafeThe following is allowed to do anything to your program (delete files,launch viruses , silently t urn 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 checke d (no secret fields at run-time; all bits lookthe same)Often crashing is a “good thing” compared to continuing silently withmeaningless data.CSE303 Autumn 2008, 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 2008, 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 chec kExamples:• fopen may return N ULL– 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 succ ess and other v alues for errors (like in bash)– if(!someCall(&realAns,arg1,args)) ...CSE303 Autumn 2008, 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 2008, 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 fi elds).C separates the concepts of code, data, and pointers.CSE303 Autumn 2008, 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 ty pes, let’s pretend you alway s 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 2008, Lecture 13


View Full Document

UW CSE 303 - Function Pointers

Documents in this Course
Profiling

Profiling

11 pages

Profiling

Profiling

22 pages

Profiling

Profiling

11 pages

Testing

Testing

12 pages

Load more
Download Function Pointers
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Function Pointers and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Function Pointers 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?