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

Similar documents
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 C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

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

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

Intro to C: Pointers and Arrays

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. Lecture 3: Pointers. Krste Asanović & Randy Katz

Arrays and Memory Management

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

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

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

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

CS61C : Machine Structures

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

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

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

CS61C : Machine Structures

Arrays and Pointers in C. Alan L. Cox

CS61C : Machine Structures

CS 61c: Great Ideas in Computer Architecture

ECE 15B COMPUTER ORGANIZATION

More C Pointer Dangers

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

C Arrays, Strings, More Pointers Instructor: Steven Ho

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

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

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

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

CS61C : Machine Structures

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

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

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

CS 110 Computer Architecture. Lecture 4: Introduction to C, Part III. Instructor: Sören Schwertfeger.

CS61C : Machine Structures

C++ for Java Programmers

CS61C : Machine Structures

Topics Introduction to Microprocessors

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

Intermediate Programming, Spring 2017*

CS61C : Machine Structures

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

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

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

Arrays and Pointers. CSE 2031 Fall November 11, 2013

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

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

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

CS61C : Machine Structures

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

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

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

CS61C : Machine Structures

CS61C : Machine Structures

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

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

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

Lecture 2: C Programm

Pointers in C/C++ 1 Memory Addresses 2

CS 61C: Great Ideas in Computer Architecture Introduction to C

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

CS61C : Machine Structures

Review of Last Lecture

CS 61c: Great Ideas in Computer Architecture

Dynamic memory allocation (malloc)

CS 0449 Sample Midterm

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

Lecture 04 Introduction to pointers

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

377 Student Guide to C++

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

Understanding Pointers

Fundamental of Programming (C)

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

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

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

CSC 1600 Memory Layout for Unix Processes"

Lecture 2, September 4

CS61C : Machine Structures

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

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

Outline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Arrays and Pointers (part 1)

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

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

Lecture 03 Bits, Bytes and Data Types

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

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

Pointers and References

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

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

Arrays and Pointers (part 1)

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

Kurt Schmidt. October 30, 2018

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

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

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

Transcription:

CS 61C: Great Ideas in Computer Architecture Introduc)on to C, Part II Instructors: Krste Asanovic & Vladimir Stojanovic h>p://inst.eecs.berkeley.edu/~cs61c/sp15 1

Review: Components of a Computer Processor Control Enable? Read/Write Memory Input Datapath PC Address Program Bytes Registers Write Data ArithmeLc & Logic Unit (ALU) Read Data Data Output Processor- Memory Interface I/O- Memory Interfaces 2

Address vs. Value Consider memory to be a single huge array Each cell of the array has an address associated with it Each cell also stores some value Do you think they use signed or unsigned numbers? NegaLve address?! Don t confuse the address referring to a memory localon with the value stored there... 101 102 103 104 105... 23 42... 3

Pointers An address refers to a parlcular memory localon; e.g., it points to a memory localon Pointer: A variable that contains the address of a variable LocaLon (address) name... 101 102 103 104 105... 23 42 104... x y p 4

Pointer Syntax int *x;! Tells compiler that variable x is address of an int! x = &y;! Tells compiler to assign address of y to x! & called the address operator in this context! z = *x;! Tells compiler to assign value at address in x to z! * called the dereference operator in this context! 5

CreaLng and Using Pointers How to create a pointer: & operator: get address of a variable int *p, x; p? x? x = 3; p? x 3 p = &x; p x 3 How get a value pointed to? * (dereference operator): get the value that the pointer points to!printf( p points to %d\n,*p);! Note the * gets used 2 different ways in this example. In the declaralon to indicate that p is going to be a pointer, and in the printf to get the value pointed to by p. 6

Using Pointer for Writes How to change a variable pointed to? Use the dereference operator * on leg of assignment operator = p x 3 *p = 5; p x 5 7

Pointers and Parameter Passing Java and C pass parameters by value Procedure/funcLon/method gets a copy of the parameter, so changing the copy cannot change the original void add_one (int x) {! x = x + 1; }! int y = 3;! add_one(y);! y remains equal to 3 8

