CS 61C: Great Ideas in Computer Architecture Lecture 3: Pointers

Slides:



Advertisements
Similar presentations
Kernighan/Ritchie: Kelley/Pohl:
Advertisements

6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
CS 61C L03 C Arrays (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #3: C Pointers & Arrays
CS 61C L03 C Pointers (1)Garcia / Patterson Fall 2002 © UCB CS61C - Machine Structures Lecture 3 C pointers  Dan Garcia (
Adapted from Dr. Craig Chase, The University of Texas at Austin.
CS 61C L04 C Pointers (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C : Machine.
Pointers Applications
CS 61C L03 C Arrays (1) A Carle, Summer 2006 © UCB inst.eecs.berkeley.edu/~cs61c/ CS61C : Machine Structures Lecture #3: C Pointers & Arrays
CS61C L4 C Pointers (1) Chae, Summer 2008 © UCB Albert Chae Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #4 –C Strings,
CS 61C Great Ideas in Computer Architecture Lecture 3: Introduction to C, Part II Instructor: Sagar Karandikar
Arrays and Pointers in C Alan L. Cox
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
CS 61C: Great Ideas in Computer Architecture Introduction to C, Part II Instructors: John Wawrzynek & Vladimir Stojanovic
C Programming Day 4. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 More on Pointers Constant Pointers Two ways to.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Topic 4: C Data Structures CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
CS 61C: Great Ideas in Computer Architecture Introduction to C, Part II Instructors: Krste Asanovic & Vladimir Stojanovic
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Pointers. Pointer Fundamentals  When a variable is defined the compiler (linker/loader actually) allocates a real memory address for the variable. –int.
Instructor: Justin Hsia 6/20/2012Summer Lecture #31 CS 61C: Great Ideas in Computer Architecture C Arrays, Strings, More Pointers.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
CS 61C: Great Ideas in Computer Architecture C Pointers Instructors: Vladimir Stojanovic & Nicholas Weaver 1.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
CS61C L04 Introduction to C (pt 2) (1) Garcia, Spring 2010 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
CSE 220 – C Programming malloc, calloc, realloc.
Computers’ Basic Organization
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
The Machine Model Memory
C Primer.
Nicholas Weaver & Vladimir Stojanovic
Motivation and Overview
C Programming Tutorial – Part I
COSC 220 Computer Science II
CSE 303 Concepts and Tools for Software Development
CS 61C: Great Ideas in Computer Architecture Introduction to C
Student Book An Introduction
Programmazione I a.a. 2017/2018.
Instructor: Ioannis A. Vetsikas
Lecture 6 C++ Programming
Pointers and References
Introduction to Pointers
Introduction to Pointers
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Lecture 8b: Strings BJ Furman 15OCT2012.
CSCE Fall 2013 Prof. Jennifer L. Welch.
Pointers, Dynamic Data, and Reference Types
Lecture 18 Arrays and Pointer Arithmetic
Outline Defining and using Pointers Operations on pointers
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Pointers.
Chapter 16 Pointers and Arrays
CSCE Fall 2012 Prof. Jennifer L. Welch.
Initializing variables
Pointers Pointers point to memory locations
15213 C Primer 17 September 2002.
August 31 addresses Drop box Questions? 31 August 2004
Presentation transcript:

CS 61C: Great Ideas in Computer Architecture Lecture 3: Pointers Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c

Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic Strings, main And in Conclusion, … CS 61c Lecture 3: Pointers

Components of a Computer Processor Control Datapath Memory Enable? Read/Write Address Write Data ReadData Processor-Memory Interface Input Bytes Program PC Registers Arithmetic & Logic Unit (ALU) Data Output I/O-Memory Interfaces focus memory CS 61c Lecture 3: Pointers

Computer Memory int a; a = -85; printf(“%d”, a); Type Name Addr Value … 108 107 106 105 104 103 102 101 100 int a; a = -85; printf(“%d”, a); Do not confuse memory address and value. Nor a street address with the person living there. Big table of words (e.g. 32 bits each) Just like houses in a city, each word has an address, e.g. 380 Soda Hall, Berkeley, CA 94720 Computers use numbers as addresses Note: addresses and values are different Just like a house is not the same as its address (give me a house or an address?) CS 61c Lecture 3: Pointers

Pointers C speak for “memory addresses” Notation Type Name Addr Value … 108 107 106 105 104 103 102 101 100 C speak for “memory addresses” Notation int *x; // variable x is an address to an int int y = 9; // y is an int x = &y; // assign address of y to x // “address operator” int z = *x; // assign what x is pointing to to z // “dereference operator” *x = -7; // assign -7 to what x is pointing to What are the values of x, y, z? CS 61c Lecture 3: Pointers

Pointer Type Pointers have types, like other variables Examples: “type of object” the pointer is “pointing to” Examples: int *pi; // pointer to int double *pd; // pointer to double char *pc; // pointer to char CS 61c Lecture 3: Pointers

Generic Pointer (void *) Points to any object (int, double, …) Does not “know” type of object it references (e.g. compiler does not know) Example: void *vp; // vp holds an address to // object of ”arbitrary” type Applications Generic functions e.g. to allocate memory malloc, free accept and return pointers of any type see next lecture CS 61c Lecture 3: Pointers

Pointer to struct CS 61c Lecture 3: Pointers

Your Turn! Answer a b RED 3 -7 GREEN 4 5 ORANGE -4 YELLOW -2 Type Name Addr Value … 108 107 106 105 104 103 102 101 100 Answer a b RED 3 -7 GREEN 4 5 ORANGE -4 YELLOW -2 CS 61c

What’s wrong with this Code? Output: a = 1853161526, p = 0x7fff5be57c08, *p = 0 Values of unutilized variables is arbitrary Output is random, changes between executions (I tried this!) Worst case: uninitialized pointer “points” to illegal memory address (e.g. 0): fatal error, overwrite data, code, … Do not count on *p == 0 CS 61c Lecture 3: Pointers

Pointers as Function Arguments Type Name Addr Value … 108 107 106 105 104 103 102 101 100 C passes arguments by value i.e. it passes a copy value does not change outside function To pass by reference use a pointer a=1 b=-9 CS 61c Lecture 3: Pointers

Parameter Passing in Java “primitive types” (int, char, double) by value (i.e. passes a copy) Objects by reference (i.e. passes a pointer) Java uses pointers internally But hides them from the programmer Mapping of variables to addresses is not defined in Java language No address operator (&) Gives JVM flexibility to move stuff around CS 61c Lecture 3: Pointers

Your Turn! Answer a b c RED 5 3 1 GREEN ORANGE YELLOW Type Name Addr Value … 105 104 103 102 101 100 Answer a b c RED 5 3 1 GREEN ORANGE YELLOW CS 61c

Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic Strings, main And in Conclusion, … CS 61c Lecture 3: Pointers

C Arrays Declaration: Element access: Index of first element: 0 // allocate space // unknown content int a[5]; // allocate & initialize int b = { 3, 2, 1 }; Element access: b[1]; a[2] = 7; Index of first element: 0 Type Name Addr Value … 108 107 106 105 104 103 102 101 100 CS 61c Lecture 3: Pointers

Beware: no array bound checking! Often the result is much worse: erratic behavior segmentation fault, etc. C does not know array length! Pass as argument into functions Output: a[0] = 1 a[1] = 2 a[2] = 3 a[3] = -1870523725 CS 61c Lecture 3: Pointers

Use Constants, Not Literals Assign size to constant Bad pattern int i, ar[10]; for(i = 0; i < 10; i++){ ... } Better pattern const int ARRAY_SIZE = 10; int i, a[ARRAY_SIZE]; for(i = 0; i < ARRAY_SIZE; i++){ ... } “Single source of truth” Avoiding maintaining two copies of the number 10 And the chance of changing only one DRY: “Don’t Repeat Yourself” CS 61c Lecture 3: Pointers

Pointing to Different Size Objects Modern machines are “byte-addressable” Hardware’s memory composed of 8-bit storage cells, each has a unique address Type declaration tells compiler how many bytes to fetch on each access through pointer E.g., 32-bit integer stored in 4 consecutive 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 CS 61c Lecture 3: Pointers

sizeof() operator Output: double: 8 array: 20 s: 4 sizeof(type) Returns number of bytes in object Number of bits in a byte is not standardized All modern computers: 8 bits per byte Some “old” computers use other values, e.g. 6 bits per ”byte” By definition, in C sizeof(char)==1 For all other types result is hardware and compiler dependent Do not assume - Use sizeof! CS 61c Lecture 3: Pointers

Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic Strings, main And in Conclusion, … CS 61c Lecture 3: Pointers

So what did Dr. Moore Predict? Transistor* cost as a function of components per chip Minimum Shifts to right: As time passes, cost decreases provided we get more Fortunately we always had good ideas to use more: Computers Memory Smartphones Internet of Things? Why a minimum? If too small, some don’t work! Horizontal: # components Vertical: cost per component * Transistors: basic elements making up computers (see later) CS 61c Lecture 3: Pointers

Dr. Moore’s Vision (in 1965) Something useful that is getting always better and less expensive is good for Society Business CS 61c Lecture 3: Pointers

Why do people say Moore’s Law is over? CS 61c Lecture 3: Pointers

Fabs (where chips are made) $5-10B Final Four: Intel TSMC Samsung Global Foundries (was IBM) CS 61c 130nm 90nm Lecture 3: Pointers 65nm 40nm 28nm <=22nm

Break! 9/18/2018 Fall 2017 - Lecture #3

Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic Strings, main And in Conclusion, … CS 61c Lecture 3: Pointers

Pointer Arithmetic - char Type Name Byte Addr* Value … 108 107 106 105 104 103 102 101 100 *pc = b c = 0x7fff50f54b3e pc = 0x7fff50f54b3f pc-c = 1 CS 61c *Computer only uses byte addresses. Tables with blue headers are simplifications.

Pointer Arithmetic - int Type Name Byte Addr Value … 108 107 106 105 104 103 102 101 100 *pi = 20 i = 0x7fff50f54b40 pi = 0x7fff50f54b44 pi-i = 1 Each integer uses 4 bytes in memory! (draw box) CS 61c Lecture 3: Pointers

Array Name / Pointer Duality Array variable is a “pointer” to the first (0th) element Can use pointers to access array elements char *pstr and char astr[] are nearly identical declarations Differ in subtle ways: astr++ is illegal Consequences: astr is an array variable, but works like a pointer astr[0] is the same as *astr astr[2] is the same as *(astr+2) Can use pointer arithmetic to access array elements CS 61c Lecture 3: Pointers

Arrays versus Pointer Example Type Name Addr Value … 104 103 102 101 100 Output: a[1]=20, *(p+1)=20, p[2]=30 a[0]=11, a[1]=22, a[2]=33 Mixing pointer and array notation can be confusing  avoid. CS 61c Lecture 3: Pointers

Pointer Arithmetic Example: Use only for arrays. Never: int n = 3; int *p; p += n; // adds n*sizeof(int) to p p -= n; // subtracts n*sizeof(int) from p Use only for arrays. Never: char *p; char a, b; p = &a; p += 1; // may point to b, or not CS 61c Lecture 3: Pointers

Arrays and Pointers a[i]  *(a+i) 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 (# of bytes) is lost! Usually bad style to interchange arrays and pointers explicitly pass size Really int *array int foo(int array[], unsigned int size) { … array[size - 1] … } main(void) int a[10], b[5]; … foo(a, 10)… foo(b, 5) … CS 61c Lecture 3: Pointers

Arrays and Pointers What does this print? 8 foo(int array[], 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? 8 ... because array is really a pointer (and a pointer is architecture-dependent, but likely to be 8 on modern 64-bit machines!) What does this print? (provided sizeof(int)==4) 40 One difference is that you cannot reassign the name a to point to another object it that the compiler knows at compile time the size of the array, which {sizeof(int)*10}, which is why it prints 40 for a (on a computer where sizeof(int) = 4) CS 61c Lecture 3: Pointers

Arrays and Pointers These code sequences have the same effect: int i; int array[5]; for (i = 0; i < 5; i++) { array[i] = …; } Name Type Addr Value … 106 105 104 103 102 101 100 int *p; int array[5]; for (p = array; p < &array[5]; p++) { *p = …; } CS 61c Lecture 3: Pointers

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 const int SZ = 10; int ar[SZ], *p, *q, sum = 0; p = &ar[0]; q = &ar[SZ]; 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 CS 61c Lecture 3: Pointers

Valid Pointer Arithmetic Add/subtract an integer to/from a pointer Difference of 2 pointers (must both point to elements in same array) Compare pointers (<, <=, ==, !=, >, >=) Compare pointer to NULL (indicates that the pointer points to nothing) Everything makes no sense & is illegal: adding two pointers multiplying pointers subtract pointer from integer CS 61c Lecture 3: Pointers

Pointers to Pointers *p = 20 CS 61c Lecture 3: Pointers

Your Turn … int x[] = { 2, 4, 6, 8, 10 }; int *p = x; int **pp = &p; (*pp)++; (*(*pp))++; printf("%d\n", *p); Name Type Addr Value … 106 105 104 103 102 101 100 Answer RED 2 GREEN 3 ORANGE 4 YELLOW 5

Administrivia Homework 0 and Mini-bio will be released by tonight Lab swap policy is posted on Piazza and the website Guerrilla Session and mini-tutoring session details will be posted soon CS 61c Lecture 3: Pointers

Break! 9/18/2018 Fall 2017 - Lecture #3

Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic Strings, main And in Conclusion, … CS 61c Lecture 3: Pointers

C Strings C strings are null- terminated character arrays Type Name Byte Addr Value … 108 107 106 105 104 103 102 101 100 C strings are null- terminated character arrays char s[] = ”abc”; CS 61c Lecture 3: Pointers

String Example Output: str = abc, length = 3 CS 61c Lecture 3: Pointers

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? CS 61c Lecture 3: Pointers

Arguments in main() To get arguments to the main function, use: int main(int argc, char *argv[]) argc is the number of strings on the command line argv is a pointer to an array containing the arguments as strings CS 61c Lecture 3: Pointers

Example UNIX: $ gcc -o ex Argc.c $ ./ex -g a "d e f” arg[0] = ./ex arg[1] = -g arg[2] = a arg[3] = d e f CS 61c Lecture 3: Pointers

Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic Strings, main And in Conclusion, … CS 61c Lecture 3: Pointers

And in Conclusion, … Pointers are “C speak” for machine memory addresses Pointer variables are held in memory, and pointer values are just numbers that can be manipulated by software In C, close relationship between array names and pointers Pointers know the type & size of the object they point to (except void *) Like most things, pointers can be used for Pointers are powerful But, without good planning, a major source of errors Plenty of examples in the next lecture! CS 61c Lecture 3: Pointers