Tutorial 6. Memory Addresses and Pointers. scanf() function. Perils of Pointers. Function Pointers. Declarations vs. Definitions

Similar documents
Tutorial 5. Call Stack and Stack Frames. Memory Addresses and Pointers. Content vs Address of Pointers. scanf() function. Perils of Pointers

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

Tutorial 5. Overflow. Data Types. Structures. Call stack. Stack frames. Debugging Tips. CS 136 Winter 2019 Tutorial 5 1

Midterm Review. Short Answer. Short Answer. Practice material from the Winter 2014 midterm. Will cover some (but not all) of the questions.

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

CS 31 Discussion 1A, Week 4. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

CMPE-013/L. Introduction to C Programming. Unit testing

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

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

Storage class and Scope:

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

a data type is Types

CSCI 2132 Software Development. Lecture 18: Functions

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

CS24 Week 3 Lecture 1

Wednesday, October 15, 14. Functions

C Model: Memory & Control Flow

Programming in C++: Assignment Week 1

/* EXAMPLE 1 */ #include<stdio.h> int main() { float i=10, *j; void *k; k=&i; j=k; printf("%f\n", *j);

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

Chapter 5. Section 5.1 Introduction to Strings. CS 50 Hathairat Rattanasook

Intermediate Programming, Spring 2017*

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

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

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

Constants, References

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

Slides adopted from T. Ferguson Spring 2016

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

Chapter 11 Introduction to Programming in C

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

Chapter 11 Introduction to Programming in C

Intro to C: Pointers and Arrays

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Chapter 11 Introduction to Programming in C

Functions CMPE-013/L. Functions. Functions What is a function? Functions Remember Algebra Class? Gabriel Hugh Elkaim Winter 2014.

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

APS105. Pointers. Memory RAM 11/5/2013. Pointers. Need a way to refer to locations of things. a pointer: Address. Example: In C

Chapter 11 Introduction to Programming in C

UNIT III (PART-II) & UNIT IV(PART-I)

CS24 Week 4 Lecture 1

Pointers. Mr. Ovass Shafi (Assistant Professor) Department of Computer Applications

Pointers, Dynamic Data, and Reference Types

Dynamic Memory & ADTs in C. The heap. Readings: CP:AMA 17.1, 17.2, 17.3, The primary goal of this section is to be able to use dynamic memory.

Dynamic Memory & ADTs in C

G52CPP C++ Programming Lecture 9

CS31 Discussion 1E Spring 17 : week 08

High Performance Programming Programming in C part 1

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

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

CS240: Programming in C

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Chapter 11 Introduction to Programming in C

Functions. Arash Rafiey. September 26, 2017

BSM540 Basics of C Language

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

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

CS107, Lecture 9 C Generics Function Pointers

CGS 3460 Summer 07 Midterm Exam

COMP 2355 Introduction to Systems Programming

Computer Programming Lecture 12 Pointers

REFERENCES, POINTERS AND STRUCTS

Recitation 7 Caches and Blocking. 9 October 2017

C++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes

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

Scope: Global and Local. Concept of Scope of Variable

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

printf("this program adds the value 10 to a given integer number.\n\n");

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

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

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

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

C programming basics T3-1 -

Programming. Functions and File I/O

A brief intro to pointers for the purposes of passing reference parameters

CS 33. Introduction to C. Part 4. CS33 Intro to Computer Systems IV 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

CSE 333 Lecture 7 - final C details

CS16 Week 2 Part 2. Kyle Dewey. Thursday, July 5, 12

Chapter 14. Dynamic Data Structures. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved.

Variables, Pointers, and Arrays

Programming Studio #9 ECE 190

CMPE-013/L. Introduction to C Programming

CMPT 379 Compilers. Anoop Sarkar. 11/13/07 1. TAC: Intermediate Representation. Language + Machine Independent TAC

Chapter 11: Compiler II: Code Generation

Programming Language B

More File Operations. Lecture 17 COP 3014 Spring april 18, 2018

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

DAY 3. CS3600, Northeastern University. Alan Mislove

Type checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Chapter 4. Section 4.4 Passing Parameters to Functions. CS 50 Hathairat Rattanasook

Lecture 2: C Programm

Pointers and Functions Passing Pointers to Functions CMPE-013/L. Pointers and Functions. Pointers and Functions Passing Pointers to Functions

Transcription:

Tutorial 6 Memory Addresses and Pointers scanf() function Perils of Pointers Function Pointers Declarations vs. Definitions CS 136 Spring 2018 Tutorial 6 1

Memory Addresses and Pointers A pointer can be declared by placing an indirection operator (*) before the identifier. C requires you to specify the type of the value stored at an address. int i = 42; int *p = &i; Note that in this example we are declaring the variable named p of the type int *. You can get the starting address of where the value of an identifier is stored in memory by using the address operator (&). CS 136 Spring 2018 Tutorial 6 2

