Unformatted text preview:

inst eecs berkeley edu cs61c CS61C Machine Structures Must see talk Thu 4 5pm Sibley by Turing Award winner Fran Allen The Challenge of Multi Cores Think Sequential Run Parallel Lecture 4 Introduction to C pt 2 2007 01 30 bspace berkeley edu Forward your email Lecturer SOE Dan Garcia www cs berkeley edu ddgarcia Voting machine usability In a study of electronic voting machines researchers found that people made errors 3 of the time on simple tasks but 15 of the time on complicated tasks such as switching their vote to another candidate technologyreview com Infotech 20122 CS61C L04 Introduction to C pt 2 1 Garcia Spring 2008 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 value CS61C L04 Introduction to C pt 2 2 Garcia Spring 2008 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 L04 Introduction to C pt 2 3 Garcia Spring 2008 UCB Arrays 1 5 Declaration int ar 2 declares a 2 element integer array An array is really just a block of memory int ar 795 635 declares and fills a 2 elt integer array Accessing elements ar num returns the numth element CS61C L04 Introduction to C pt 2 4 Garcia Spring 2008 UCB Arrays 2 5 Arrays are almost identical to pointers char string and char string are nearly identical declarations They differ in very subtle ways incrementing declaration of filled arrays Key Concept An array variable is a pointer to the first element CS61C L04 Introduction to C pt 2 5 Garcia Spring 2008 UCB Arrays 3 5 Consequences ar is an array variable but looks like a pointer in many respects though not all ar 0 is the same as ar ar 2 is the same as ar 2 We can use pointer arithmetic to access arrays more conveniently Declared arrays are only allocated while the scope is valid char foo char string 32 return string is incorrect CS61C L04 Introduction to C pt 2 6 Garcia Spring 2008 UCB Arrays 4 5 Array size n want to access from 0 to n 1 so you should use counter AND utilize a constant for declaration incr Wrong int i ar 10 for i 0 i 10 i Right define 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 L04 Introduction to C pt 2 7 Garcia Spring 2008 UCB Arrays 5 5 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 L04 Introduction to C pt 2 8 Garcia Spring 2008 UCB Pointer Arithmetic 1 2 Since a pointer is just a mem address we can add to it to traverse an array p 1 returns a ptr to the next array elt p vs p x p p p 1 x p x p p p 1 x p What if we have an array of large structs objects C takes care of it In reality p 1 doesn t add 1 to the memory address it adds the size of the array element CS61C L04 Introduction to C pt 2 9 Garcia Spring 2008 UCB Pointer Arithmetic 2 2 C knows the size of the thing a pointer points to every addition or subtraction moves that many bytes 1 byte for a char 4 bytes for an int etc So the following are equivalent int get int array int n return array n OR return array n CS61C L04 Introduction to C pt 2 10 Garcia Spring 2008 UCB Pointers in C Why use pointers If we want to pass a huge struct or array it s easier 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 Dangling reference premature free Memory leaks tardy free CS61C L04 Introduction to C pt 2 11 Garcia Spring 2008 UCB C Strings A string in C is just an array of characters char string abc How do you tell how long a string is Last character is followed by a 0 byte null terminator int strlen char s int n 0 while s n 0 n return n CS61C L04 Introduction to C pt 2 12 Garcia Spring 2008 UCB Peer Instruction Question void main int p x 5 y init y p x 10 int z flip sign p printf x d y d p d n x y p flip sign int n n n Errors 0 1 2 3 4 5 How many syntax logic errors in this C99 code 6 7 CS61C L04 Introduction to C pt 2 13 Garcia Spring 2008 UCB Peer Instruction Answer void main int p x 5 y init y p x 10 int z flip sign p printf x d y d p d n x y p flip sign int n n n How many syntax logic errors I get 5 signed printing of pointer is logical error CS61C L04 Introduction to C pt 2 14 Errors 0 1 2 3 4 5 6 7 Garcia Spring 2008 UCB Pointer Arithmetic Peer Instruction Q How many of the following are invalid invalid I II III IV V VI VII VIII IX X pointer integer integer pointer pointer pointer pointer integer integer pointer pointer pointer compare pointer to pointer compare pointer to integer compare pointer to 0 compare pointer to NULL CS61C L04 Introduction to C pt 2 15 1 2 3 4 5 6 7 8 9 1 0 Garcia Spring 2008 UCB Pointer Arithmetic Peer Instruction Ans How many of the following are invalid I II III IV V VI VII VIII IX X pointer integer integer pointer pointer pointer pointer integer integer pointer pointer pointer compare pointer to pointer compare pointer to integer compare pointer to 0 compare pointer to NULL CS61C L04 Introduction to C pt 2 16 ptr 1 1 ptr ptr ptr ptr 1 1 ptr ptr ptr invalid ptr1 ptr2 1 2 ptr 1 3 ptr NULL 4 ptr NULL 5 6 7 8 9 1 0 Garcia Spring 2008 UCB Peer …


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
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 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?