CS 2461: Computer Architecture I

Similar documents
Chapter 16 Pointers and Arrays

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Pointers and Arrays

Chapter 16 Pointers and Arrays

n Address of a variable in memory n Allows us to indirectly access variables ! Array n A list of values arranged sequentially in memory

Pointers and Arrays A QUICK PREVIEW OF FOR CHAPTERS 10 AND 11 CMPE13. Cyrus Bazeghi

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

Why do we need Pointers? Call by Value vs. Call by Reference in detail Implementing Arrays Buffer Overflow / The Stack Hack

[0569] p 0318 garbage

SYSC 2006 C Winter 2012

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

Introduction to Scientific Computing and Problem Solving

211: Computer Architecture Summer 2016

Pointers. Memory. void foo() { }//return

Homework #3 CS2255 Fall 2012

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

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

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

C++ for Java Programmers

Midterm 1 Review. Introduction to Programming in C. General. Turing Machine. Universal Turing Machine

CS 222: Pointers and Manual Memory Management

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

Arrays and Pointers in C. Alan L. Cox

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

Textbook chapter 10. Abstract data structures are. In this section, we will talk about. The array The stack Arithmetic using a stack

12. Pointers Address-of operator (&)

Computer Programming Lecture 12 Pointers

Reference operator (&)

Outline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Memory, Arrays & Pointers

Lab 3. Pointers Programming Lab (Using C) XU Silei

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

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

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers. Kevin Webb Swarthmore College March 1, 2016

What have we learned about when we learned about function parameters? 1-1

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #33 Pointer Arithmetic

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

MYcsvtu Notes LECTURE 34. POINTERS

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Object oriented programming C++

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

EM108 Software Development for Engineers

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

Chapter 12 Variables and Operators

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

C++ Programming Chapter 7 Pointers

Principles of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Chapter-11 POINTERS. Important 3 Marks. Introduction: Memory Utilization of Pointer: Pointer:

More about BOOLEAN issues

CS162 - POINTERS. Lecture: Pointers and Dynamic Memory


KOM3191 Object Oriented Programming Dr Muharrem Mercimek ARRAYS ~ VECTORS. KOM3191 Object-Oriented Computer Programming

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

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

CS 61c: Great Ideas in Computer Architecture

CPSC 213. Introduction to Computer Systems. Static Scalars and Arrays. Unit 1b

Pointers. Reference operator (&) ted = &andy;

Intermediate Programming, Spring 2017*

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

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Slides adopted from T. Ferguson Spring 2016

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

CS107 Handout 13 Spring 2008 April 18, 2008 Computer Architecture: Take II

Language comparison. C has pointers. Java has references. C++ has pointers and references

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Pointers and Arrays 1

Goals of this Lecture

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

Week 7: Pointers and Arrays. BJ Furman 02OCT2009

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

Introduction to C: Pointers

Lecture 04 Introduction to pointers

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Introduction to Programming Using Java (98-388)

Pointers, Dynamic Data, and Reference Types

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

Arrays, Strings, & Pointers

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

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

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

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

What is an algorithm?

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 6: Pointers

Chapter 14 Functions

Exercise 3 / Ch.7. Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways!

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

Transcription:

Next: Pointers, Arrays, Structs... : Computer Architecture I The real fun stuff in C.. Pointers and Arrays Read Chapters 16, 18 of text Functions, Arrays, Pointers Dynamic data structures Allocating space during run-time Read chapter 19 of text Pointers and Arrays Passing by value is not enough Pointer Address of a variable in memory Allows us to indirectly access variables in other words, we can talk about its address rather than its value Array A list of values arranged sequentially in memory Example: a list of telephone numbers Expression a[] refers to the 5th element of the array a Parameters are passed by value Arguments pushed onto run-time stack Example 2 (test2.c): you ve seen this: function that's supposed to swap the values of its arguments. 1

Executing the Swap Function Executing the Swap Function R6 before call firstval secondval valueb valuea void Swap(int firstval, int secondval) int tempval = firstval; firstval = secondval; secondval = tempval; Swap R6 firstval secondval valueb valuea main after call tempval firstval secondval valueb valuea Executing the Swap Function Arrays R6 before call Swap R6 firstval secondval valueb valuea main after call tempval firstval secondval valueb valuea These values changed... Swap needs addresses of variables outside its own activation record....but these did not. What are arrays? a collection of many variables of the same type with an index Ex: int my_array[10] ; // declaration LC-: allocates 10 slots for 16-bit integers in Data Memory These are stored in consecutive locations in memory my_array Just a label for memory location x000 Address x000 x001 x002 x008 x009 Contents On LC-: 10 16-bit slots Note: can t assume initialized to 0! 2