Pointers and Parameter Passing How can we get a funclon to change the value held in a variable? void add_one (int *p) {!*p = *p + 1; }! int y = 3;! add_one(&y);! y is now equal to 4 9

Types of Pointers Pointers are used to point to any kind of data (int, char, a struct, a pointer, 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 void * sparingly to help avoid program bugs, and security issues, and other bad things! 10

More C Pointer Dangers Declaring a pointer just allocates space to hold the pointer does not allocate thing being pointed to! Local variables in C are not inilalized, they may contain anything (aka garbage ) What does the following code do? void f() { int *ptr; *ptr = 5; } 11

tyepdef struct { int x; int y; } Point; Point p1; Point p2; Point *paddr; Pointers and Structures /* dot notation */ int h = p1.x; p2.y = p1.y; /* arrow notation */ int h = paddr->x; int h = (*paddr).x; /*structure assignment*/ p2 = p1; Note, C structure assignment is not a deep copy. All members are copied, but not things pointed to by members. 12

Pointers in C 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 Want to modify an object, not just pass its value 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 anylme you deal with them Most problemalc with dynamic memory management coming up next lecture Dangling references and memory leaks 13

Why Pointers in C? At Lme C was invented (early 1970s), compilers ogen didn t produce efficient code Computers 25,000 Lmes faster today, compilers be>er C designed to let programmer say what they want code to do without compiler gepng in way Even give compiler hints which registers to use! Today, many applicalons a>ain acceptable performance using higher- level languages without pointers Low- level system code slll needs low- level access via pointers, hence conlnued popularity of C 14

Clickers/Peer InstrucLon Time void foo(int *x, int *y)! { int t;! if ( *x > *y ) { t = *y; *y = *x; *x = t; }! }! int a=3, b=2, c=1;! foo(&a, &b);! foo(&b, &c);! foo(&a, &b);! printf("a=%d b=%d c=%d\n", a, b, c);!! Result is: A: a=3 b=2 c=1! B: a=1 b=2 c=3! C: a=1 b=3 c=2! D: a=3 b=3 c=3! E: a=1 b=1 c=1! 15

Administrivia We can accommodate all those on the wait list, but you have to enroll in a lab seclon with space! Lab seclon is important, but you can a>end different discussion seclon Enroll into lab with space, and try to swap with someone later HW0 due 11:59:59pm Sunday 2/1 Right ager the Superbowl Midterm- II now Thursday April 9 in class 16

C Arrays DeclaraLon: int ar[2]; declares a 2- element integer array: just a block of memory int ar[] = {795, 635}; declares and inilalizes a 2- element integer array returns the num th element 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; } 18

Array Name / Pointer Duality Key Concept: Array variable is a pointer to the first (0 th ) element So, array variables almost idenlcal to pointers char *string and char string[] are nearly idenlcal declaralons Differ in subtle ways: incremenlng, declaralon of filled arrays 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) Can use pointer arithmelc to conveniently access arrays 19

Changing a Pointer Argument? What if want funclon to change a pointer? What gets printed? void inc_ptr(int *p)! { p = p + 1; }!! int A[3] = {50, 60, 70};! int *q = A;! inc_ptr( q);! printf( *q = %d\n, *q);! A! q! *q = 50! 50! 60! 70!

Pointer to a Pointer SoluLon! Pass a pointer to a pointer, declared as **h! Now what gets printed? void inc_ptr(int **h)! { *h = *h + 1; }!! int A[3] = {50, 60, 70};! int *q = A;! inc_ptr(&q);! printf( *q = %d\n, *q);! *q = 60! A! q! q! 50! 60! 70!

C Arrays are Very PrimiLve 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 SegmentaLon faults and bus errors: These are VERY difficult to find; be careful! (You ll learn how to debug these in lab) 22

