Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Similar documents
Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Dynamic Allocation of Memory

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Dynamic Allocation of Memory

List Iterator Implementation

Scope and Parameter Passing

Scope and Parameter Passing

Recursive Linked Lists

Fundamental Data Types

Minimal Spanning Trees

Programming Languages

Friends and Unary Operators

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

The Critical-Path Algorithm

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Programming Languages

Pointers (1A) Young Won Lim 1/9/18

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

Pointers (1A) Young Won Lim 1/5/18

Magnification and Minification

The x86 Architecture

The Traveling Salesman Problem Brute Force Method

Density Curves Sections

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Homework #3 CS2255 Fall 2012

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Implementing Linked Lists

LR Parsing - Conflicts

Scheduling and Digraphs

The Pairwise-Comparison Method

BITG 1113: POINTER LECTURE 12

Stacks and their Applications

Inheritance: The Fundamental Functions

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

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

Recognition of Tokens

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

The Traveling Salesman Problem Nearest-Neighbor Algorithm

The x86 Instruction Set

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

The Class Construct Part 1

The Plurality-with-Elimination Method

Solving Recursive Sequences by Iteration

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

The Graphics Pipeline

Binary Tree Applications

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

The Constructors. Lecture 6 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 26, 2018

Pointers (1A) Young Won Lim 3/5/18

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Recursive Descent Parsers

The Decreasing-Time Algorithm

Pointers (1A) Young Won Lim 1/14/18

Pointers (1A) Young Won Lim 2/6/18

List Iterators. Lecture 34 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 24, 2013

Pointers (1A) Young Won Lim 2/10/18

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

MySQL Creating a Database Lecture 3

What is an algorithm?

Sampling Distribution Examples Sections 15.4, 15.5

The x87 Floating-Point Unit

Building the Abstract Syntax Trees

Rotations and Translations

Pointers (1A) Young Won Lim 1/22/18

Inheritance: The Fundamental Functions

Function Usage. Lecture 15 Sections 6.3, 6.4. Robb T. Koether. Hampden-Sydney College. Mon, Oct 1, 2018

The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Introduction to Compiler Design

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):

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

The Coefficient of Determination

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

Displaying Distributions - Quantitative Variables

Applying Textures. Lecture 27. Robb T. Koether. Hampden-Sydney College. Fri, Nov 3, 2017

The Graphics Pipeline

LR Parsing - The Items

Binary Tree Implementation

Pointers. Variable Declaration. Chapter 10

The Projection Matrix

Ambient and Diffuse Light

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

XQuery FLOWR Expressions Lecture 35

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

CUP. Lecture 18 CUP User s Manual (online) Robb T. Koether. Hampden-Sydney College. Fri, Feb 27, 2015

The Class Construct Part 2

The Projection Matrix

Abstract Syntax Trees Synthetic and Inherited Attributes

Pointers, Dynamic Data, and Reference Types

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

Arrays, Pointers and Memory Management

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

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

Basic PHP Lecture 17

The Standard Template Library Classes

The Normal Distribution

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

Relational Databases

Transcription:

Pointers Lecture 2 Sections 10.3-10.8 Robb T. Koether Hampden-Sydney College Fri, Jan 18, 2013 Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 1 / 35

1 Introduction 2 Pointer Arithmetic Pointer + int Pointer - Pointer 3 Pointers and Arrays 4 Pointers as Function Parameters 5 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 2 / 35

Outline 1 Introduction 2 Pointer Arithmetic Pointer + int Pointer - Pointer 3 Pointers and Arrays 4 Pointers as Function Parameters 5 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 3 / 35

Big Endian and Little Endian Suppose we make the assignment int i = 0x25116634; If the architecture is Big Endian, then it is stored as "Big" end first 25 11 66 34 If the architecture is Little Endian, then it is stored as "Little" end first 34 66 11 25 Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 4 / 35

Big Endian and Little Endian Definition (Big Endian, Little Endian) If a processor has Big Endian architecture, then it stores the highest-order byte of the value in the lowest byte address. If a processor has Little Endian architecture, then it stores the lowest-order byte of the value in the lowest byte address. Multi-byte objects are addressed by their lowest byte address. Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 5 / 35

Example Example (Example) Endianness.cpp Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 6 / 35

Outline 1 Introduction 2 Pointer Arithmetic Pointer + int Pointer - Pointer 3 Pointers and Arrays 4 Pointers as Function Parameters 5 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 7 / 35

Outline 1 Introduction 2 Pointer Arithmetic Pointer + int Pointer - Pointer 3 Pointers and Arrays 4 Pointers as Function Parameters 5 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 8 / 35

Pointer + int It is permissible to add an integer to a pointer. int i = 123; int* p = &i; int* q = p + 1; The integer is multiplied by the size of the object pointed to. That value is added to the address. The result is a pointer of the same type. Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 9 / 35

Pointer + int The integer is interpreted as the number of objects of that type between the original address and the new address. The resulting address is interpreted as a pointer to the object located the specified number of objects to the right, as in an array. Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 10 / 35

Pointer Arithmetic int i = 123; // Addr of i is 0x0100 int* p = &i; // p = 0x0100 char* q = (char*)&i; // q = 0x0100 short* r = (short*)&i; // r = 0x0100 0x0100 (i) Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 11 / 35

