DOC PREVIEW
UW CSE 303 - Lecture Notes

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

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 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 10 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 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

'&$%CSE 303:Concepts and Tools for Software DevelopmentDan GrossmanSpring 2005Lecture 8— C: locals, left vs. right expressions, dangling pointers, ...Dan Grossman CSE303 Spring 2005, Lecture 8 1'&$%Where are We• The low-level execution model of a process (one address space)• Basics of C:– Language features: functions, pointers, arrays– Idioms: Array-lengths, ’\0’ terminators• Today, more features:– Control constructs and int guards– Local declarations– Left vs. right expressions– Stack arrays and implicit pointers (confusing)∗ dangling pointers– structs vs. pointers to structs.Next time: The heap and manual memory management.Dan Grossman CSE303 Spring 2005, Lecture 8 2'&$%Control constructs• while, if, for, break, continue, switch all much like Java.• Key difference: No built-in bool type.– Anything but 0 (or NULL) is true.– 0 and NULL are false.• goto much maligned, but makes se nse for some tasks (moregeneral than Java’s labeled break).Dan Grossman CSE303 Spring 2005, Lecture 8 3'&$%Local declarations• Silly syntax restriction not in Java or C++: declarations only atthe beginning of a “block” – but any statement can be a block.– Just means put in braces if you need to (see main in sums.c)– Difference between s imilar notions: scope and lifetime– If you “goto into scope”, YPMSTCOFa• You can also allocate arrays on the stack, but:– Size must be a constant expression– Array types as function arguments don’t mean arrays (!)– Referring to an array doesn’t mean what you think it does (!)∗ “implicit array promotion” (come back to this)aYour Program Might Set The Computer On Fire.Dan Grossman CSE303 Spring 2005, Lecture 8 4'&$%Left vs. rightWe have been fairly sloppy in 142, 143, and so far here about thedifference between the le ft side of an assignment and the right. To“really get” C, it helps to get this straight:• Law #1: Left expressions get evaluated to locations (addresses)• Law #2: Right expressions get evaluated to values• Law #3: Values include numbers and pointers (addresses)The key difference is the “rule” for variables:• As a left express ion, a variable is a location and we are done• As a right expression, a variable gets evaluated to its locationscontents, and then we are done.• Most things do not make s ense as left expressions.Note: This is true in Java too.Dan Grossman CSE303 Spring 2005, Lecture 8 5'&$%The address-of and dereference operatorsvoid f() {int x;int y;int *p;int *q;x = 3;y = x+1;p = &x;q = p;q = &y;*q = *p;q = 0; /* i.e., NULL */*q = 4; /* YPMSTCOF */}Dan Grossman CSE303 Spring 2005, Lecture 8 6'&$%Dangling Pointersint* f(int x) {int *p;if(x) {int y = 3;p = &y; /* ok */} /* ok, but p now dangling *//* y = 4 does not compile */*p = 7; /* YPMSTCOF, but probably not */return p; /* uh-oh */}void g(int *p) { *p = 123; }void h() {g(f(7)); /* YPMSTCOF, and likely a problem */}Dan Grossman CSE303 Spring 2005, Lecture 8 7'&$%Stack Arrays RevisitedA very confusing thing about C: “implicit array promotion (inright-expressions”void f1(int* p) { *p = 5; }int* f2() {int x[3];x[0] = 5;/* (&x)[0] = 5; wrong */*x = 5;*(x+0) = 5;f1(x);/* f1(&x); wrong *//* x = &x[2]; wrong */int *p = &x[2];}Dan Grossman CSE303 Spring 2005, Lecture 8 8'&$%More gotchasDeclarations in C are funky:• You can put multiple declarations on one line, e.g., int x, y; orint x=0, y; or int x, y=0;, ...• But int *x, y; means int *x; int y; — you usually meanint *x, *y;No forward references:• A function must be defined and/or dec lared before it is used.(Lying: “implicit declaration” warnings, return type assumed to beint, ...)• You get a linker error if something is declared but never defined(or main is not defined).• You can still write mutually recursive functions, you just nee d adeclaration.Dan Grossman CSE303 Spring 2005, Lecture 8 9'&$%StructsA struct is a record.A pointer to a struct is like a Java object w ith no methods.x.f is for fie ld acces s.(*x).f in C is like x.f in Java.x->f is an abbreviation for (*x).f.There is a huge difference between passing a struct and passing apointer to a struct.Again, left-expressions evaluate to locations (which can be wholestruct locations or just field locations).Again, right-expressions evaluate to values (which can be whole structsor just fields).Dan Grossman CSE303 Spring 2005, Lecture 8


View Full Document

UW CSE 303 - Lecture Notes

Documents in this Course
Profiling

Profiling

11 pages

Profiling

Profiling

22 pages

Profiling

Profiling

11 pages

Testing

Testing

12 pages

Load more
Download Lecture Notes
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 Lecture Notes 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 Lecture Notes 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?