Memory Addresses and Pointers To get the value of what a pointer "points at", we use the indirection operator (*). This is often known as the deference operator and it is the inverse of the address operator (&). // To print the value p is pointing at printf("the value of i is: %d", *p); CS 136 Spring 2018 Tutorial 6 3

Pointers as Function Parameters Allows functions to mutate variables that live outside the function. Allows functions to avoid copying large structures. CS 136 Spring 2018 Tutorial 6 4

Example: Mutating Values in Functions Compare these two segments of code void inc(int p) { p += 1; } int main(void) { int x = 42; inc(x); printf("%d\n", x); } Output: 42 void inc(int *p) { *p += 1; } int main(void) { int x = 42; inc(&x); printf("%d\n", x); } Output: 43 CS 136 Spring 2018 Tutorial 6 5

Example: Call Stacks with Pointers Trace the content of the Call Stack for the following program 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 *i = *j; 4 *j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } 12 13 int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } CS 136 Spring 2018 Tutorial 6 6

Example: Call Stacks with Pointers 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 *i = *j; 4 *j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } 12 13 int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } p1: p2:.x = 3.y = 4.x = 5.y = 6 CS 136 Spring 2018 Tutorial 6 7

Example: Call Stacks with Pointers 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 *i = *j; 4 *j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } 12 13 int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } swap_posn: p1: addr_1 p2: addr_2 return address: 16 p1: [addr_1].x = 3.y = 4 p2: [addr_2].x = 5.y = 6 CS 136 Spring 2018 Tutorial 6 8

Example: Call Stacks with Pointers 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 *i = *j; 4 *j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } 12 13 int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } swap_int: i: addr_3 j: addr_4 temp:? return address: swap_posn:9 swap_posn: p1: addr_1 p2: addr_2 return address: 16 p1: [addr_1].x = 3 [addr_3].y = 4 p2: [addr_2].x = 5 [addr_4].y = 6 CS 136 Spring 2018 Tutorial 6 9

Example: Call Stacks with Pointers 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 * i = *j; 4 *j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } 12 13 int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } swap_int: i: addr_3 j: addr_4 temp: 3 return address: swap_posn:9 swap_posn: p1: addr_1 p2: addr_2 return address: 16 p1: [addr_1].x = 3 [addr_3].y = 4 p2: [addr_2].x = 5 [addr_4].y = 6 CS 136 Spring 2018 Tutorial 6 10

Example: Call Stacks with Pointers 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 *i = *j; 4 * j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } 12 13 int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } swap_int: i: addr_3 j: addr_4 temp: 3 return address: swap_posn:9 swap_posn: p1: addr_1 p2: addr_2 return address: 16 p1: [addr_1].x = 5 [addr_3].y = 4 p2: [addr_2].x = 5 [addr_4].y = 6 CS 136 Spring 2018 Tutorial 6 11

Example: Call Stacks with Pointers 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 *i = *j; 4 *j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } 12 13 int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } swap_posn: p1: addr_1 p2: addr_2 return address: 16 p1: [addr_1].x = 5 [addr_3].y = 4 p2: [addr_2].x = 3 [addr_4].y = 6 CS 136 Spring 2018 Tutorial 6 12

Example: Call Stacks with Pointers 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 *i = *j; 4 *j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } 12 13 int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } swap_posn: p1: addr_1 p2: addr_2 return address: 16 p1: [addr_1].x = 5 [addr_3].y = 4 p2: [addr_2].x = 3 [addr_4].y = 6 CS 136 Spring 2018 Tutorial 6 13

Example: Call Stacks with Pointers 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 *i = *j; 4 *j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } 12 13 int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } swap_int: i: addr_5 j: addr_6 temp:? return address: swap_posn:10 swap_posn: p1: addr_1 p2: addr_2 return address: 16 p1: [addr_1].x = 5 [addr_3].y = 4 [addr_5] p2: [addr_2].x = 3 [addr_4].y = 6 [addr_6] CS 136 Spring 2018 Tutorial 6 14

Example: Call Stacks with Pointers 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 * i = *j; 4 *j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } 12 13 int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } swap_int: i: addr_5 j: addr_6 temp: 4 return address: swap_posn:10 swap_posn: p1: addr_1 p2: addr_2 return address: 16 p1: [addr_1].x = 5 [addr_3].y = 4 [addr_5] p2: [addr_2].x = 3 [addr_4].y = 6 [addr_6] CS 136 Spring 2018 Tutorial 6 15

