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

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

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

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

Dynamic Allocation of Memory

Scope and Parameter Passing

Dynamic Allocation of Memory

Fundamental Data Types

List Iterator Implementation

Minimal Spanning Trees

Programming Languages

Scope and Parameter Passing

Scheduling and Digraphs

The Pairwise-Comparison Method

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

The Critical-Path Algorithm

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

Recursive Linked Lists

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

Recognition of Tokens

The x86 Instruction Set

Programming Languages

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

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

Friends and Unary Operators

Density Curves Sections

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

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

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

BITG 1113: POINTER LECTURE 12

Inheritance: The Fundamental Functions

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

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

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

MySQL Creating a Database Lecture 3

Solving Recursive Sequences by Iteration

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

Inheritance: The Fundamental Functions

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

The x86 Architecture

Binary Tree Applications

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

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

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

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

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

Recursive Descent Parsers

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

Magnification and Minification

The Decreasing-Time Algorithm

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

Ambient and Diffuse Light

The Traveling Salesman Problem Brute Force Method

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

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

Binary Tree Implementation

Stacks and their Applications

The Class Construct Part 2

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

What is an algorithm?

Sampling Distribution Examples Sections 15.4, 15.5

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

Building the Abstract Syntax Trees

LR Parsing - Conflicts

Abstract Syntax Trees Synthetic and Inherited Attributes

Rotations and Translations

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

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

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

Introduction to Compiler Design

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

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 Plurality-with-Elimination Method

The Class Construct Part 1

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

The Coefficient of Determination

The Graphics Pipeline

Binary Tree Implementation

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

Displaying Distributions - Quantitative Variables

The Graphics Pipeline

Pointers. Variable Declaration. Chapter 10

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

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

The x87 Floating-Point Unit

The Projection Matrix

Pointers, Dynamic Data, and Reference Types

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

Arrays, Pointers and Memory Management

The Standard Template Library Classes

The Normal Distribution

Shading Triangles. Lecture 37. Robb T. Koether. Hampden-Sydney College. Mon, Nov 30, 2015

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

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

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes

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

Transcription:

Pointers Lecture 2 Sections 10.3-10.8 Robb T. Koether Hampden-Sydney College Mon, Jan 20, 2014 Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 1 / 35

1 Endianness 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 Mon, Jan 20, 2014 2 / 35

Outline 1 Endianness 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 Mon, Jan 20, 2014 3 / 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 Mon, Jan 20, 2014 4 / 35

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) Pointers Mon, Jan 20, 2014 5 / 35

Example Example (Example) Endianness.cpp Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 6 / 35

Outline 1 Endianness 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 Mon, Jan 20, 2014 7 / 35

Outline 1 Endianness 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 Mon, Jan 20, 2014 8 / 35

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) Pointers Mon, Jan 20, 2014 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 Mon, Jan 20, 2014 10 / 35

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) Pointers Mon, Jan 20, 2014 11 / 35

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) Pointers Mon, Jan 20, 2014 12 / 35

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) Pointers Mon, Jan 20, 2014 13 / 35

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) Pointers Mon, Jan 20, 2014 14 / 35

Example Example (Example) Ptr Plus Int.cpp Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 15 / 35

Outline 1 Endianness 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 Mon, Jan 20, 2014 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 Mon, Jan 20, 2014 17 / 35

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

Pointer Subtraction Pointer Subtraction int* p; int* q; int a = q - p; p q - p = 5 q Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 19 / 35

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

Pointer Arithmetic Pointer Arithmetic 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 Mon, Jan 20, 2014 21 / 35

Pointer Arithmetic 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 Mon, Jan 20, 2014 22 / 35

Outline 1 Endianness 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 Mon, Jan 20, 2014 23 / 35

Pointers and Constants Pointers and Constants int const i = 123; int j = 456; int const * pi1 = &i; int * const pi2 = &j; int const * const pi3 = &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 Mon, Jan 20, 2014 24 / 35

An Array Name as a Pointer 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 Mon, Jan 20, 2014 25 / 35

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

Example Example (Example) Pointer Loop.cpp Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 27 / 35

Outline 1 Endianness 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 Mon, Jan 20, 2014 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 Mon, Jan 20, 2014 29 / 35

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

Example Example (Example) Pass By Pointer.cpp Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 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 Mon, Jan 20, 2014 32 / 35

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 Mon, Jan 20, 2014 33 / 35

Outline 1 Endianness 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 Mon, Jan 20, 2014 34 / 35

Assignment Homework Read Sections 10.3-10.8. Robb T. Koether (Hampden-Sydney College) Pointers Mon, Jan 20, 2014 35 / 35