DOC PREVIEW
Berkeley COMPSCI 61C - Introduction to C

This preview shows page 1-2-15-16-31-32 out of 32 pages.

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

Unformatted text preview:

Slide 1ReviewCommon C ErrorC Pointer DangersMore C Pointer DangersPointers in CArrays (1/6)Arrays (2/6)Arrays (3/6)Arrays (4/6)Arrays (5/6)Arrays (6/6)AdministriviaSlide 14Segmentation Fault vs Bus Error?Pointer Arithmetic (1/2)Pointer Arithmetic (2/2)C StringsC string.h Standard FunctionsC string.h SAFE(R) FunctionsC Strings HeadachesPointer Arithmetic Peer Instruction QPeer Instruction“And in Conclusion…”Reference slidesArrays (one elt past array must be valid)Pointers & Allocation (1/2)Pointers & Allocation (2/2)Pointer ArithmeticPointer Arithmetic to Copy memoryPointer Arithmetic SummaryArrays vs. PointersCS61C L03 Introduction to C (pt 2) (1)Pearce, Summer 2010 © UCBinst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 3 – Introduction to C (pt 2) 2010-06-23The typical development cycle for a C programmer (cs61c student?)http://xkcd.com/303/Instructor Paul PearceC help session: Tonight 7:00-9:00pm @ 306 SodaCS61C L03 Introduction to C (pt 2) (2)Pearce, Summer 2010 © UCBReview•All declarations go at the beginning of each function except if you use C99.•Only 0 and NULL evaluate to FALSE.•All data is in memory. Each memory location has an address to use to refer to it and a value stored in it.•A pointer is a C version of the address.* “follows” a pointer to its value& gets the address of a variableCS61C L03 Introduction to C (pt 2) (3)Pearce, Summer 2010 © UCBCommon C Error•There is a difference between assignment and equalitya = b is assignmenta == b is an equality testif (x = 5) {…} // This builds, and costs sleep•This is one of the most common errors for beginning C programmers!•One solution (when comparing with constant) is to put the var on the right! If you happen to use =, it won’t compile.if (3 == a) { ...CS61C L03 Introduction to C (pt 2) (4)Pearce, Summer 2010 © UCBC Pointer Dangers•Unlike Java, C lets you cast a value of any type to any other type without performing any checking.int x = 1000;int *p = x; /* invalid */int *q = (int *) x; /* valid */•The first pointer declaration is invalid since the types do not match.•The second declaration is valid C but is almost certainly wrong•Is it ever correct?CS61C L03 Introduction to C (pt 2) (5)Pearce, Summer 2010 © UCBMore C Pointer Dangers•Declaring a pointer just allocates space to hold the pointer – it does not allocate something to be pointed to!•Local variables in C are not initialized, they may contain anything.•What does the following code do?void f(){ int *ptr; *ptr = 5;}CS61C L03 Introduction to C (pt 2) (6)Pearce, Summer 2010 © UCBPointers in C•Why use pointers?•If we want to pass a huge struct or array, it’s easier / faster / etc to pass a pointer than the whole thing.•In general, pointers allow cleaner, more compact code.•So what are the drawbacks?•Pointers are probably the single largest source of bugs in software, so be careful anytime you deal with them.•Allocation issues (Discussed tomorrow)CS61C L03 Introduction to C (pt 2) (7)Pearce, Summer 2010 © UCBArrays (1/6)•Declaration:int arr[2];declares a 2-element integer array. An array is really just a block of memory. int arr[] = {795, 635};declares and fills a 2-elt integer array.•Accessing elements:arr[num]returns the numth element.CS61C L03 Introduction to C (pt 2) (8)Pearce, Summer 2010 © UCBArrays (2/6)•Arrays are closely tied with pointers•They differ in very subtle ways: incrementing, declaration of filled arrays, value•Pointers are frequently used to access arrays•Some pointer math can be used on arrays•Key Concept: An array variable is a “pointer” to the first element.CS61C L03 Introduction to C (pt 2) (9)Pearce, Summer 2010 © UCBArrays (3/6)•Arrays vs pointersint arr[] = {1,2,3};int *ptr = arr;104 108 112 116 120 124 128 132 136 140 144 148 152 1562116 ... ...arrptrLocation (address)name1 3Value of &ptr: 156 Value of &arr: 116Value of ptr: 116 Value of arr: 116Value of *ptr: 1 Value of *arr: 1Legal: ptr = ptr + 1 Illegal: arr = arr + 1Legal: Asgn to ptr Illegal: Asgn to arrCS61C L03 Introduction to C (pt 2) (10)Pearce, Summer 2010 © UCBArrays (4/6)•Consequences:•arr is an array variable but looks like a pointer in many respects (though not all)•arr[0] is the same as *arr•arr[2] is the same as *(arr+2)•We can use pointer arithmetic to access arrays more conveniently.•Declared arrays are only allocated while the scope is validint *foo() { int arr[32]; ...; return arr;} is incorrectCS61C L03 Introduction to C (pt 2) (11)Pearce, Summer 2010 © UCBArrays (5/6)•Array size n; want to access from 0 to n-1, so you should use counter AND utilize a variable for declaration & incr•Wrongint i, arr[10];for(i = 0; i < 10; i++){ ... }•Right int ARRAY_SIZE = 10int i, a[ARRAY_SIZE];for(i = 0; i < ARRAY_SIZE; i++){ ... }•Why? SINGLE SOURCE OF TRUTH•You’re utilizing indirection and avoiding maintaining two copies of the number 10CS61C L03 Introduction to C (pt 2) (12)Pearce, Summer 2010 © UCBArrays (6/6)•Pitfall: An array in C does not know its own length, & bounds not checked!•Consequence: We can accidentally access off the end of an array.•Consequence: We must pass the array and its size to a procedure which is going to traverse it.•Segmentation faults and bus errors:•These are VERY difficult to find; be careful! (You’ll learn how to debug these in lab…)CS61C L03 Introduction to C (pt 2) (13)Pearce, Summer 2010 © UCBAdministrivia•Read K&R 6 by the next lecture•Lab problems•Labs should be fixed by Thursday. •You can get Tuesdays lab checked off Thursday. Everyone gets 1 bonus point (for first hour checkoff).•Homework expectations•Readers don’t have time to fix your programs. They must run on lab machines.•Code that doesn’t compile or fails all of the autograder tests  0•C Help session tonight! 7pm, 306 Soda•HW1 due Friday!CS61C L03 Introduction to C (pt 2) (14)Pearce, Summer 2010 © UCBAdministrivia•Slip days•You get 3 “slip days” to use for any homework assignment or project•They are used at 1-day increments. Thus 1 minute late = 1 slip day used.•They’re recorded automatically (by checking submission time) so you don’t need to tell us when you’re using them•Once you’ve used all of your slip days, when a project/hw is late, it’s … 0 points.•If you submit twice, we ALWAYS grade the latter, and deduct slip days appropriately•You no longer need


View Full Document

Berkeley COMPSCI 61C - Introduction to C

Documents in this Course
SIMD II

SIMD II

8 pages

Midterm

Midterm

7 pages

Lecture 7

Lecture 7

31 pages

Caches

Caches

7 pages

Lecture 9

Lecture 9

24 pages

Lecture 1

Lecture 1

28 pages

Lecture 2

Lecture 2

25 pages

VM II

VM II

4 pages

Midterm

Midterm

10 pages

Load more
Download Introduction to C
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 Introduction to C 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 Introduction to C 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?