Example: Call Stacks with Pointers 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 *i = *j; 4 * j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } 12 13 int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } swap_int: i: addr_5 j: addr_6 temp: 4 return address: swap_posn:10 swap_posn: p1: addr_1 p2: addr_2 return address: 16 p1: [addr_1].x = 5 [addr_3].y = 6 [addr_5] p2: [addr_2].x = 3 [addr_4].y = 6 [addr_6] CS 136 Spring 2018 Tutorial 6 16

Example: Call Stacks with Pointers 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 *i = *j; 4 *j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } 12 13 int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } swap_posn: p1: addr_1 p2: addr_2 return address: 16 p1: [addr_1].x = 5 [addr_3].y = 6 [addr_5] p2: [addr_2].x = 3 [addr_4].y = 4 [addr_6] CS 136 Spring 2018 Tutorial 6 17

Example: Call Stacks with Pointers 1 void swap_int(int *i, int *j) { 2 int temp = *i; 3 *i = *j; 4 *j = temp; 5 } 6 7 void swap_posn(struct posn *p1, 8 9 struct posn *p2) { swap_int(&(p1->x), &(p2->x)); 10 swap_int(&(p1->y), &(p2->y)); 11 } 12 13 int main(void) { 14 struct posn p1 = {3, 4}; 15 struct posn p2 = {5, 6}; 16 swap_posn(&p1, &p2); 17 } End of Stack Trace p1: [addr_1].x = 5 [addr_3].y = 6 [addr_5] p2: [addr_2].x = 3 [addr_4].y = 4 [addr_6] CS 136 Spring 2018 Tutorial 6 18

scanf() function Reads data and stores it based on the parameter format. Returns the number of values that have been successfully read. It returns EOF if it hits the end of the input. For example: (See Seashell) char char1; char char2; printf("enter a character:"); scanf("%c", &char1); // can read whitespace printf("the input is %c\n", char1); scanf(" %c", &char2); // ignores whitespace printf("the input is %c\n", char2); CS 136 Spring 2018 Tutorial 6 19

Perils of Pointers: Expired Scopes What will this program output when foo runs? When it doesn t run? // Get a bad address int *weird(int a) { return &a; } int main(void) { int p_content; int *p = weird(5); // #JUSTFOOTHINGS void foo(void) { int x = 1; } } //foo(); p_content = *p; printf("%d\n", p_content); CS 136 Spring 2018 Tutorial 6 20

Example: Perils of Pointers: Stack Trace Trace the content of the Call Stack for the following program 1 int *foo(int a) { 2 return &a; 3 } 4 5 int main(void) { 6 int x = 42; 7 int *p = foo(x); 8 printf("%d\n", *p); 9 } CS 136 Spring 2018 Tutorial 6 21

Example: Perils of Pointers: Stack Trace 1 int *foo(int a) { 2 return &a; 3 } 4 5 int main(void) { 6 int x = 42; 7 int *p = foo(x); 8 printf("%d\n", *p); 9 } x: 42 p:? CS 136 Spring 2018 Tutorial 6 22

Example: Perils of Pointers: Stack Trace 1 int *foo(int a) { 2 return &a; 3 } 4 5 int main(void) { 6 int x = 42; 7 int *p = foo(x); 8 printf("%d\n", *p); 9 } foo: a: 42 [addr] return address: 7 x: 42 p:? CS 136 Spring 2018 Tutorial 6 23

Example: Perils of Pointers: Stack Trace 1 int *foo(int a) { 2 return &a; 3 } 4 5 int main(void) { 6 int x = 42; 7 int *p = foo(x); 8 printf("%d\n", *p); 9 } End of Stack Trace x: 42 p: addr CS 136 Spring 2018 Tutorial 6 24

Example: Perils of Pointers: Stack Trace x: 42 p: addr Notice that the stack frame for main has a pointer to addr. However, the stack frame which contains the contents of addr has been popped. Accessing the contents of this memory will result in undefined behaviour. CS 136 Spring 2018 Tutorial 6 25

Function Pointers Functions are not first-class values in C. Function pointers, on the other hand, are. A function pointer stores the (starting) address of a function, which is an address in the code section of memory. The syntax to declare a function pointer with name fpname is: return_type (*fpname)(param1_type, param2_type,...) A function pointer can only point to a function that already exists. CS 136 Spring 2018 Tutorial 6 26

Problem: find_max Implement the function find_max(fp, lower, upper) on Seashell. The function does the following: // find_max(fp, lower, upper) returns the // maximum value of fp(x) in the range // [lower, upper] // requires: upper > lower CS 136 Spring 2018 Tutorial 6 27

Declarations vs. Definitions // Declaration: bool is_impossible(int power_level); // Definition: bool is_impossible(int power_level) { return (power_level > 9000); } CS 136 Spring 2018 Tutorial 6 28

Why? Reusability. Maintainability. Abstraction. CS 136 Spring 2018 Tutorial 6 29