DOC PREVIEW
Berkeley COMPSCI 61C - Introduction to C

This preview shows page 1-2 out of 6 pages.

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

Unformatted text preview:

CS61C L03 Introduction to C (pt 2) (1)!Pearce, Summer 2010 © UCB!inst.eecs.berkeley.edu/~cs61c "CS61C : Machine Structures" Lecture 3 – Introduction to C (pt 2) " 2010-06-23!The typical !development cycle !for a C programmer (cs61c student?)!http://xkcd.com/303/ ! !Instructor Paul Pearce!C help session: Tonight 7:00-9:00pm @ 306 Soda!CS61C L03 Introduction to C (pt 2) (2)!Pearce, Summer 2010 © UCB!Review!• 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 variable!CS61C L03 Introduction to C (pt 2) (3)!Pearce, Summer 2010 © UCB!Common C Error!• There is a difference between assignment and equality!a = b #is assignment!a == b is an equality test!if (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 © UCB!C 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 © UCB!More 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 © UCB!Pointers 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 © UCB!Arrays (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 © UCB!Arrays (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 © UCB!Arrays (3/6)!• Arrays vs pointers int arr[] = {1,2,3}; int *ptr = arr; 104 108 112 116 120 124 128 132 136 140 144 148 152 156"2"116" ..." ..."arr"ptr"Location (address)"name"1" 3"Value of &ptr: 156 Value of &arr: 116 Value of ptr: 116 Value of arr: 116 Value of *ptr: 1 Value of *arr: 1 Legal: ptr = ptr + 1 Illegal: arr = arr + 1 Legal: Asgn to ptr Illegal: Asgn to arr CS61C L03 Introduction to C (pt 2) (10)!Pearce, Summer 2010 © UCB!Arrays (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 valid! int *foo() { int arr[32]; ...; return arr; } is incorrect!CS61C L03 Introduction to C (pt 2) (11)!Pearce, Summer 2010 © UCB!Arrays (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!• Wrong"int i, arr[10]; for(i = 0; i < 10; i++){ ... } • Right int ARRAY_SIZE = 10 int 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 10!CS61C L03 Introduction to C (pt 2) (12)!Pearce, Summer 2010 © UCB!Arrays (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 © UCB!Administrivia!• 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 © UCB!Administrivia!• 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 to tell anyone how your dog ate your computer.!• You should really save for a rainy day … we all get sick and/or have family emergencies!!CS61C L03 Introduction to C (pt 2) (15)!Pearce, Summer 2010 © UCB!Segmentation Fault vs Bus Error?!•


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?