Use Defined Constants Array size n; want to access from 0 to n- 1, so you should use counter AND ullize a variable for declaralon & incrementalon Bad pa>ern int i, ar[10]; for(i = 0; i < 10; i++){... } Be>er pa>ern const int ARRAY_SIZE = 10 int i, a[array_size]; for(i = 0; i < ARRAY_SIZE; i++){... } Accessing elements: ar[num] SINGLE SOURCE OF TRUTH You re ullizing indireclon and avoiding maintaining two copies of the number 10 DRY: Don t Repeat Yourself 23

PoinLng to Different Size Objects Modern machines are byte- addressable Hardware s memory composed of 8- bit storage cells, each has a unique address A C pointer is just abstracted memory address Type declaralon tells compiler how many bytes to fetch on each access through pointer E.g., 32- bit integer stored in 4 conseculve 8- bit bytes short *y! int *x! char *z! 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 Byte address 16- bit short stored in two bytes 32- bit integer stored in four bytes 8- bit character stored in one byte 24

sizeof() operator sizeof(type) returns number of bytes in object But number of bits in a byte is not standardized In olden Lmes, when dragons roamed the earth, bytes could be 5, 6, 7, 9 bits long By definilon, sizeof(char)==1 Can take sizeof(arr), or sizeof(struc>ype) We ll see more of sizeof when we look at dynamic memory management 25

Pointer ArithmeLc pointer + number pointer number e.g., pointer + 1 adds 1 something to a pointer char *p; char a; char b; p = &a; p += 1; In each, p now points to b (Assuming compiler doesn t reorder variables in memory. Never code like this!!!!) int *p; int a; int b; p = &a; p += 1; Adds 1*sizeof(char) to the memory address Adds 1*sizeof(int) to the memory address Pointer arithme)c should be used cau)ously 26

Arrays and Pointers Passing arrays: Array pointer to the inilal (0th) array element a[i] *(a+i) An array is passed to a funclon as a pointer The array size is lost! Usually bad style to interchange arrays and pointers Avoid pointer arithmelc! Really int *array int foo(int array[], { } int unsigned int size) array[size - 1] main(void) { } int a[10], b[5]; Must explicitly pass the size foo(a, 10) foo(b, 5) 27

Arrays and Pointers int foo(int array[], { } int unsigned int size) printf( %d\n, sizeof(array)); 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 28

Arrays and Pointers int i; int array[10]; int *p; int array[10]; for (i = 0; i < 10; i++) { array[i] = ; } for (p = array; p < &array[10]; p++) { *p = ; } These code sequences have the same effect! 29

Clickers/Peer InstrucLon Time int x[5] = { 2, 4, 6, 8, 10 };! int *p = x;! int **pp = &p;! (*pp)++;! (*(*pp))++;! printf("%d\n", *p);! Result is: A: 2 B: 3 C: 4 D: 5 E: None of the above 30

In the News (1/23/2015): Google Exposing Apple Security Bugs Google security published details of three bugs in Apple OS X (90 days ager privately nolfying Apple) One network stack problem fixed in Yosemite, all in next beta One is dereferencing a null pointer! One is zeroing wrong part of memory! Separately, Google announces it won t patch WebKit vulnerability affeclng Android 4.3 and below (only about 930 million aclve users) 31

Concise strlen() int strlen(char *s) { } char *p = s; while (*p++) ; /* Null body of while */ return (p s 1); What happens if there is no zero character at end of string? 32

Point past end of array? 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., not cause an error

Valid Pointer ArithmeLc 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 illegal since makes no sense: adding two pointers mullplying pointers subtract pointer from integer

Arguments in main()! To get arguments to the main funclon, use: int main(int argc, char *argv[])! What does this mean? argc contains the number of strings on the command line (the executable counts as one, plus one for each argument). Here argc is 2: unix% sort myfile argv is a pointer to an array containing the arguments as strings 35

Example! foo hello 87! argc = 3 /* number arguments */ argv[0] = "foo", argv[1] = "hello", argv[2] = "87"! Array of pointers to strings 36

And In Conclusion, Pointers are abstraclon of machine memory addresses Pointer variables are held in memory, and pointer values are just numbers that can be manipulated by sogware In C, close relalonship between array names and pointers Pointers know the type of the object they point to (except void *) Pointers are powerful but potenlally dangerous 37