DOC PREVIEW
UW CSE 303 - The C-Level of Abstraction

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

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

Unformatted text preview:

'&$%CSE 303:Concepts and Tools for Software DevelopmentDan GrossmanSpring 2007Lecture 7— Introduction to C: The C-Level of AbstractionDan Grossman CSE303 Spring 2007, Lecture 7 1'&$%Welcome to CCompared to Java, in rough order of importance• Lower level (less for compiler to do)• Unsafe (wrong programs might do anything)• Not “object-oriente d”• “Standard library” is much smaller.• Many similar control constructs (loops, ifs, ...)• Many syntactic similarities (operators, types, ...)A different world-view and much more to keep track of; Java-likethinking can get you in trouble.Dan Grossman CSE303 Spring 2007, Lecture 7 2'&$%Our planA semi-nontraditional way to learn C:• Learn how C programs actually run on machines like attu– Not promised by C’s definition– You do not need to “reason in terms of the implementation”when you follow the rules.– But it does help to know this model∗ To remember why C has the rules it does∗ To debug incorrect programs• Learn some C basics (including “Hello World!”)• Learn what C is (still) used for• Learn more about the language and good idioms• Later: A little C++Dan Grossman CSE303 Spring 2007, Lecture 7 3'&$%Address spaceSimple model of a running process (provided by the O/S):• There is one address space (an array of bytes)– Most common size today for a machine like attu is 232– We w ill “assume 32” for now, though you often shouldn’t– That is more RAM than you have (O/S maintains illusion; maylead to slowness )– “Subscripting” this array takes 32 bits– Something’s address is its position in this array.– Trying to read a not-used part of the array may cause a“segmentation fault” (immediate crash).• All data and code for the process are in this address space.– Code and data are bits; program “remembers” what is where.– O/S also lets you read/write file s, stdin, stdout, stderr.Dan Grossman CSE303 Spring 2007, Lecture 7 4'&$%Address-space layoutThe following is definitely different on different s yste ms, but it’s oneway to understand how C is implemented:code globals heap → ... ← stackSo in one array of 8-bit bytes we have:• Code instructions (typically immutable)• Space for global variables (mutable and immutable) (like Java’sstatic fields)• A heap for other data (like objects returned by Java’s new)• Unused portions; access causes “seg-fault”• A call-stack holding local variables and code addressesNote: Assuming an int occupies 4 bytesDan Grossman CSE303 Spring 2007, Lecture 7 5'&$%The stackThe call-stack (or just stack) has one “part” or “frame” (compilerfolks call it an activation record) for each function (cf. Java method)call that has not yet returned.It holds:• Room for local variables• The return address (index into code for what to execute after thefunction is done)Dan Grossman CSE303 Spring 2007, Lecture 7 6'&$%What could go wrong?Remember, the programmer has to keep the bits s traight even thoughC deals in terms of variables, functions, data structures, etc. (not bits).• If arr is an array of 10 elements, arr[30] accesses some otherthing.• Writing 8675309 where a return address should be makes afunction start executing stuff that may not be code.• ...Correct C programs can’t do these things, but nobody is perfect.On the plus side, there is no “unnecessary overhead” like keeping arraylengths around and checking them!Okay, time to see C...Dan Grossman CSE303 Spring 2007, Lecture 7 7'&$%Hello, World!#include<stdio.h>int main(int argc, char**argv) {fputs("Hello, World!\n",stdout);return 0;}• Compiling: gcc -o hi hello.c (maybe add -Wall)• Running: ./hiIntuitively: main gets called with the command-line args and theprogram exits when it returns.But there is a lot going on in terms of what the language constructsmean, what the compiler does, and what happens when the programruns.We w ill focus mostly on the language.Dan Grossman CSE303 Spring 2007, Lecture 7 8'&$%Quick Hello Explanation#include<stdio.h>int main(int argc, char**argv) {fputs("Hello, World!\n",stdout);return 0;}• #include finds the file stdio.h (from where?) and includes itsentire contents. (stdio.h describes fputs and stdout.)• A function definition is much like a Java method (return type,name, arguments with types, braces, body); it is not part of aclass and there are no built-in objects or this.• An int is like in Java, though its size depends on the compiler (itis 32 bits on attu).• main is a special function name; every full program has one.• char** is a long story...Dan Grossman CSE303 Spring 2007, Lecture 7 9'&$%PointersThink address, i.e., an index into the address-space array.If argv is a pointer, then *argv returns the pointed-to value.So does argv[0].And if argv points to an array of 2 values, then argv[1] returns thesecond one (and so does *(argv+1) but the + here is funny).People like to say “arrays and pointers are the s ame t hing in C”. thisis sloppy talking, but people say it anyway.Type syntax: t* describes either• NULL (seg-fault if you dereference it)• A pointer holding the address of some number of values of type t.How m any? You have to know somehow; no length primitive.Dan Grossman CSE303 Spring 2007, Lecture 7 10'&$%Pointers, continuedSo reading right to left: argv (of type char**) holds a pointer to(one or more) pointer(s) to (one or more) char(s).Fact #1 about main: argv holds a pointer to j pointers to (one ormore) char(s) w here argc holds j.Common idiom: array lengths as other arguments .Fact #2 about main: For 0 ≤ i ≤ j where argc holds j, argv[j] isan array of char(s) with last element equal to the character ’\0’(which is not ’0’).Very comm on idiom: pointers to char arrays ending with ’\0’ arecalled strings. The standard library and language often use this idiom.[Let’s draw a picture of “memory” when hi runs.]Dan Grossman CSE303 Spring 2007, Lecture 7 11'&$%Rest of the story#include<stdio.h>int main(int argc, char**argv) {fputs("Hello, World!\n",stdout);return 0;}• fputs is a function taking a string (a char*) and a FILE* (whereFILE is a type defined in stdio.h).• "Hello, World!\n" evaluates to a pointer to a global,immutable array of 15 characters (including a trailing ’\0’, \n isone character).• stdout is a global variables of type FILE* defined in stdio.h.How this ge ts hooked up to the screen (or somewhere e lse) is thelibrary’s (nontrivial)


View Full Document

UW CSE 303 - The C-Level of Abstraction

Documents in this Course
Profiling

Profiling

11 pages

Profiling

Profiling

22 pages

Profiling

Profiling

11 pages

Testing

Testing

12 pages

Load more
Download The C-Level of Abstraction
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 The C-Level of Abstraction 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 The C-Level of Abstraction 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?