ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers

Similar documents
ECE 2400 Computer Systems Programming Fall 2018 Topic 6: C Dynamic Allocation

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

Operating Systems 2INC0 C course Pointer Advanced. Dr. Ir. Ion Barosan

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

ECE 2400 Computer Systems Programming Fall 2017 Topic 12: Transition from C to C++

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

Computer Programming Lecture 12 Pointers

Variation of Pointers

ECE 2400 Computer Systems Programming Fall 2018 Topic 11: Transition to C++

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

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

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

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

Lecture 05 POINTERS 1

G Programming Languages - Fall 2012

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

C Pointers. 7.2 Pointer Variable Definitions and Initialization

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

Chapter 6 - Pointers

Slides adopted from T. Ferguson Spring 2016

ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C

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

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

ECE 2400 Computer Systems Programming Fall 2018 Topic 7: Concrete Data Types

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

ECE 2400 Computer Systems Programming Fall 2018 Topic 2: C Recursion

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

MYcsvtu Notes LECTURE 34. POINTERS

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

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

Chapter 7 C Pointers

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

Chapter 7. Pointers. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

CA31-1K DIS. Pointers. TA: You Lu

Pointers, Dynamic Data, and Reference Types

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

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

Pointer in C SHARDA UNIVERSITY. Presented By: Pushpendra K. Rajput Assistant Professor

SYSC 2006 C Winter 2012

Motivation was to facilitate development of systems software, especially OS development.

CS 137 Part 5. Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors. October 25th, 2017

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

Data Structure Series

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

C Pointers Pearson Education, Inc. All rights reserved.

Introduction to Scientific Computing and Problem Solving

Outline. Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples.

Data Structures and Algorithms for Engineers

Using pointers with functions

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

Data Structures and Algorithms for Engineers

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

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

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!

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Class Information ANNOUCEMENTS

PROGRAMMAZIONE I A.A. 2017/2018

Pointers and Output Parameters

Introduction to Data Structures. Systems Programming

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

Low-Level C Programming. Memory map Pointers Arrays Structures

Programming for Engineers Pointers

CS 61c: Great Ideas in Computer Architecture

C++ Programming Chapter 7 Pointers

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

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

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

CS 11 C track: lecture 5

EM108 Software Development for Engineers

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

ECE 15B COMPUTER ORGANIZATION

CSE 307: Principles of Programming Languages

Fast Introduction to Object Oriented Programming and C++

CMSC 341 Lecture 10 Binary Search Trees

CS240: Programming in C

CS132 Algorithm. Instructor: Jialiang Lu Office: Information Center 703

a data type is Types

BBM 201 DATA STRUCTURES

Procedural programming with C

Programming in C/C Lecture 2

Intro to C: Pointers and Arrays

Kurt Schmidt. October 30, 2018

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

COMsW Introduction to Computer Programming in C

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -STRUCTURE- SPRING 2015 SEON-JU AHN, CNU EE

Computer Systems Principles. C Pointers

Singly linked lists in C.

Dynamic Memory Allocation and Linked Lists

Intermediate Programming, Spring 2017*

Overview (1A) Young Won Lim 9/9/17

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

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Dynamic Memory Allocation

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

Dynamic memory. EECS 211 Winter 2019

Introduction to Pointers in C. Address operator. Pointers. Readings: CP:AMA 11, 17.7

REFERENCES, POINTERS AND STRUCTS

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

Transcription:

ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers School of Electrical and Computer Engineering Cornell University revision: 2017-09-23-11-06 1 Pointer Basics 2 2 Call by Value vs. Call by Reference 4 3 Mapping Conceptual Storage to Machine Memory 8 4 Pointers to Other Types 13 1

