Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Similar documents
CS61C : Machine Structures

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

HW1 due Monday by 9:30am Assignment online, submission details to come

More C Pointer Dangers

Review! * follows a pointer to its value! & gets the address of a variable! Pearce, Summer 2010 UCB! ! int x = 1000; Pearce, Summer 2010 UCB!

CS61C : Machine Structures

Instructor: Randy H. Katz hbp://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #4. Agenda

CS61C : Machine Structures

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

CS 61c: Great Ideas in Computer Architecture

ECE 15B COMPUTER ORGANIZATION

C Arrays, Strings, More Pointers Instructor: Steven Ho

Reference slides! C Strings! A string in C is just an array of characters.!!!char string[] = "abc";! How do you tell how long a string is?!

Agenda. Address vs. Value Consider memory to be a single huge array. Review. Pointer Syntax. Pointers 9/9/12

Reference slides! Garcia, Fall 2011 UCB! CS61C L04 Introduction to C (pt 2) (1)!

IT 252 Computer Organization and Architecture Introduction to the C Programming Language

CS61C : Machine Structures

CS61C : Machine Structures

Intro to C: Pointers and Arrays

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

CS 61C: Great Ideas in Computer Architecture Introduc)on to C, Part II

Has there been an update to ANSI C? Lecture #3 C Pointers Yes! It s called the C99 or C9x std

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

CS 61C: Great Ideas in Computer Architecture Introduc)on to C, Part II. Control. Consider memory to be a single huge array

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

Pointers, Arrays, Memory: AKA the cause of those Segfaults

Arrays and Memory Management

How about them A s!! Go Oaktown!! CS61C - Machine Structures. Lecture 4 C Structures Memory Management Dan Garcia.

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS61C : Machine Structures

CS61C : Machine Structures

Arrays and Pointers in C. Alan L. Cox

CS 102 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

Lecture 2: C Programm

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

Topics Introduction to Microprocessors

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

CS61C : Machine Structures

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

CS 61c: Great Ideas in Computer Architecture

Numbers: positional notation. CS61C Machine Structures. Faux Midterm Review Jaein Jeong Cheng Tien Ee. www-inst.eecs.berkeley.

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

Arrays and Pointers (part 1)

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

CS61C : Machine Structures

Arrays and Pointers (part 1)

CS61C : Machine Structures

Intermediate Programming, Spring 2017*

First of all, it is a variable, just like other variables you studied

Chapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

Array Initialization

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

C: Introduction, Pointers Instructors: Steven Ho, Nick Riasanovsky

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

A brief introduction to C programming for Java programmers

Lectures 5-6: Introduction to C

CS 11 C track: lecture 5

CS61C : Machine Structures

Introduction to Scientific Computing and Problem Solving

C'Programming' data'separate'from'methods/functions' Low'memory'overhead'compared'to'Java'

CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduc)on to C (Part I) We are here!

CS61C : Machine Structures

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output }

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro

In Java we have the keyword null, which is the value of an uninitialized reference type

Memory, Arrays & Pointers

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language. Krste Asanović & Randy Katz

Agenda. CS 61C: Great Ideas in Computer Architecture. Lecture 2: Numbers & C Language 8/29/17. Recap: Binary Number Conversion

Number review... Lecture 3 Introduction to the C Programming Language (pt 1) Has there been an update to ANSI C?

ELEC 377 C Programming Tutorial. ELEC Operating Systems

CS 61C: Great Ideas in Computer Architecture Introduction to C, Part III

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

CS 61C: Great Ideas in Computer Architecture Lecture 2: Introduction to C, Part I

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

Lectures 5-6: Introduction to C

Lecture #6 Intro MIPS; Load & Store

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

EL2310 Scientific Programming

Arrays, Pointers and Memory Management

Agenda. Pointer Arithmetic. Pointer Arithmetic pointer + number, pointer number 6/23/2011. Pointer Arithmetic: Peer Instruction Question

Lectures 13 & 14. memory management