Arrays Pointers Indexing Arrays C offers indexing capability on array variables Ex: In this example: my_array[2] equals Allocates 10 slots for 16-bit integers in Data Memory What happens when you type: my_array [11]??? Address Contents my_array x000 x001 Offset of 2 x002 from start: my_array x008 x009 Remember the offset?: LDR RD, RS, Offset Imagine: LDR R0, my_array, #2 On LC-: 10 16-bit slots Note: can t assume initialized to 0! What are pointers? a variable that contains the address of a memory location Ex: int *my_ptr ; // declaration Declares a variable called my_ptr that contains address of an int. The asterisks: * tells compiler this isn t an integer variable It is a variable that will hold the address of an integer! We know this from Assembly: R0 can hold address of a slot in data memory Example of use: int a=0 ; // declares a regular integer variable int *b ; // declares a pointer to an integer var. b=&a ; // finds address of a, assigns it to b *b=5 ; // dereferences b, sets value of a=5 Pointers Pointers Example of use: int a=0 ; // declares a regular integer variable int *b ; // declares a pointer to an integer var. b=&a ; // finds address of a, assigns it to b *b=5 ; // dereferences b, sets value of a=5 Address Contents x000 (a) 05 x001 (b) x000 Dereferencing fancy word for: contents at address Dereferencing pointer b means: get contents of memory at the address b is pointing to Two language mechanisms for supporting pointers in C * : for dereferencing a pointer & : for getting the address of a variable For your future knowledge: * : called the Indirection or Dereference operator & : called the Address Operator These unary operators are called Pointer Operators Note: There is a difference between pointer operators and declaring pointer variables: int * my_pointer ; int * in this context is a type not the use of the operator * Confused? Chapter 16 in Patt/Patel is outstanding!

Arrays and pointers Pointers and Arrays Arrays and pointers are intimately connected in C Array declarations allocate areas of memory for use We are really defining an address (aka a pointer) to the first element of the array Example mixing arrays and pointers! int my_array[10]; // declares array of 10 ints int *my_ptr; // declares a pointer to an int var. my_ptr = my_array + 2; // points to rd row in array my_array my_ptr=x002 Address Contents x000 x001 x002 x008 x009 Dereferencing ptr: *my_ptr equals Pointers and arrays are intimately related In terms of assembly we can make a distinction between the address of the start of a block of memory and the values stored in that block of memory C Code Assembly Code int my_array[10] int *my_ptr; my_ptr = my_array; my_array.blkw #10 LEA R0, my_array ; R0 is equiv to ; my_ptr Pointer Arithmetic Pointers/Arrays/Strings Just as we used arithmetic on address values to iterate through arrays in assembly, we can use arithmetic on pointer values in C float my_array[10]; // declares array of 10 floats float *my_ptr; // declares a pointer to a float v my_ptr = my_array + 2; // points to rd row in array my_ptr = my_ptr + 1; // points to th row in array Compiler looks at the type of variable being pointed to and increments by the correct amount to point to the next element In this case ptr may actually be incremented by since each float takes up bytes There is no string datatype in C But we can use arrays of char s to mimic behavior Simplest Ways to Declare Strings : char my_string [256] ; Works just like any array, each element is character my_string[0]= T ; my_string[1]= h ; You must null terminate this array Note: no way to know length of an array Unless one loops through it entirely and determines ending Pass my_string as argument to functions! That s the 1 st address of the string in memory char *my_string = This is a string ; Will be null terminated Cannot be modified

Exercise 1.test your pointer skills! Go to lectures page, download test.c do NOT run the code Read through code and write down (and submit along with Exercise 2 in a while) answers to the questions Next, run the code and check your answers Pointers int i; int *ip; Pointer: Variable that contains address of another variable. Operators: *p returns value pointed to by p &z returns address of variable z A pointer is a data object which is separate from what it points to. Pointer to a data type ip is a pointer to an integer Example and C to LC translation int i; int *ptr; store the value into the memory location associated with i i = ; store the address of i into the memory location associated with ptr ptr = &i; *ptr = *ptr + 1; store the result into memory at the address stored in ptr read the contents of memory at the address stored in ptr Example: LC- Code ; i is 1st local (offset 0), ptr is 2nd (offset -1) ; i = ; AND R0, R0, #0 ; clear R0 ADD R0, R0, # ; put in R0 STR R0, R5, #0 ; store in i ; ptr = &i;??? ; get addr of i??? ; store in ptr ; *ptr = *ptr + 1;??? ; get ptr??? ; dereference/load contents (*ptr)??? ; add one??? ; store result where ptr points 5

