Unformatted text preview:

inst eecs berkeley edu cs61c CS61C Machine Structures Lecture 4 Introduction to C pt 2 2010 01 27 C review update Tonight 7 30 8 30pm 306 Soda Lecturer SOE Dan Garcia www cs berkeley edu ddgarcia All eyes on Apple Today Apple will make a big announcement many have speculated it s a tablet much like the iPhone which will change the industry www nytimes com 2010 01 26 technology 26apple html CS61C L04 Introduction to C pt 2 1 Garcia Spring 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 value CS61C L04 Introduction to C pt 2 2 Garcia Spring 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 L04 Introduction to C pt 2 3 Garcia Spring 2010 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 2010 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 2010 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 2010 UCB Arrays 4 5 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 ar 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 L04 Introduction to C pt 2 7 Garcia Spring 2010 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 2010 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 2010 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 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 Dangling reference premature free Memory leaks tardy free CS61C L04 Introduction to C pt 2 11 Garcia Spring 2010 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 2010 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 13 invalid a 1 b 2 c 3 d 4 e 5 Garcia Spring 2010 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 a 101 10 5 10 then b 104 10 5 10 then c 101 other 5 10 then d 104 other 5 10 then e One of the two printfs CS61C L04 Introduction to C pt 2 15 5 10 what will the 101 11 5 11 104 11 5 11 101 3 others 104 3 others causes an ERROR Garcia Spring 2010 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 17 Garcia Spring 2010 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 CS61C L04 Introduction to C pt 2 18 Garcia Spring 2010 UCB Administrivia Read K R …


View Full Document

Berkeley COMPSCI 61C - Lecture 4 – 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 Lecture 4 – 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 Lecture 4 – 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?