Unformatted text preview:

inst eecs berkeley edu cs61c CS61C Machine Structures Lecture 3 Introduction to C pt 2 2010 06 23 C help session Tonight 7 00 9 00pm 306 Soda Instructor Paul Pearce The typical development cycle for a C programmer cs61c student http xkcd com 303 CS61C L03 Introduction to C pt 2 1 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 2 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 3 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 4 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 5 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 6 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 7 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 8 Pearce Summer 2010 UCB Arrays 3 6 Arrays vs pointers int arr 1 2 3 int ptr arr Location address 104 108 112 116 120 124 128 132 136 140 144 148 152 156 name 1 arr 2 3 116 ptr 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 9 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 10 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 11 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 12 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 13 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 14 Pearce Summer 2010 UCB Segmentation Fault vs Bus Error http www hyperdictionary com Bus Error A fatal failure in the execution of a machine language instruction resulting from the processor detecting an anomalous condition on its bus Such conditions include invalid address alignment accessing a multi byte number at an odd address accessing a physical address that does not correspond to any device or some other device specific hardware error A bus error triggers a processor level exception which Unix translates into a SIGBUS signal which if not caught will terminate the current process Segmentation Fault An error in which a running Unix program attempts to access memory not allocated to it and terminates with a segmentation violation error and usually a core dump CS61C L03 …


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?