CS 61C: Great Ideas in Computer Architecture. Memory Management and Usage

CSCI-1200 Data Structures Spring 2017 Lecture 5 Pointers, Arrays, Pointer Arithmetic

C++ for Java Programmers

CS61, Fall 2012 Section 2 Notes

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

Transcription:

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 Spring 2011 -- Lecture #4 1 Machine Interpretation Levels of Representation/Interpretation High Level Language Program (e.g., C) Compiler Assembly Language Program (e.g., MIPS) Assembler Machine Language Program (MIPS) Hardware Architecture Description (e.g., block diagrams) Architecture Implementation temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; lw $t0, 0($2) lw $t1, 4($2) sw $t1, 0($2) sw $t0, 4($2) We are here! Anything can be represented as a number, i.e., data or instructions 0000 1001 1100 0110 1010 1111 0101 1000 1010 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) Spring 2011 -- Lecture #4 2 Peer Instruction Question 1 int *p; *p = 5; printf( %d\n,*p); What is the result from executing this code? a) Prints 5 b) Prints garbage c) Always crashes. d) Almost always crashes. Spring 2011 -- Lecture #4 3 Spring 2011 -- Lecture #4 4 Peer Instruction Answer 1 int *p; *p = 5; printf( %d\n,*p); What is the result from executing this code? a) Prints 5 Uninitialized variables contain garbage, b) Prints garbage p may point to an invalid memory address. c) Always crashes. Trying to write to an invalid address will d) Almost always crashes. crash program (segmentation fault). There s a (very) small chance p may just happen to contain a valid address. Spring 2011 -- Lecture #4 5 Peer Instruction Question 2 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) How many syntax + logic errors in this C code? #Errors a: 1 b: 2 c: 3 d: 4 e: >4 Spring 2011 -- Lecture #4 6 1

Peer Instruction Answer 2 #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); How many syntax + logic errors in this C code? #Errors a: 1 b: 2 c: 3 d: 4 e: >4 (6!) Spring 2011 -- Lecture #4 7 Pointers Pointers are used to point to any kind of data (int, char, a struct, etc.) Normally a pointer only points to one type (int, char, a struct, etc.). void * is a type that can point to anything (generic pointer) Use sparingly to help avoid program bugs, and security issues, and other bad things! Spring 2011 -- Lecture #3 8 Pointers and Structures Pointers Summary typedef struct int x; int y; Point; Point p1; Point p2; Point* paddr; /* dot notation */ int h = p1.x; p2.y = p1.y; /* arrow notation these lines are equivalent */ int h = (*paddr).x; int h = paddr->x; Why use pointers? If we want to pass a large 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 C, so be careful anytime you deal with them Most problematic with dynamic memory management which you will to know by the end of the semester, but not for the projects (there will be a lab later in the semester) Dangling references and memory leaks Spring 2011 -- Lecture #4 9 Spring 2011 -- Lecture #3 10 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 num th element Spring 2011 -- Lecture #4 11 Spring 2011 -- Lecture #4 12 2

Arrays (2/5) Arrays (3/5) 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 (0 th ) element Spring 2011 -- Lecture #4 13 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 An array variable is read-only Cannot say ar = [anything] Declared arrays are only allocated while the scope is valid char *foo() char string[32];...; return string; is incorrect and very very bad Spring 2011 -- Lecture #4 14 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 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) Spring 2011 -- Lecture #4 15 Spring 2011 -- Lecture #4 16 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; Spring 2011 -- Lecture #4 17 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 ) int strlen(char s[]) int n = 0; while (s[n]!= 0) n++; return n; Spring 2011 -- Lecture #4 18 3

