DOC PREVIEW
UW CSE 303 - Lecture Notes

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

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

Unformatted text preview:

'&$%CSE 303:Concepts and Tools for Software DevelopmentHal PerkinsAutumn 2008Lecture 8— C: locals, left vs. right expressions, dangling pointers, ...CSE303 Autumn 2008, Lecture 8 1'&$%Where are We• The low-leve l exe cution 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– File structure; storage duration and scope– Left vs. right expressions; more pointers– Dangling pointers– Stack arrays and implicit pointers (confusing)Next time: structs; t he heap and manual memory management.CSE303 Autumn 2008, Lecture 8 2'&$%Control constructs• while, if, for, break, continue, switch all much like Java.• Key difference: No built-in boolean type; use ints (or pointers)– Anything but 0 (or NULL) is true.– 0 and NULL are false.– C99 did add a bool library but use is still sporadic/optional• goto much maligned, but make s s ense for some tasks (moregeneral than Java’s labeled break).• Gotcha: switch cases fall-through unless there is an explicittransfer (typically a break).• See sums.c; should be understandable on your own (with helpfrom the book, etc .)CSE303 Autumn 2008, Lecture 8 3'&$%Storage, lifetime, and scope• At run-time, eve ry variable needs space.– When is the space allocated and deallocated?• Every variable has scope.– Where can the variable be used (unless another variableshadows it)?C has several answers (with inconsistent reuse of the word static).Some answers rarely used but understanding storage, lifetime, andscope is important.Related: Allocating space is separate from initializing that space.• Use uninitialized bits? Hopefully crash but who knows.CSE303 Autumn 2008, Lecture 8 4'&$%Storage, lifetime, and scope• Global variables allocated before main, deallocated after main.Scope is entire program.– Usually bad style, kind of like public static Java fields.• Static global variables like global variables but scope is just thatfile, kind of like private static Java fields.– Related: static functions cannot be called from other files.• Static local variables like global variables (!) but scope is just thatfunction, rarely used.• Local variables allocated “when reached” deallocated “after thatblock”, scope is that block.– So with recursion, multiple spaces for s ame variable (one perstack frame).– Like local variables in Java.CSE303 Autumn 2008, Lecture 8 5'&$%A typical file layoutNo rules on this order, but good conventional style// includes for functions, types defined elsewhere (just prototypes)#include <stdio.h>#include ...// global variables (usually avoid them)int some_global;static int this_file_arr[7] = { 0, 2, 4, 5, 9, -4, 6 };// function prototypes for forward-references (to get around// uses-follow-definition rule)void some_later_fun(char, int); // argument names optional// functionsvoid f() { ... }void some_later_fun(char x, int y) {...}int main(int argc, char**argv) {...}CSE303 Autumn 2008, Lecture 8 6'&$%Some glitches• Silly almost-obsolete syntax restriction not in Java or C++:declarations only at the beginning of a “block” – but anystatement can be a block.– Just put in braces if you need to (see main in sums.c)– Or use --std=c99 compiler option (gcc)• (Local or global) variables holding arrays must have a constant size– So the compiler knows how much s pace to give .– (C99 has an extension to remove this limitation; rarely used.)– So for arrays whose size depends on run-time information,allocate them in the heap and point to them (next time)• 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)CSE303 Autumn 2008, Lecture 8 7'&$%Function arguments• Storage and scope of arguments is like for local variables.• But intialized by the caller (“copying” the value)• So assigning to an argument has no affect on the c aller.• But assigning to the space pointed-to by an argument might.void f() { int g(int x) {int i=17; x = x+1;int j=g(i); return x+1;printf("%d %d",i,j); }}CSE303 Autumn 2008, Lecture 8 8'&$%Left vs. rightWe have been fairly sloppy in 142, 143, and so far here about thedifference between the left 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 differenc e 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 location’scontents, and then we are done.• Most things do not make sense as left expressions.Note: This is true in Java too.CSE303 Autumn 2008, Lecture 8 9'&$%Function arguments• Storage and scope of arguments is like for local variables.• But intialized by the caller (“copying” the value)• So assigning to an argument has no affect on the c aller.• But assigning to the space pointed-to by an argument might.void f() { int g(int* p) {int i=17; (*p) = (*p) + 1;int j=g(&i); return (*p) + 1;printf("%d %d",i,j); }}CSE303 Autumn 2008, Lecture 8 10'&$%Function arguments• Storage and scope of arguments is like for local variables.• But intialized by the caller (“copying” the value)• So assigning to an argument has no affect on the c aller.• But assigning to the space pointed-to by an argument might.void f() { int g(int* p) {int i=17; int k = *p;int j=g(&i); int *q = &k;printf("%d %d",i,j); p = q;} (*p) = (*q) + 1;return (*q) + 1;}CSE303 Autumn 2008, Lecture 8 11'&$%Pointers to pointers to ...Any level of pointer makes sense:• Example: argv, *argv, **argv• Same example: argv, argv[0], argv[0][0]But &(&p) makes no s ense (&p is not a left-expression, the value is anaddress but the value is in no-particular-place). This makes sense:void f(int x) {int*p = &x;int**q = &p;... can use x, p, *p, q, *q, **q, ...}Note: When playing, you can print pointers with %p (just numbers inhexadecimal)CSE303 Autumn 2008, Lecture 8 12'&$%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; /* could CRASH but probably not */return p; /*


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?