Unformatted text preview:

CS 61C Great Ideas in Computer Architecture Machine Structures Introduction to C Part II Instructors Randy H Katz David A Patterson http inst eecs Berkeley edu cs61c sp11 01 13 2019 Spring 2011 Lecture 4 1 Levels of Representation Interpretation High Level Language Program e g C Compiler Assembly Language Program e g MIPS Assembler Machine Language Program MIPS temp v k v k v k 1 v k 1 temp lw lw sw sw t0 0 2 t1 4 2 t1 0 2 t0 4 2 0000 1010 1100 0101 1001 1111 0110 1000 1100 0101 1010 0000 We are here Anything can be represented as a number i e data or instructions 0110 1000 1111 1001 1010 0000 0101 1100 1111 1001 1000 0110 0101 1100 0000 1010 1000 0110 1001 1111 Machine Interpretation Hardware Architecture Description e g block diagrams Architecture Implementation Logic Circuit Description Circuit Schematic Diagrams Spring 2011 Lecture 4 01 13 2019 2 Agenda Arrays Administrivia Strings Pointer Allocation Technology Break Pointer Problems Summary 01 13 2019 Spring 2011 Lecture 4 3 Peer Instruction Question void 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 Errors Red 1 Orange 2 How many syntax logic Green 3 Yellow 4 errors in this C code Pink 4 01 13 2019 Spring 2011 Lecture 4 4 Peer Instruction Answer insert stdio h void 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 Errors Red 1 Orange 2 How many syntax logic Green 3 errors in this C code Yellow 4 Pink 4 01 13 2019 Spring 2011 Lecture 4 5 Agenda Arrays Administrivia Strings Pointer Allocation Technology Break Pointer Problems Summary 01 13 2019 Spring 2011 Lecture 4 6 Arrays 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 element 01 13 2019 Spring 2011 Lecture 4 7 Arrays 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 element 01 13 2019 Spring 2011 Lecture 4 8 Arrays 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 valid char foo char string 32 return string is incorrect and very very bad 01 13 2019 Spring 2011 Lecture 4 9 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 incrementation Bad pattern int i ar 10 for i 0 i 10 i Better pattern int ARRAY SIZE 10 int 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 10 01 13 2019 Spring 2011 Lecture 4 10 Arrays 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 11 Array 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 Spring 2011 Lecture 4 12 Agenda Arrays Administrivia Strings Pointer Allocation Technology Break Pointer Problems 01 13 2019 Spring 2011 Lecture 4 13 01 13 2019 Spring 2011 Lecture 4 14 CS 61c in the New Google is hiring And your knowledge of MapReduce is sure to impress them 01 13 2019 Spring 2011 Lecture 4 15 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 16 Pointers and Structures struct 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 17 Agenda Arrays Administrivia Strings Pointer Allocation Technology Break Pointer Problems Summary 01 13 2019 Spring 2011 Lecture 4 18 C Strings 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 aka null terminator 01 13 2019 int strlen char s int n 0 while s n 0 n return n Spring 2011 Lecture 4 19 Pointer Arithmetic pointer number E g pointer 1 char char char pointer number adds 1 something to a pointer p a b int int int p a p 1 p a b p a p 1 In each p now points to b Assuming compiler doesn t reorder variables in memory Adds 1 sizeof char Adds 1 sizeof int to the memory address to the memory address Pointer arithmetic should be used cautiously 01 13 2019 Spring 2011 Lecture 4 20 Arrays and Pointers Passing arrays Array pointer to the initial 0th array element a i a i An array is passed to a function as a pointer The array size is lost Usually bad style to interchange arrays and pointers Avoid pointer arithmetic 01 13 2019 Must explicitly Really int array pass the size int foo int array unsigned int size array size 1 int main void int a 10 b 5 foo a 10 foo b 5 Spring 2011 Lecture 4 21 Arrays and Pointers int foo int array unsigned int size printf d n sizeof array int main void int a 10 b 5 foo a 10 foo b 5 printf d n sizeof a 01 13 2019 Spring 2011 Lecture 4 What does this print 8 because array is really a pointer and a pointer is architecture dependent but likely to be 8 on modern machines What does …


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
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) 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?