C String Standard Functions #include <string.h> int strlen(char *string); Compute the length of string int strcmp(char *str1, char *str2); Return 0 if str1 and str2 are identical (how is this different from str1 == str2?) char *strcpy(char *dst, char *src); Copy contents of string src to the memory at dst. Caller must ensure that dst has enough memory to hold the data to be copied Note: dst = src only copies pointers, not string itself Spring 2011 -- Lecture #4 19 Spring 2011 -- Lecture #4 20 Administrivia Give your consent forms to your TA in discussion or lab. HW 1 has been posted get started if you haven t already. Lab 2 has been posted. Spring 2011 -- Lecture #4 21 Spring 2011 -- Lecture #4 22 Arrays as Arguments An array is passed to a function as a pointer The array size is lost! Passing arrays: Really int *array Must explicitly 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) Arrays as Arguments 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)); What does this print?... because array is really a pointer (and a pointer is architecture dependent, but likely to be 8 on modern machines!) What does this print? 8 40 Spring 2011 -- Lecture #4 23 Spring 2011 -- Lecture #4 24 4

Pointer Arithmetic pointer + number, pointer number E.g., pointer + 1 adds 1 something to a pointer (in both examples, pretend variable a sits at address 100) char *p; char a; p = &a; Adds 1*sizeof(char) to the memory address What does the following line yield? printf( %u %u\n,p,p+1); int *p; int a; p = &a; 100 101 100 104 Pointer arithmetic should be used cautiously Adds 1*sizeof(int) to the memory address Pointer Arithmetic Since a pointer is just a memory address, we can add to it to step through an array p+1 correctly computes a ptr to the next array element automatically depending on sizeof(type) What if we have an array of large structs (objects)? C takes care of it in the same way it handles basic types. Spring 2011 -- Lecture #4 25 Spring 2011 -- Lecture #4 26 Pointer Arithmetic What is valid pointer arithmetic? Add an integer to a pointer Subtract 2 pointers (in the same array) Compare pointers (<, <=, ==,!=, >, >=) Compare pointer to NULL (indicates that the pointer points to nothing) Everything else is illegal since it makes no sense: Adding two pointers Multiplying pointers Subtract pointer from integer Spring 2011 -- Lecture #4 27 Pointer Arithmetic to Copy Memory We can use pointer arithmetic to walk through memory: void copy(int *from, int *to, int n) int i; for (i=0; i<n; i++) *to++ = *from++; Note we had to pass size (n) to copy Spring 2011 -- Lecture #4 28 Peer Instruction Peer Instruction int main(void) int A[] = 5,10; int *p = A; 5 10 A[0] A[1] p = p + 1; *p = *p + 1; p int main(void) int A[] = 5,10; int *p = A; 5 10 A[0] A[1] p = p + 1; *p = *p + 1; p If the first printf outputs 100 5 5 10, what will the next two printf output? a) 101 10 5 10 then 101 11 5 11 b) 104 10 5 10 then 104 11 5 11 c) 101 <other> 5 10 then 101 <3-others> d) 104 <other> 5 10 then 104 <3-others> e) One of the two printfs causes an ERROR If the first printf outputs 100 5 5 10, what will the next two printf output? a) 101 10 5 10 then 101 11 5 11 b) 104 10 5 10 then 104 11 5 11 c) 101 <other> 5 10 then 101 <3-others> d) 104 <other> 5 10 then 104 <3-others> e) One of the two printfs causes an ERROR Spring 2011 -- Lecture #4 29 Spring 2011 -- Lecture #4 30 5