1. Pointer Basics Pointers are a way of referring to the location of a variable Pointers enable a variable s value to be a pointer to a completely different variable Programmers can access what a pointer points to and redirect the pointer to point to something else This is an example of indirection, a powerful programming concept Pointers can be confusing, but the key is to carefully construct the corresponding frame diagram 1. Pointer Basics Pointers require introducing new types and new operators Every type T has a corresponding pointer type T* A variable of type T* contains a pointer to a variable of type T 1 int* b_ptr; // pointer to a variable of type int 2 char* a_ptr; // pointer to a variable of type char 3 float* c_ptr; // pointer to a variable of type float The address-of operator (&) evaluates to the location of a variable The address-of operator is used to initialize/assign to pointers 1 int b; // variable of type int 2 int* b_ptr; // pointer to a variable of type int 3 b_ptr = &b; // assign location of b to b_ptr The dereference operator (*) evaluates to the value of the variable the pointer points to 1 int b = 42; // initialize variable of type int to 42 2 int* b_ptr = &b; // pointer to a variable of type int 3 int c = *b_ptr; // initialize c with what b_ptr points to 2

1. Pointer Basics Example declaring, initializing, RHS dereferencing pointers 1 int a = 3; 2 int* a_ptr; 3 a_ptr = &a; 4 5 int b = 2; 6 int c = b + (*a_ptr); Example illustrating aliasing 1 int a = 3; 2 int* a_ptr0 = &a; 3 int* a_ptr1 = a_ptr0; 4 int c = (*a_ptr0) + (*a_ptr1); Example declaring, initializing, LHS dereferencing pointers 1 int a = 3; 2 int b = 2; 3 4 int c; 5 int* c_ptr = &c; 6 *c_ptr = a + b; 3

2. Call by Value vs. Call by Reference Be careful three very different uses of the * symbol! Multiplication operator int a = b * c; Pointer type int* d = &a; Dereference operator int e = *d; 2. Call by Value vs. Call by Reference So far, we have always used call by value Call by value copies values into parameters Changes to parameters by callee are not seen by caller 1 void sort( int x, int y ) 2 { 3 if ( x > y ) { 4 int temp = x; 5 x = y; 6 y = temp; 7 } 8 } 9 10 int main( void ) 11 { 12 int a = 9; 13 int b = 5; 14 sort( a, b ); 15 return 0; 16 } 4

2. Call by Value vs. Call by Reference Call by reference uses pointers as parameters Callee can read and modify parameters by dereferencing pointers Changes to parameters by callee are seen by caller 1 void sort( int* x_ptr, 2 int* y_ptr ) 3 { 4 if ( (*x_ptr) > (*y_ptr) ) { 5 int temp = *x_ptr; 6 *x_ptr = *y_ptr; 7 *y_ptr = temp; 8 } 9 } 10 11 int main( void ) 12 { 13 int a = 9; 14 int b = 5; 15 sort( &a, &b ); 16 return 0; 17 } http://cpp.sh/537og 5

2. Call by Value vs. Call by Reference 1 void sort3( int* p_ptr, 2 int* q_ptr, 3 int* r_ptr ) 4 { 5 sort( p_ptr, q_ptr ); 6 sort( q_ptr, r_ptr ); 7 sort( p_ptr, 1_ptr ); 8 } 9 10 int main( void ) 11 { 12 int a = 9; 13 int b = 5; 14 int c = 3; 15 sort3( &a, &b, &c ); 16 return 0; 17 } http://cpp.sh/77j33 6

2. Call by Value vs. Call by Reference Draw frame diagram corresponding to the execution of this program 1 void avg( int* result_ptr, 2 int x, int y ) 3 { 4 int sum = x + y; 5 *result_ptr = sum / 2; 6 } 7 8 int main( void ) 9 { 10 int a = 10; 11 int b = 20; 12 int c; 13 avg( &c, a, b ); 14 return 0; 15 } 7

3. Mapping Conceptual Storage to Machine Memory 3. Mapping Conceptual Storage to Machine Memory Our current use of frame diagrams is conceptual Real machine uses memory to store variables Real machine does not use arrows, uses memory addresses Program Execution Arrow State Statement Statement Syntax Statement Semantics 8

