SOW C++ CSO Chapter 09 9e
SOW C++ CSO Chapter 09 9e
Pointers
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
9.1
Getting the Address of a Variable
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Getting the Address of a
Variable
Each variable in program is stored at a
unique address
Use address operator & to get address of
a variable:
int num = -99;
cout << # // prints address
// in hexadecimal
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
9.2
Pointer Variables
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Pointer Variables
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Something Like Pointers: Arrays
We have already worked with something similar
to pointers, when we learned to pass arrays as
arguments to functions.
showValues(numbers, SIZE);
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Something Like Pointers : Arrays
The values parameter, in the showValues
function, points to the numbers array.
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Something Like Pointers:
Reference Variables
We have also worked with something like pointers
when we learned to use reference variables.
Suppose we have this function:
void getOrder(int &donuts)
{
cout << "How many doughnuts do you want? ";
cin >> donuts;
}
And we call it with this code:
int jellyDonuts;
getOrder(jellyDonuts);
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Something Like Pointers:
Reference Variables
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Pointer Variables
Pointer variables are yet another way using a
memory address to work with a piece of data.
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Pointer Variables
Definition:
int *intptr;
Read as:
“intptr can hold the address of an int”
Spacing in definition does not matter:
int * intptr; // same as above
int* intptr; // same as above
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Pointer Variables
Assigning an address to a pointer variable:
int *intptr;
intptr = #
Memory layout:
num intptr
25 0x4a00
address of num: 0x4a00
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Pointer Variables
Initialize pointer variables with the special
value nullptr.
In C++ 11, the nullptr key word was
introduced to represent the address 0.
Here is an example of how you define a
pointer variable and initialize it with the
value nullptr:
int *ptr = nullptr;
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
A Pointer Variable in Program 9-2
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
The Indirection Operator
The indirection operator (*) dereferences
a pointer.
It allows you to access the item that the
pointer points to.
int x = 25;
int *intptr = &x;
cout << *intptr << endl;
This prints 25.
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
The Indirection Operator in Program 9-3
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
The Indirection Operator in Program 9-3
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
9.3
The Relationship Between Arrays
and Pointers
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
The Relationship Between
Arrays and Pointers
Array name is starting address of array
int vals[] = {4, 7, 11};
4 7 11
starting address of vals: 0x4a00
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
The Relationship Between
Arrays and Pointers
Array name can be used as a pointer
constant:
int vals[] = {4, 7, 11};
cout << *vals; // displays 4
Pointer can be used as an array name:
int *valptr = vals;
cout << valptr[1]; // displays 7
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
The Array Name Being Dereferenced in Program 9-5
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Pointers in Expressions
Given:
int vals[]={4,7,11}, *valptr;
valptr = vals;
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Array Access
Array elements can be accessed in many ways:
Array access method Example
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Array Access
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
From Program 9-7
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
9.4
Pointer Arithmetic
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Pointer Arithmetic
Operations on pointer variables:
Operation Example
int vals[]={4,7,11};
int *valptr = vals;
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
From Program 9-9
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
9.5
Initializing Pointers
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Initializing Pointers
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
9.6
Comparing Pointers
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Comparing Pointers
Relational operators (<, >=, etc.) can be
used to compare addresses in pointers
Comparing addresses in pointers is not
the same as comparing contents pointed
at by pointers:
if (ptr1 == ptr2) // compares
//
addresses
if (*ptr1 == *ptr2) // compares
// contents
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
9.7
Pointers as Function Parameters
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Pointers as Function
Parameters
A pointer can be a parameter
Works like reference variable to allow change to
argument from within function
Requires:
1) asterisk * on parameter in prototype and heading
void getNum(int *ptr); // ptr is pointer to an int
2) asterisk * in body to dereference the pointer
cin >> *ptr;
3) address as argument to the function
getNum(&num); // pass address of num to getNum
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Example
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Pointers as Function Parameters in Program 9-11
(Program Continues)
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Pointers as Function Parameters in Program 9-11
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Pointers to Constants
• If we want to store the address of a
constant in a pointer, then we need to
store it in a pointer-to-const.
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Pointers to Constants
Example: Suppose we have the following
definitions:
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Pointers to Constants
Suppose we wish to pass the payRates array to
a function? Here's an example of how we can do
it.
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Constant Pointers
A constant pointer is a pointer that is
initialized with an address, and cannot
point to anything else.
Example
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Constant Pointers
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Constant Pointers to Constants
A constant pointer to a constant is:
a pointer that points to a constant
a pointer that cannot point to anything except
what it is pointing to
Example:
int value = 22;
const int * const ptr = &value;
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Constant Pointers to Constants
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
9.8
Dynamic Memory Allocation
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Dynamic Memory Allocation
Can allocate storage for a variable while
program is running
Computer returns address of newly
allocated variable
Uses new operator to allocate memory:
double *dptr = nullptr;
dptr = new double;
new returns address of memory location
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Dynamic Memory Allocation
Can also use new to allocate array:
const int SIZE = 25;
arrayPtr = new double[SIZE];
Can then use [] or pointer arithmetic to access array:
for(i = 0; i < SIZE; i++)
*arrayptr[i] = i * i;
or
for(i = 0; i < SIZE; i++)
*(arrayptr + i) = i * i;
Program will terminate if not enough memory available to
allocate
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Releasing Dynamic Memory
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Dynamic Memory Allocation in Program 9-14
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Dynamic Memory Allocation in Program 9-14
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Returning Pointers from
Functions
Pointer can be the return type of a function:
int* newNum();
The function must not return a pointer to a local
variable in the function.
A function should only return a pointer:
to data that was passed to the function as an
argument, or
to dynamically allocated memory
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
From Program 9-15
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
9.10
Using Smart Pointers to Avoid
Memory Leaks
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Using Smart Pointers to Avoid
Memory Leaks
In C++ 11, you can use smart pointers to dynamically
allocate memory and not worry about deleting the
memory when you are finished using it.
Three types of smart pointer:
unique_ptr
shared_ptr
weak_ptr
Must #include the memory header file:
#include <memory>
In this book, we introduce unique_ptr:
unique_ptr<int> ptr( new int );
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Using Smart Pointers to Avoid
Memory Leaks
The notation <int> indicates that the pointer can point to an int .
The name of the pointer is ptr .
The expression new int allocates a chunk of memory to hold an
int.
The address of the chunk of memory will be assigned to ptr.
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.
Using Smart Pointers in Program 9-17
Copyright © 2018, 2015, 2012, 2009 Pearson Education, Inc. All rights reserved.