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

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

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

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

Dynamic Allocation of Memory

Dynamic Allocation of Memory

Minimal Spanning Trees

Scope and Parameter Passing

The Critical-Path Algorithm

List Iterator Implementation

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

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

Scope and Parameter Passing

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

Implementing Linked Lists

Fundamental Data Types

Binary Tree Applications

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

Density Curves Sections

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

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

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

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

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

Programming Languages

Programming Languages

The Decreasing-Time Algorithm

Building the Abstract Syntax Trees

Recursive Descent Parsers

Friends and Unary Operators

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

Homework #3 CS2255 Fall 2012

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

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

Inheritance: The Fundamental Functions

Recursive Linked Lists

Sampling Distribution Examples Sections 15.4, 15.5

Scheduling and Digraphs

Rotations and Translations

The Pairwise-Comparison Method

BITG 1113: POINTER LECTURE 12

Introduction to Compiler Design

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

The Graphics Pipeline

The Plurality-with-Elimination Method

MySQL Creating a Database Lecture 3

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

Recognition of Tokens

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

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

Solving Recursive Sequences by Iteration

Stacks and their Applications

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

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

Pointers as Arguments

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

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

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

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

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

The Traveling Salesman Problem Brute Force Method

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

[0569] p 0318 garbage

The Graphics Pipeline

The Model Stack. Lecture 8. Robb T. Koether. Hampden-Sydney College. Wed, Sep 6, 2017

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

Insertions, Deletions, and Updates

The x86 Instruction Set

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

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

The Projection Matrix

The x86 Architecture

Street-Routing Problems

LR Parsing - Conflicts

CS31 Discussion 1E Spring 17 : week 08

Pointers, Dynamic Data, and Reference Types

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

The Coefficient of Determination

Pointers, Arrays and C-Strings

Operators. Lecture 3 COP 3014 Spring January 16, 2018

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

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

Displaying Distributions - Quantitative Variables

The x87 Floating-Point Unit

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

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

Specular Reflection. Lecture 19. Robb T. Koether. Hampden-Sydney College. Wed, Oct 4, 2017

Abstract Syntax Trees Synthetic and Inherited Attributes

The Standard Template Library Classes

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

The Class Construct Part 1

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Function Definition Syntax Tree

Memory, Data, & Addressing II CSE 351 Spring

Nondeterministic Programming in C++

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

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

Arrays, Pointers and Memory Management

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

Magnification and Minification

Transcription:

Pointer Arithmetic Lecture 4 Chapter 10 Robb T. Koether Hampden-Sydney College Wed, Jan 25, 2017 Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 1 / 36

1 Pointer Arithmetic Pointer + int Pointer - Pointer 2 Pointers and Arrays 3 Pointer Compatibility 4 Endianness 5 Assignment Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 2 / 36

Outline 1 Pointer Arithmetic Pointer + int Pointer - Pointer 2 Pointers and Arrays 3 Pointer Compatibility 4 Endianness 5 Assignment Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 3 / 36

Outline 1 Pointer Arithmetic Pointer + int Pointer - Pointer 2 Pointers and Arrays 3 Pointer Compatibility 4 Endianness 5 Assignment Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 4 / 36

Pointer + int Pointer + int int i = 123; int* p = &i; int* q = p + 1; p++; It is permissible to add an integer to a pointer. 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) Pointer Arithmetic Wed, Jan 25, 2017 5 / 36

Pointer + int When an integer is added to a pointer, the integer is interpreted as the number of objects of that type from the original address to the new address. The result is a pointer that points to the object at the new address. Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 6 / 36

Pointer Arithmetic 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) Pointer Arithmetic Wed, Jan 25, 2017 7 / 36

Pointer Arithmetic 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) Pointer Arithmetic Wed, Jan 25, 2017 8 / 36

Pointer Arithmetic 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) Pointer Arithmetic Wed, Jan 25, 2017 9 / 36

Pointer Arithmetic 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) Pointer Arithmetic Wed, Jan 25, 2017 10 / 36

Example Example (Example) PtrPlusInt.cpp Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 11 / 36

Outline 1 Pointer Arithmetic Pointer + int Pointer - Pointer 2 Pointers and Arrays 3 Pointer Compatibility 4 Endianness 5 Assignment Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 12 / 36

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 from the first address to the second address. Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 13 / 36