Pointers as Arguments Swap Passing a pointer into a function allows the function to read/change memory outside its activation record. void NewSwap(int *firstval, int *secondval) int tempval = *firstval; *firstval = *secondval; *secondval = tempval; Arguments are integer pointers. Caller passes addresses of variables that it wants function to change. a function that will swap two integers Last try: void swap(int *a, int *b) int t; t = *a; *a = *b; *b = t; Now it works Trace We call it like this int x = 2; int y = 8; swap(&x, &y); int x = 2; int y = 8; swap(&x, &y); void swap(int *a, int *b) int t; t = *a; *a = *b; *b = t; y 8 x 2 stack Stack Frame for main 6

Trace Trace int x = 2; int y = 8; swap(&x, &y); int x = 2; int y = 8; swap(&x, &y); void swap(int *a, int *b) int t; t = *a; *a = *b; *b = t; t b a y 8 x 2 Stack Frame for swap void swap(int *a, int *b) int t; t = *a; *a = *b; *b = t; t 2 b a y 8 x 2 Stack Frame for swap stack stack Trace Trace int x = 2; int y = 8; swap(&x, &y); int x = 2; int y = 8; swap(&x, &y); void swap(int *a, int *b) int t; t = *a; *a = *b; *b = t; t 2 b a y 8 x 8 Stack Frame for swap void swap(int *a, int *b) int t; t = *a; *a = *b; *b = t; t 2 b a y 2 x 8 Stack Frame for swap stack stack 7

Trace Passing Pointers int x = 2; int y = 8; swap(&x, &y); How do you pass pointers in the activation record? Using LC compiler void swap(int *a, int *b) int t; t = *a; *a = *b; *b = t; y 2 x 8 stack Stack Frame for main Parameters to the function are the addresses of the arguments! Passing Pointers to a Function Pointers main() wants to swap the values of valuea and valueb passes the addresses to NewSwap: NewSwap(&valueA, &valueb); R6 Code for passing arguments: ADD R0, R5, #-1 ; addr of valueb R5 ADD R6, R6, #-1 ; push STR R0, R6, #0 ADD R0, R5, #0 ; addr of valuea ADD R6, R6, #-1 ; push STR R0, R6, #0 xeffa xeff9 tempval firstval secondval valueb valuea xeffd Powerful and dangerous No runtime checking (for efficiency) Bad reputation Java attempts to remove the features of pointers that cause many of the problems hence the decision to call them references No address of operators No dereferencing operator (always dereferencing) No pointer arithmetic 8

Arrays Array Syntax How do we allocate a group of memory locations? Declaration type variable[num_elements]; all array elements are of the same type number of elements must be known at compile-time Array Reference variable[index]; i-th element of array (starting with zero); no limit checking at compile-time or run-time Arrays: Memory layout Arrays int ia[6]; Allocates consecutive spaces for 6 integers How much space is allocated? int ia[6]; Allocates consecutive spaces for 6 integers How much space is allocated? 6 * sizeof(int) Also creates ia which is effectively a constant pointer to the first of the six integers What does ia[] mean? ia 9

Arrays sizeof int ia[6]; Allocates consecutive spaces for 6 integers How much space is allocated? 6 * sizeof(int) Also creates ia which is effectively a constant pointer to the first of the six integers What does ia[] mean? Multiply by sizeof(int). Add to ia and dereference yielding: Compile time operator Two forms sizeof object sizeof ( type name ) Returns the size of the object or the size of objects of type name in bytes ia ia[] Note: Parentheses can be used in the first form with no adverse effects sizeof Arrays if sizeof(int) == then sizeof(i) == On a typical 2 bit machine... sizeof(*ip) Not the same thing!!! sizeof(ip) char *cp; sizeof(char) 1 sizeof(*cp) 1 sizeof(cp) int ia[6]; ia ia[] means *(ia + ) int ia[6]; sizeof(ia) 2 10

Pointer Arithmetic Arrays Note on the previous slide when we added the literal to a pointer it actually gets interpreted to mean * sizeof(thing being pointed at) int ia[6]; 0 1 2 5 This is why pointers have associated with them what they are pointing at! ia Array elements are numbered like this since that's how the pointer arithmetic works out! Pointer Arithmetic Address calculations depend on size of elements In our LC- code, we've been assuming one word per element. e.g., to find th element, we add to base address It's ok, because we've only shown code for int and char, both of which take up one word. If double, we'd have to add 8 to find address of th element. C does size calculations under the covers, depending on size of item being pointed to: double x[10]; double *y = x; *(y + ) = 1; allocates 20 words (2 per element) Relationship between Arrays and Pointers An array name is essentially a pointer to the first element in the array char word[10]; char *cptr; cptr = word;/* points to word[0] */ Difference: Can change the contents of cptr, as in cptr = cptr + 1; (The identifier "word" is not a variable.) same as x[] -- base address plus 6 11

