DOC PREVIEW
Berkeley COMPSCI 61C - Introduction to C (Part II)

This preview shows page 1-2-14-15-29-30 out of 30 pages.

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

Unformatted text preview:

PowerPoint PresentationMore C Pointer DangersArrays (1/5)Arrays (2/5)Arrays (3/5)Arrays (4/5)Arrays (5/5)Pointer Arithmetic (1/2)Pointer Arithmetic (2/2)Pointers in CC StringsPeer Instruction QuestionPointer Arithmetic Peer Instruction QPeer Instruction“And in Conclusion…”Reference slidesAdministriviaSlide 21Pointers & Allocation (1/2)Pointers & Allocation (2/2)Arrays (one elt past array must be valid)Pointer ArithmeticPointer Arithmetic to Copy memoryArrays vs. PointersPointer Arithmetic SummarySegmentation Fault vs Bus Error?C Pointer DangersC Strings HeadachesCommon C ErrorC String Standard FunctionsCS61C L04 Introduction to C (pt 2) (1)Garcia, Spring 2008 © UCBLecturer SOE Dan Garciawww.cs.berkeley.edu/~ddgarciainst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 4 – Introduction to C (pt 2) 2007-01-30bspace.berkeley.eduForward your email!Voting machine usability In a study of electronic votingmachines, 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/Must-see talk Thu 4-5pm @ Sibley by Turing Award winner Fran Allen:“The Challenge of Multi-Cores: Think Sequential, Run Parallel”CS61C L04 Introduction to C (pt 2) (3)Garcia, Spring 2008 © UCBMore 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) (4)Garcia, Spring 2008 © UCBArrays (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) (5)Garcia, Spring 2008 © UCBArrays (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) (6)Garcia, Spring 2008 © UCBArrays (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 validchar *foo() { char string[32]; ...; return string;} is incorrectCS61C L04 Introduction to C (pt 2) (7)Garcia, Spring 2008 © UCBArrays (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•Wrongint i, ar[10];for(i = 0; i < 10; i++){ ... }•Right #define ARRAY_SIZE 10int 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 10CS61C L04 Introduction to C (pt 2) (8)Garcia, Spring 2008 © UCBArrays (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) (9)Garcia, Spring 2008 © UCBPointer 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) (10)Garcia, Spring 2008 © UCBint get(int array[], int n){ return (array[n]);// OR... return *(array + n);}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:CS61C L04 Introduction to C (pt 2) (11)Garcia, Spring 2008 © UCBPointers 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) (12)Garcia, Spring 2008 © UCBC 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) (13)Garcia, Spring 2008 © UCBPeer Instruction Questionvoid 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 in this C99 code?#Errors 0 1 2 3 4 5 6 7CS61C L04 Introduction to C (pt 2) (15)Garcia, Spring 2008 © UCBPointer Arithmetic Peer Instruction QHow many of the following are invalid?I. pointer + integerII. integer + pointerIII. pointer + pointerIV. pointer – integerV. integer – pointerVI. pointer – pointerVII. compare pointer to pointerVIII. compare pointer to integerIX. compare pointer to 0X. compare pointer to NULL#invalid 1 2 3 4 5 6 7 8 9(1)0CS61C L04 Introduction to C (pt 2) (17)Garcia, Spring 2008 © UCBint main(void){int A[] = {5,10};int *p = A;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 5 10, what will the other two printf output?1: 101 10 5 10 then 101 11 5 112: 104 10 5 10 then 104 11 5 113: 101 <other> 5 10 then 101 <3-others>4: 104 <other> 5 10 then 104 <3-others>5: One of the two printfs causes an ERROR 6: I


View Full Document

Berkeley COMPSCI 61C - Introduction to C (Part II)

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 (Part II)
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 (Part II) 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 (Part II) 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?