DOC PREVIEW
UW CSE 303 - Post-overview, Function Pointers, Coding up Objects

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 DevelopmentDan GrossmanSpring 2005Lecture 13— C: post-overview, function pointers, coding up objectsDan Grossman CSE303 Spring 2005, Lecture 13 1'&$%Where are We“Official Notice”:• Homework 4 will be posted today, due a week from Thursday• Midterm Friday, “first page” will be posted today or tomorrow– Covers through today• Homework 2 will be available tomorrow (thanks Ben!)Today:• Top-down view of C• Function pointers• Coding up objects (more later?)Dan Grossman CSE303 Spring 2005, Lecture 13 2'&$%Top-down post-overviewNow that we have seen m ost of C, le t’s summ arize/organize:• Preprocessing– #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– Take .o and .a files and make a program– libc.a in by default, has printf, malloc, ...– More laterDan Grossman CSE303 Spring 2005, Lecture 13 3'&$%• Executing– O/S maintains the “big array” address-space illusion– Execution starts at main– Library manages the heap via malloc/free.Dan Grossman CSE303 Spring 2005, 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 sim ilar to in Java (+ goto, –exception-handling, ints for bools)– Local declarations have local scope.• Left-expressions (locations) and right-expressions (values,including pointers-to-locations)– * for pointer dereference, & for address-of, . for field acces sDan Grossman CSE303 Spring 2005, 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.Dan Grossman CSE303 Spring 2005, Lecture 13 6'&$%C is unsafeThe following is allowed to set your computer on fire:array-bounds violation (bad pointer arithmetic), dangling-pointerdereferences, dereferencing NULL, using results of wrong casts, usingcontents of uninitialized locations, linking errors (inconsistentassumptions), ...Casts are not chec ked (no secret fields at run-time; all bits look thesame)Dan Grossman CSE303 Spring 2005, Lecture 13 7'&$%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.Dan Grossman CSE303 Spring 2005, Lecture 13 8'&$%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]);}C separates the concepts of code, data, and pointers.Dan Grossman CSE303 Spring 2005, Lecture 13 9'&$%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 s lide: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.Dan Grossman CSE303 Spring 2005, Lecture 13 10'&$%Toward objectsIf you want a pointer to code and data, like in Java, then DIY:struct MyPoint {// dataint x;int y;// codeint (*getX)(struct MyPoint*);void (*setX)(struct MyPoint*,int);int (*getY)(struct MyPoint*);void (*setY)(struct MyPoint*,int);double (*distance2origin)(struct MyPoint*);};“extra argument” is Java’s this, e lse c ode has no access to the other(data and code) fields.When this “coding pattern” became common, C++ was born (sorta).Dan Grossman CSE303 Spring 2005, Lecture 13 11'&$%A much bigger storyWe have just s cratched the s urface of “C-level O OP”.Food for thought (not on the exam!):• How c ould a subclass override methods, add methods, or addfields? (you need casts in a couple different places!)• What is the difference between calling this->getX and callingMyPoint_getX?• Aren’t struct MyPoint objects awfully large – how could wesave space?Dan Grossman CSE303 Spring 2005, Lecture 13


View Full Document

UW CSE 303 - Post-overview, Function Pointers, Coding up Objects

Documents in this Course
Profiling

Profiling

11 pages

Profiling

Profiling

22 pages

Profiling

Profiling

11 pages

Testing

Testing

12 pages

Load more
Download Post-overview, Function Pointers, Coding up Objects
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 Post-overview, Function Pointers, Coding up Objects 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 Post-overview, Function Pointers, Coding up Objects 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?