Passing Arrays as Arguments Array as a Local Variable C passes arrays by reference the address of the array (i.e., of the first element) is written to the function's activation record otherwise, would have to copy each element main() int numbers[ma_nums]; This must be a constant, e.g., #define MA_NUMS 10 mean = Average(numbers); int Average(int inputvalues[ma_nums]) for (index = 0; index < MA_NUMS; index++) sum = sum + inputvalues[index]; return (sum / MA_NUMS); int foo(int myarray[]) int grid[10]; Array as a Local Variable LC- Code for Array References Array elements are allocated as part of the activation record. int grid[10]; First element (grid[0]) is at lowest address of allocated space. If grid is first variable allocated, then R5 will point to grid[9]. grid[0] grid[1] grid[2] grid[] grid[] grid[5] grid[6] grid[7] grid[8] grid[9] ; x = grid[] + 1??? ; where is grid[0]??? ; R1 = grid[]??? ; plus 1 STR??? ; store into x R5 x grid[0] grid[1] grid[2] grid[] grid[] grid[5] grid[6] grid[7] grid[8] grid[9] 12

Common Pitfalls with Arrays in C Recall Overrun array limits There is no checking at run-time or compile-time to see whether reference is within array bounds. int array[10]; int i; for (i = 0; i <= 10; i++) array[i] = 0; Declaration with variable size Size of array must be known at compile time. void SomeFunction(int num_elements) int temp[num_elements]; int ia[6]; ia[2] = 2; Address calculation: 2 * sizeof(*ia) + ia Access is by dereferencing *(2 * sizeof(*ia) + ia) 2 Remember! You don't type in the sizeof part! What happens? Stack Smashing int ia[6]; ia[8] = 8; Address calculation: 8 * sizeof(*ia) + ia 2 8 Remember! You don't type in the sizeof part! int another(int a, int b) int x[]; x[0] x[1] x[2] x[] Old FP Return Addr Return Val a b FP 1

Stack Smashing int another(int a, int b) int x[]; x[0] x[1] x[2] x[] x[] Old FP x[5] Ret Addr x[6] Ret Val x[7] a x[8] b FP Multi-Dimensional Arrays Declaration How does a two dimensional array work? int ia[][]; Number of Columns Number of Rows 0 1 2 0 1 2 Type Address Declaration at compile time i.e. size must be known How would you store it? 1

0 1 2 0 1 2 Column Major Order How would you store it? Advantage Using Row Major Order allows visualization as an array of arrays ia[1] 0,0 1,0 2,0 0,1 1,1 2,1 0,2 1,2 2,2 0, 1, 2, Column 0 Column 1 Column 2 Column 0,0 0,1 0,2 0, 1,0 1,1 1,2 1, 2,0 2,1 2,2 2, Row Major Order 0,0 0,1 0,2 0, 1,0 1,1 1,2 1, 2,0 2,1 2,2 2, ia[1][2] 0,0 0,1 0,2 0, 1,0 1,1 1,2 1, 2,0 2,1 2,2 2, Row 0 Row 1 Row 2 Element Access Element Access Given a row and a column index How to calculate location? To skip over required number of rows: row_index * sizeof(row) row_index * Number_of_columns * sizeof(arr_type) This plus address of array gives address of first element of desired row Add column_index * sizeof(arr_type) to get actual desired element Element_Address = Array_Address + Row_Index * Num_Columns * Sizeof(Arr_Type) + Column_Index * Sizeof(Arr_Type) Element_Address = 0,0 0,1 0,2 0, 1,0 1,1 1,2 1, 2,0 2,1 2,2 2, Array_Address + (Row_Index * Num_Columns + Column_Index) * Sizeof(Arr_Type) 15

What if array is stored in Column Major Order? How does C store arrays Element_Address = Array_Address + (Column_Index * Num_Rows + Row_Index) * Sizeof(Arr_Type) 0,0 1,0 2,0 0,1 1,1 2,1 0,2 1,2 2,2 0, 1, 2, Row major Pointer arithmetic stays unmodified Remember this.. Affects how well your program does when you access memory Now think about Now think about A D array A D array int a int a[5] 16

Now think about Now think about A D array A D array int a[][5] int a[][][5] Offset to a[i][j][k]? Next: Data Structures A D array int a[][][5] [slices][rows][columns] Programs are solving a real world problem Entities in the real world are real objects that need to be represented using some data structure With specific attributes Objects may be a collection of basic data types In C we call this a structure offset = (i * rows * columns) + (j * columns) + k 17