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

This preview shows page 1-2-3-24-25-26-27-48-49-50 out of 50 pages.

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

Unformatted text preview:

Slide 1Levels of Representation/InterpretationAgendaPeer Instruction QuestionPeer Instruction AnswerAgendaArrays (1/5)Arrays (2/5)Arrays (3/5)Arrays (4/5)Arrays (5/5)Array SummaryAgendaSlide 14CS 61c in the NewsAdministriviaPointers and StructuresAgendaC StringsPointer ArithmeticArrays and PointersArrays and PointersArrays and PointersPointer Arithmetic (1/2)Pointer Arithmetic (2/2)Peer InstructionPeer InstructionAgendaPointers & Allocation (1/2)Pointers & Allocation (2/2)Arrays (one element past array must be valid)Pointer ArithmeticPointer Arithmetic to Copy MemoryPointer Arithmetic SummaryPointer Arithmetic: Peer Instruction QuestionPointer Arithmetic Peer Instruction AnswerC FunctionsC FunctionsPointers and Functions (1/4)Pointers and Functions (2/4)Pointers and Functions (3/4)Pointers and Functions (4/4)C String Standard Functions #include <string.h>AgendaAgendaSegmentation Fault vs. Bus ErrorC Pointer DangersC String ProblemsMore Common C ErrorsAnd in Conclusion, …CS 61C: Great Ideas in Computer Architecture (Machine Structures)Introduction to C (Part II)Instructors:Randy H. KatzDavid A. Pattersonhttp://inst.eecs.Berkeley.edu/~cs61c/sp111Spring 2011 -- Lecture #401/13/2019Levels of Representation/Interpretationlw $t0, 0($2)lw $t1, 4($2)sw $t1, 0($2)sw $t0, 4($2)High Level LanguageProgram (e.g., C)Assembly Language Program (e.g., MIPS)Machine Language Program (MIPS)Hardware Architecture Description(e.g., block diagrams) CompilerAssemblerMachine Interpretationtemp = v[k];v[k] = v[k+1];v[k+1] = temp;0000 1001 1100 0110 1010 1111 0101 10001010 1111 0101 1000 0000 1001 1100 0110 1100 0110 1010 1111 0101 1000 0000 1001 0101 1000 0000 1001 1100 0110 1010 1111 Logic Circuit Description(Circuit Schematic Diagrams)Architecture ImplementationAnything can be representedas a number, i.e., data or instructions01/13/2019 2Spring 2011 -- Lecture #4We are here!Agenda•Arrays•Administrivia•Strings•Pointer Allocation•Technology Break•Pointer Problems•Summary01/13/2019 Spring 2011 -- Lecture #4 3Peer Instruction Questionvoid main(); { int *p, x=5, y; // init y = *(p = &x) + 1; 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 C code?01/13/2019 Spring 2011 -- Lecture #4 4#ErrorsRed: 1Orange: 2Green: 3Yellow: 4Pink: >4Peer Instruction Answervoid main(); { int *p, x=5, y; // init y = *(p = &x) + 1; 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 C code?01/13/2019 Spring 2011 -- Lecture #4 5#insert <stdio.h>#ErrorsRed: 1Orange: 2Green: 3Yellow: 4Pink: >4Agenda•Arrays•Administrivia•Strings•Pointer Allocation•Technology Break•Pointer Problems•Summary01/13/2019 Spring 2011 -- Lecture #4 6Arrays (1/5)•Declaration:int ar[2];declares a 2-element integer array: just a block of memory int ar[] = {795, 635};declares and initializes a 2-element integer array•Accessing elements:ar[num]returns the numth element01/13/2019 Spring 2011 -- Lecture #4 7Arrays (2/5)•Arrays are (almost) identical to pointers–char *string and char string[] are nearly identical declarations–Differ in subtle ways: incrementing, declaration of filled arrays•Key Concept: Array variable is a “pointer” to the first (0th) element01/13/2019 Spring 2011 -- Lecture #4 8Arrays (3/5)•Consequences:–ar is an array variable, but looks like a pointer–ar[0] is the same as *ar–ar[2] is the same as *(ar+2)–We can use pointer arithmetic to conveniently access arrays•Declared arrays are only allocated while the scope is validchar *foo() { char string[32]; ...; return string;} is incorrect and very very bad01/13/2019 Spring 2011 -- Lecture #4 9Arrays (4/5)•Array size n; want to access from 0 to n-1, so you should use counter AND utilize a variable for declaration & incrementation–Bad patternint i, ar[10];for(i = 0; i < 10; i++){ ... }–Better patternint ARRAY_SIZE = 10int i, a[ARRAY_SIZE];for(i = 0; i < ARRAY_SIZE; i++){ ... }•SINGLE SOURCE OF TRUTH–You’re utilizing indirection and avoiding maintaining two copies of the number 1001/13/2019 Spring 2011 -- Lecture #4 10Arrays (5/5)•Pitfall: An array in C does not know its own length, and its bounds are not checked!–Consequence: We can accidentally access off the end of an array–Consequence: We must pass the array and its size to any procedure that is going to manipulate it•Segmentation faults and bus errors:–These are VERY difficult to find; be careful! (You’ll learn how to debug these in lab)01/13/2019 Spring 2011 -- Lecture #4 11Array Summary•Array indexing is syntactic sugar for pointers•a[i] is treated as *(a+i)•E.g., three equivalent ways to zero an array:–for (i=0; i < size; i++) a[i] = 0;–for (i=0; i < size; i++) *(a+i) = 0;–for (p=a; p < a+size; p++) *p = 0;01/13/2019 12Spring 2011 -- Lecture #4Agenda•Arrays•Administrivia•Strings•Pointer Allocation•Technology Break•Pointer Problems01/13/2019 Spring 2011 -- Lecture #4 1301/13/2019 Spring 2011 -- Lecture #4 1401/13/2019 Spring 2011 -- Lecture #4 15CS 61c in the NewsGoogle is hiring!And your knowledgeof MapReduce is sureto impress them!Administrivia•We have your pictures!–file:///Users/randykatz/Documents/Courses/CS61C/Sp11/Photos/cs61Pictures/classpage.html •This week in lab and homework:–Lab #2, MapReduce posted–HW #2, Assembly Language posted (hws are leading the lecture – this is a good forcing function to read ahead) •Join the class discussion/announcements group!–But don’t forget that some stuff is VERY easy to find on your own (e.g., arrow notation in C/C++)01/13/2019 Spring 2011 -- Lecture #4 16Pointers and Structuresstruct Point { int x; int y;};Point p1;Point p2;Point* paddr;/* dot notation */int h = p1.x;p2.y = p1.y;/* arrow notation */int h = paddr->x;int h = (*paddr).x;/* This works too */p1 = p2;01/13/2019 Spring 2011 -- Lecture #4 17Agenda•Arrays•Administrivia•Strings•Pointer Allocation•Technology Break•Pointer Problems•Summary01/13/2019 Spring 2011 -- Lecture #4 18C Strings•String in C is just an array of characterschar string[] = "abc";•How do you tell how long a string is?–Last character is followed by a 0 byte (aka “null terminator”)01/13/2019 Spring 2011 -- Lecture #4 19int strlen(char s[]){ int n = 0; while (s[n] != 0) n++; return n;}01/13/2019 Spring 2011 -- Lecture #4 20Pointer Arithmeticpointer


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?