Pointer Subtraction Pointer Subtraction int* p; int* q; int a = q - p; p q Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 14 / 36

Pointer Subtraction Pointer Subtraction int* p; int* q; int a = q - p; p q - p = 5 q Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 15 / 36

Example Example (Example) PtrMinusPtr.cpp Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 16 / 36

Pointer Arithmetic Pointer Arithmetic int a = (ptr2 - ptr1) + 5; int b = ptr2 - (ptr1-5); int c = (ptr2 + 5) - ptr1; These are legal operations. Are they equivalent? Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 17 / 36

Pointer Arithmetic Illegal Addition int* mid = (ptr2 + ptr1)/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) Pointer Arithmetic Wed, Jan 25, 2017 18 / 36

Outline 1 Pointer Arithmetic Pointer + int Pointer - Pointer 2 Pointers and Arrays 3 Pointer Compatibility 4 Endianness 5 Assignment Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 19 / 36

Pointers and Constants Pointers and Constants int i = 456; int const j = 123; int const * ptr1 = &j; int * const ptr2 = &i; int const * const ptr3 = &j; 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) Pointer Arithmetic Wed, Jan 25, 2017 20 / 36

An Array Name as a Pointer Pointers and Arrays int a[3] = {10, 20, 30}; // a is int* const int* ptr = a; // ptr is int* cout << *ptr << endl; cout << *(ptr + 1) << endl; cout << *(ptr + 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) Pointer Arithmetic Wed, Jan 25, 2017 21 / 36

A Pointer as an Array Name Pointers and Arrays int a[3] = {10, 20, 30}; int* ptr = a; cout << ptr[0] << endl; cout << ptr[1] << endl; cout << ptr[2] << endl; A pointer name may be indexed. ptr[i] is equivalent to *(ptr + i). Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 22 / 36

Example Example (Example) PointerLoop.cpp Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 23 / 36

Outline 1 Pointer Arithmetic Pointer + int Pointer - Pointer 2 Pointers and Arrays 3 Pointer Compatibility 4 Endianness 5 Assignment Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 24 / 36

Pointers to void A pointer to void, i.e., a void* object, is a pointer that does not point to any particular kind of object. There is no such thing as a void object. void is treated as a neutral type for situations where the type does not matter or is unknown. A void pointer may hold the address of any type of object. A void pointer may not be dereferenced. Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 25 / 36

Assignment of Pointers The assignment ptr = NULL is valid for any type of pointer. The pointer assignment ptr1 = ptr2 is legal if and only if ptr1 and ptr2 point to the same type of object, or ptr1 is a pointer to void. In all other cases, we would have to cast p2 to be compatible with ptr1. Assigning pointers to different types can cause serious problems. Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 26 / 36

Pointer Compatibility Pointer Compatibility int i = 123; float f = 4.56; int* ptri = &i; float* ptrf = &f; // Swap ptri and ptrf (and run for cover) void* temp = ptri; ptri = (int*)ptrf; ptrf = (float*)ptri; What are the values of *ptri and *ptrf? Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 27 / 36

Pointer Compatibility &i ptri &f ptrf 0x0000007b 0x4091eb85 i = 123 f = 4.56 Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 28 / 36

Pointer Compatibility &i ptri &f ptrf 0x0000007b 0x4091eb85 i = 123 f = 4.56 Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 29 / 36

Example Example (Example) PtrSwap.cpp Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 30 / 36

Outline 1 Pointer Arithmetic Pointer + int Pointer - Pointer 2 Pointers and Arrays 3 Pointer Compatibility 4 Endianness 5 Assignment Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 31 / 36

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) Pointer Arithmetic Wed, Jan 25, 2017 32 / 36

Big Endian and Little Endian Suppose we make the assignment int i = 1000000000; It so happens that 1000000000 10 = 3B9ACA00 16. If the architecture is Big Endian, then it is stored as "Big" end first 3B 9A CA 00 If the architecture is Little Endian, then it is stored as "Little" end first 00 CA 9A 3B Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 33 / 36

Example Example (Example) Endianness.cpp Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 34 / 36

Outline 1 Pointer Arithmetic Pointer + int Pointer - Pointer 2 Pointers and Arrays 3 Pointer Compatibility 4 Endianness 5 Assignment Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 35 / 36

Assignment Assignment Read Chapter 10. Robb T. Koether (Hampden-Sydney College) Pointer Arithmetic Wed, Jan 25, 2017 36 / 36