Pointer Arithmetic: Peer Instruction Question How many of the following are invalid? I. pointer + integer II. integer + pointer III. pointer + pointer IV. pointer integer V. integer pointer VI. pointer pointer VII. compare pointer to pointer VIII. compare pointer to integer IX. compare pointer to 0 X. compare pointer to NULL #invalid a: 1 b: 2 c: 3 d: 4 e: 5 Pointer Arithmetic Peer Instruction Answer How many of the following are invalid? I. pointer + integer ptr + 1 II. integer + pointer 1 + ptr III. pointer + pointer ptr + ptr IV. pointer integer ptr - 1 V. integer pointer 1 - ptr VI. pointer pointer ptr - ptr VII. compare pointer to pointer ptr1 == ptr2 VIII. compare pointer to integer ptr == 1 IX. compare pointer to 0 ptr == NULL X. compare pointer to NULL ptr == NULL #invalid a: 1 b: 2 c: 3 d: 4 e: 5 Spring 2011 -- Lecture #4 31 Spring 2011 -- Lecture #4 32 Spring 2011 -- Lecture #4 33 Spring 2011 -- Lecture #4 34 Pointers & Allocation (1/2) After declaring a pointer: int *ptr; ptr doesn t actually point to anything yet (points somewhere, but don t know where). We can either: Make it point to something that already exists, or Allocate room in memory for something new that it will point to Pointers & Allocation (2/2) Option 1: Pointing to something that already exists: int *ptr, var1, var2; var1 = 5; ptr = &var1; var2 = *ptr; var1 and var2 have space implicitly allocated for them ptr? var1? 5 var2? 5 Option 2: Allocate room in memory for new thing to point to. See next lecture. Spring 2011 -- Lecture #4 35 Spring 2011 -- Lecture #4 36 6

Arrays (one element past array must be valid) Array size n; want to access from 0 to n-1, but test for exit by comparing to address one element past the array int ar[10], *p, *q, sum = 0;... p = &ar[0]; q = &ar[10]; while (p!= q) /* sum = sum + *p; p = p + 1; */ sum += *p++; Is this legal? C defines that one element past end of array must be a valid address, i.e., will not cause an bus error or address error Pointers and Functions (1/4) Sometimes you want to have a procedure increment a variable What gets printed? void AddOne(int x) x = x + 1; int y = 5; AddOne(y); printf( y = %d\n, y); y = 5 Spring 2011 -- Lecture #4 37 Spring 2011 -- Lecture #4 38 Pointers and Functions (2/4) Solved by passing in a pointer to our subroutine. Now what gets printed? void AddOne(int *p) *p = *p + 1; int y = 5; AddOne(&y); printf( y = %d\n, y); y = 6 Pointers and Functions (3/4) But what if the thing you want changed is a pointer? What gets printed? void IncrementPtr(int p = p + 1; int A[3] = 50, 60, 70; int *q = A; IncrementPtr(q); printf( *q = %d\n, *q); *p) *q = 50 A q 50 60 70 Spring 2011 -- Lecture #4 39 Spring 2011 -- Lecture #4 40 Pointers and Functions (4/4) Solution! Pass a pointer to a pointer, declared as **h Now what gets printed? void IncrementPtr(int **h) *h = *h + 1; int A[3] = 50, 60, 70; int *q = A; IncrementPtr(&q); printf( *q = %d\n, *q); *q = 60 A q q 50 60 70 Spring 2011 -- Lecture #4 41 Spring 2011 -- Lecture #4 42 7

Segmentation Fault vs. Bus Error C Pointer Dangers 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. 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 */ First pointer declaration is invalid since the types do not match Second declaration is valid C but is almost certainly wrong Is it ever correct? Spring 2011 -- Lecture #4 43 Spring 2011 -- Lecture #4 44 C String Problems Common mistake is to forget to allocate an extra byte for the null terminator More generally, C requires the programmer to manage memory manually (unlike Java or C++) When creating a long string by concatenating several smaller strings, the programmer must insure there is enough space to store the full string! What if you don t know ahead of time how big your string will be? Buffer overrun security holes! More Common C Errors Difference between assignment and equality a = b is assignment a == b is an equality test One of the most common errors for beginning C programmers! One pattern (when comparing with constant) is to put the var on the right! If you happen to use =, it won t compile! if (3 == a)... Spring 2011 -- Lecture #4 45 Spring 2011 -- Lecture #4 46 And in Conclusion, Pointers and array variables are very similar. Can use pointer or array syntax to index into arrays. Strings are (null terminated) arrays of characters Adding 1 to a pointer moves the pointer by the size of the thing it s pointing to. Pointers are the source of many bugs in C, so handle with care Spring 2011 -- Lecture #4 47 8