Pointer Arithmetic int i = 123; // Addr of i is 0x0100 int* p = &i; // p = 0x0100 char* q = (char*)&i; // q = 0x0100 short* r = (short*)&i; // r = 0x0100 p - 1 p p + 1 p + 2 0x00fc 0x0100 (i) 0x0104 0x0108 Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 12 / 35

Pointer Arithmetic int i = 123; // Addr of i is 0x0100 int* p = &i; // p = 0x0100 char* q = (char*)&i; // q = 0x0100 short* r = (short*)&i; // r = 0x0100 q - 1 q q + 1 q + 2 0x00fc 0x0100 (i) 0x0104 0x0108 Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 13 / 35

Pointer Arithmetic int i = 123; // Addr of i is 0x0100 int* p = &i; // p = 0x0100 char* q = (char*)&i; // q = 0x0100 short* r = (short*)&i; // r = 0x0100 r - 1 r r + 1 r + 2 0x00fc 0x0100 (i) 0x0104 0x0108 Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 14 / 35

Example Example (Example) Ptr Plus Int.cpp Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 15 / 35

Outline 1 Introduction 2 Pointer Arithmetic Pointer + int Pointer - Pointer 3 Pointers and Arrays 4 Pointers as Function Parameters 5 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 16 / 35

Pointer Subtraction Subtraction of one pointer from another pointer (of the same type) is permitted. Take the difference of the addresses. Divide it by the size of the object pointed to. The result is an int. Interpret the result as the number of objects of that type between two addresses. Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 17 / 35

Pointer Subtraction Example (Pointer Subtraction) int* p; int* q; int a = q - p; p q Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 18 / 35

Pointer Subtraction Example (Pointer Subtraction) int* p; int* q; int a = q - p; p q - p = 5 q Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 19 / 35

Example Example (Example) Ptr Minus Ptr.cpp Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 20 / 35

Pointer Arithmetic Example int a = (p2 - p1) + 5; int b = p2 - (p1-5); int c = (p2 + 5) - p1; These are legal operations. Are they equivalent? Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 21 / 35

Pointer Arithmetic Example (Illegal Addition) int* mid = (p2 + p1)/2; Pointer addition is illegal. How can we obtain a pointer to the object halfway between p1 and p2? Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 22 / 35

Outline 1 Introduction 2 Pointer Arithmetic Pointer + int Pointer - Pointer 3 Pointers and Arrays 4 Pointers as Function Parameters 5 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 23 / 35

Pointers and Constants Example (Pointers and Constants) int const i = 123; int j = 456; int const * iptr = &i; int * const iptr2 = &j; int const * const iptr3 = &i; A pointer may point to a constant the object pointed to cannot be changed. A pointer itself may be constant the pointer cannot be changed. In fact, a constant pointer may point to a constant object! Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 24 / 35

An Array Name as a Pointer Example (Pointers and Arrays) int a[3] = {10, 20, 30}; int* iptr = a; cout << *iptr << endl; cout << *(iptr + 1) << endl; cout << *(iptr + 2) << endl; An array name represents a pointer that points to the first member of the array. The array name is a constant pointer; its value cannot be changed. An array name may be assigned to a pointer of the same type. Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 25 / 35

A Pointer as an Array Name Example (Pointers and Arrays) int a[3] = {10, 20, 30}; int* iptr = a; cout << iptr[0] << endl; cout << iptr[1] << endl; cout << iptr[2] << endl; A pointer name may be indexed. p[i] is equivalent to *(p + i). Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 26 / 35

Example Example (Example) Pointer Loop.cpp Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 27 / 35

Outline 1 Introduction 2 Pointer Arithmetic Pointer + int Pointer - Pointer 3 Pointers and Arrays 4 Pointers as Function Parameters 5 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 28 / 35

Pointers as Function Parameters In CS I, we learn to pass objects to function by reference. The intention is to allow the object to be modified by the function. Before pass-by-reference was introduced, the same was accomplished by passing a pointer. To access the object form within the function, the pointer parameter must be dereferenced. Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 29 / 35

Example Example (Pointer Parameter) void swap(int* a, int* b) { int temp = *a; *a = *b; *b = temp; return; } Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 30 / 35

Example Example (Example) Pass By Pointer.cpp Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 31 / 35

Arrays as Function Parameters When an array is passed as a parameter, the name of the array is passed, as a pointer. Thus, the function receives a pointer to the first element of the array. This is far more efficient than copying the entire array. Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 32 / 35

Example Example (Array Parameter) void sort(int list[], int size) { for (int i = 0; i < size - 1; i++) { int* q = list; while (q < list + size - 1) { if (*q > *(q + 1)) swap(q, q + 1); q++; } } return; } Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 33 / 35

Outline 1 Introduction 2 Pointer Arithmetic Pointer + int Pointer - Pointer 3 Pointers and Arrays 4 Pointers as Function Parameters 5 Assignment Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 34 / 35

Assignment Homework Read Sections 10.3-10.8. Robb T. Koether (Hampden-Sydney College) Pointers Fri, Jan 18, 2013 35 / 35