3. Mapping Conceptual Storage to Machine Memory Can visualize memory using a byte or word view Stack stored at high addresses, grows down As a simplification, assume we only have 128 bytes of memory 1 int a = 3; 2 int* a_ptr; 3 a_ptr = &a; 4 5 int b = 2; 6 int c; 7 c = b + (*a_ptr); Memory (byte addr) Memory (4B word addr) 9

3. Mapping Conceptual Storage to Machine Memory Both code and are stored in 128 bytes of memory Stack stored at high addresses, grows down Code stored at low addresses, execution moves up Stack Frame: collection of data on the associated with function call including return value, return addr, parameters, local variables 1 void sort( int* x_ptr, int* y_ptr ) 2 { 3 if ( (*x_ptr) > (*y_ptr) ) { 4 int temp = *x_ptr; 5 *x_ptr = *y_ptr; 6 *y_ptr = temp; 7 } 8 } Memory (4B word addr) 9 10 int main( void ) 11 { 12 int a = 9; 13 int b = 5; 14 sort( &a, &b ); 15 return 0; 16 } 10

3. Mapping Conceptual Storage to Machine Memory 1 #include <stdio.h> 2 3 void sort( int* x_ptr, int* y_ptr ) 4 { 5 printf( "addr of x_ptr : %p (points to %p)\n", &x_ptr, x_ptr ); 6 printf( "addr of y_ptr : %p (points to %p)\n", &y_ptr, y_ptr ); 7 if ( (*x_ptr) > (*y_ptr) ) { 8 int temp = *x_ptr; 9 printf( "addr of temp : %p\n", &temp ); 10 *x_ptr = *y_ptr; 11 *y_ptr = temp; 12 } 13 } 14 15 int main( void ) 16 { 17 int a = 9; 18 int b = 5; 19 printf( "addr of a : %p\n", &a ); 20 printf( "addr of b : %p\n", &b ); 21 sort( &a, &b ); 22 return 0; 23 } http://cpp.sh/7ymyp Working with the raw pointer values is challenging, especially with address space layout randomization Real calling conventions are more complex that the simple one used in lecture 11

3. Mapping Conceptual Storage to Machine Memory Example frame for x86 Memory (4B word addresses) 12

