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 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 x p p p 1 x p x p p p 1 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 Pointer Arithmetic Peer Instruction Q 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 15 invalid 1 2 3 4 5 6 7 8 9 1 0 Garcia Spring 2008 UCB Peer Instruction int main void int A 5 10 int p A 5 10 A 0 A 1 p printf u d d d n p p A 0 A 1 p p 1 printf u d d d n p p A 0 A 1 p p 1 printf u d d d n p p A 0 A 1 If the first printf outputs 100 5 other two printf output 1 101 10 5 10 then 2 104 10 5 10 then 3 101 other 5 10 then 4 104 other 5 10 then 5 One of the two printfs 6 I surrender CS61C L04 Introduction to C pt 2 17 5 10 what will the 101 11 5 11 104 11 5 11 101 3 others 104 3 others causes an ERROR Garcia Spring 2008 UCB And in Conclusion Pointers and arrays are virtually same C knows how to increment pointers C is an efficient language with little protection Array bounds not checked Variables not automatically initialized Beware The cost of efficiency is more overhead for the programmer C gives you a lot of extra rope but be careful not to hang yourself with it CS61C L04 Introduction to C pt 2 18 Garcia Spring 2008 UCB Reference slides You ARE responsible for the material on these slides they re just taken from the reading anyway we ve moved them to the end and off stage to give more breathing room to lecture …
View Full Document
Unlocking...