DOC PREVIEW
UW CSE 303 - Lecture Notes

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

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

Unformatted text preview:

'&$%CSE 303:Concepts and Tools for Software DevelopmentHal PerkinsAutumn 2007Lecture 7— Introduction to C: The C-Level of AbstractionCSE303 Autumn 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.CSE303 Autumn 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++CSE303 Autumn 2007, Lecture 7 3'&$%Some ReferencesThere’s a lot on the we b, but here are some primary sources.• Programming in C, Kochan. CSE303 textbook and a decentintroduction to C programming with examples, in spite of quirkslike poor commenting style and some other det ails.• C: A Reference Manual, Harbison & Steele (c urrently 5th ed.).Probably the best current reference on C and its libraries, updatedwith information about recent versions of the C standard.• The C Programming Language, Kernighan & Ritchie. “K&R” is aclassic, one that every programmer has on their shelves (orshould). A bit dated now, but the primary source.• Essential C, Stanford CS lib,http://cslibrary.stanford.edu/101/EssentialC.pdf.Great short introduction to the language by some friends atStanford.CSE303 Autumn 2007, Lecture 7 4'&$%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.CSE303 Autumn 2007, Lecture 7 5'&$%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 bytesCSE303 Autumn 2007, Lecture 7 6'&$%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)CSE303 Autumn 2007, Lecture 7 7'&$%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...CSE303 Autumn 2007, Lecture 7 8'&$%Hello, World!#include<stdio.h>int main(int argc, char**argv) {fputs("Hello, World!\n",stdout);return 0;}• Compiling: gcc -o hi hello.c (usually add -Wall -g)• 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.CSE303 Autumn 2007, Lecture 7 9'&$%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...CSE303 Autumn 2007, Lecture 7 10'&$%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.CSE303 Autumn 2007, Lecture 7 11'&$%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


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?