4. Pointers to Other Types 4. Pointers to Other Types In addition to pointing to primitive types, pointers can also point to other pointers, to structs, or even functions. Pointers to struct Pointer to a struct is declared exactly as what we have already seen Be careful to dereference the pointer first, then access a field 1 typedef struct _node_t 2 { 3 int value; 4 struct _node_t* next_ptr; 5 } 6 node_t; 7 8 int main( void ) 9 { 10 // First node 11 node_t node0; 12 node0.value = 3; 13 14 node_t* node_ptr = &node0; 15 (*node_ptr).value = 4; 16 17 // Second node 18 node_t node1; 19 node1.value = 5; 20 node1.next_ptr = &node0; 21 22 node_ptr = &node1; 23 (*node_ptr).value = 6; 24 (*((*node_ptr).next_ptr)).value = 7; 25 26 return 0; 27 } 13

4. Pointers to Other Types C provides the arrow operator (->) as syntactic sugar a->b is equivalent to (*a).b 1 int main( void ) 2 { 3... 4 5 node_t* node_ptr = &node0; 6 node_ptr->value = 4; 7 8... 9 10 node_ptr = &node1; 11 node_ptr->value = 6; 12 node_ptr->next_ptr->value = 7; 13 } Pointers to Nothing NULL is defined in stdlib.h to be a pointer to nothing NULL can be used to indicate there is no answer or error Use flat-headed arrow to indicate NULL in frame diagrams In previous example, NULL can mean there is no next node 14

4. Pointers to Other Types 1 #include <stddef.h> 2 #include <stdio.h> 3 4 typdef struct _node_t 5 { 6 int value; 7 struct _node_t* next_ptr; 8 } 9 node_t; 10 11 int main( void ) 12 { 13 node_t node0; 14 node0.value = 3; 15 node0.next_ptr = NULL; 16 17 node_t node1; 18 node1.value = 4; 19 node1.next_ptr = &node0; 20 21 node_t node2; 22 node2.value = 5; 23 node2.next_ptr = &node1; 24 25 int sum = 0; 26 node_t* node_ptr = &node2; 27 while ( node_ptr!= NULL ) { 28 sum += node_ptr->value; 29 node_ptr = node_ptr->next_ptr; 30 } 31 return 0; 32 } 15

4. Pointers to Other Types Pointers to Pointers 1 int a = 3; 2 int* a_ptr = &a; 3 int** a_pptr = &a_ptr; 4 int*** a_ppptr = &a_pptr; 5 6 int b = ***a_ppptr + 1; Pointers to Functions Code is also stored in memory, so a function pointer points to code Enables passing a function as a parameter to a different function Consider the following functions: 1 int lt( int x, int y ) { return x < y; } 2 int gt( int x, int y ) { return x > y; } The type of a function is the function s signature A function signature includes the parameter types and return type We don t really care about the function or parameter names lt and gt have the same function signature and thus the same type lt and gt are essentially two values of the same type We can write the type of these functions as follows: 1 int ( int, int ) 16

4. Pointers to Other Types Recall that every type T has a corresponding pointer type T* So the type of a pointer to this function might look like this: 1 (int ( int, int ))*... and declaring a variable to hold a function pointer like this: 1 (int ( int, int ))* cmp_ptr; This makes sense and would be consistent, but C actually uses a slightly different syntax for declaring a function pointer: 1 int (*cmp_ptr) ( int, int ); This also makes sense since now there is a direct connection between a function declaration and a function pointer: 1 int lt ( int x, int y ); 2 int (*cmp_ptr) ( int, int ); The type of a function pointer is complex, so use a typedef 1 typedef int (*cmp_ptr_t) ( int, int ); 2 cmp_ptr_t cmp_ptr; The address-of operator (&) applied to a function name evaluates to a pointer to that function 1 typedef int (*cmp_ptr_t) ( int, int ); 2 cmp_ptr_t cmp_ptr = > We can dereference a function pointer and use the call operator (()) to call a function via function pointer 1 typedef int (*cmp_ptr_t) ( int, int ); 2 cmp_ptr_t cmp_ptr = > 3 int result = (*cmp_ptr)( 3, 4 ); 17

4. Pointers to Other Types 1 #include <stdio.h> 2 3 int lt( int x, int y ) { return x < y; } 4 int gt( int x, int y ) { return x > y; } 5 6 typedef int (*cmp_ptr_t) ( int, int ); 7 8 void sort( int* x_ptr, int* y_ptr, cmp_ptr_t cmp_ptr ) 9 { 10 // Dereference function pointer, then call function 11 if ( (*cmp_ptr)( (*x_ptr), (*y_ptr) ) ) { 12 int temp = *x_ptr; 13 *x_ptr = *y_ptr; 14 *y_ptr = temp; 15 } 16 } 17 18 void sort3( int* x_ptr, int* y_ptr, int* z_ptr, 19 cmp_ptr_t cmp_ptr ) 20 { 21 sort( x_ptr, y_ptr, cmp_ptr ); 22 sort( y_ptr, z_ptr, cmp_ptr ); 23 sort( x_ptr, y_ptr, cmp_ptr ); 24 } 25 26 int main( void ) 27 { 28 int a = 9; int b = 5; int c = 3; 29 30 sort3( &a, &b, &c, &gt ); 31 printf(" %d < %d < %d \n", a, b, c ); 32 33 sort3( &a, &b, &c, &lt ); 34 printf(" %d > %d > %d \n", a, b, c ); 35 36 return 0; 37 } http://cpp.sh/36nqu 18