24
Lecture – Pointers 1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson and Introduction to C++ Programming by Roberge and Smith

Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson

Embed Size (px)

Citation preview

Page 1: Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson

Lecture – Pointers 1

C++ Pointers

Joseph Spring/Bob Dickerson

School of Computer Science

Operating Systems and Computer NetworksBased on notes by Bob Dickerson and Introduction to C++ Programming by Roberge and Smith

Page 2: Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson

Lecture – Pointers 2

Areas for Discussion

• Pointers: Introduction

• Declaring Pointers

• Dereferencing a Pointer

• Pointer Arithmetic

• The Address Operator

• Pointers and Arrays

• Dynamic Memory Allocation

• De-allocating Memory Allocation

Page 3: Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson

Lecture – Pointers 3

Pointers: Introduction

Page 4: Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson

Pointers: Introduction• Pointers

– Are variables that holds the memory address of a data value rather than the data value itself

– point to the data value– Are useful for:

• Manipulating array elements

• Processing strings

• Dynamically allocating system memory

– Are declared by preceding the identifier with the indirection operator *

Lecture – Pointers 4

Page 5: Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson

Lecture – Pointers 5

Declaring Pointers

Page 6: Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson

Declaring PointersThe declaration:

int *iptr;

creates a pointer iptr that can store the address of an integer

We note:– At this stage iptr does not contain an address– We have to assign an address to iptr– We can assign an address by using the address-

of operator &Lecture – Pointers 6

Page 7: Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson

Declaring Pointers

Example

int *iptr; //Declaring iptr as a pointer to an integer value

int num = 5; //num contains the integer value 5

iptr = #//iptr now points to num

To access the value pointed to by a pointer we

can use the indirection operator *

cout << *iptr << “\n”;

Lecture – Pointers 7

Page 8: Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson

Lecture – Pointers 8

Dereferencing Pointers

Page 9: Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson

Dereferencing a Pointercout << *iptr << “\n”;

Here the indirection operator is used to:– Output the integer value (5)– Stored in the memory location (num)– Pointed to by iptr

• Accessing a value pointed to by a pointer is called dereferencing a pointer

• The * operator is referred to as the dereferencing operator

Lecture – Pointers 9

Page 10: Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson

Lecture – Pointers 10

Assigning Pointers

Page 11: Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson

Assigning a Pointers contents to another Pointer• This maybe achieved by using the

assignment operator =

int *iptrv2; //iptrv2 is a pointer to an integer value

iptrv2 = iptr //iptrv2 points to what iptr points to (num)

• The above creates a second pointer iptrv2 to the integer variable num

Lecture – Pointers 11

Page 12: Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson

Lecture – Pointers 12

Pointers and Arrays

Page 13: Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson

Pointers and Arrays• Recall an array variable is a pointer to the

first element in an array

char section[11] = “A Title”, // sample string

*strptr; // pointer to a char in the string

strptr = section;

• Here both section and strptr point to the first character in the array

Lecture – Pointers 13

Page 14: Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson

Pointers and Arrays

‘A’

‘ ’

‘T’

‘i’

‘t’

‘l’

‘e’

‘\0’

Lecture – Pointers 14

section

strptr

Page 15: Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson

Pointer Arithmetic#include<iostream>

Using namespace std;

Void main() {

char section[11] = “A Title”, //sample string

*strptr; //pointer to character in string

//set strptr to point to the first character in string

strptr = section;

//display each character in the string. Stop when strptr points to the null

character at the end of the string

while(*strptr != ‘\0’) {

cout << *strptr; //output character that pointer strptr points to

strptr++; // Advance to the next character

}

cout << “\n”

}Lecture – Pointers 15

Page 16: Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson

Pointers and Arrays

‘A’

‘ ’

‘T’

‘i’

‘t’

‘l’

‘e’

‘\0’

Lecture – Pointers 16

section

strptr points to this memory locationafter third execution of statement strptr++

Page 17: Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson

Pointers and Arrays

‘A’

‘ ’

‘T’

‘i’

‘t’

‘l’

‘e’

‘\0’

Lecture – Pointers 17

section

Strptr points to this memory locationafter seventh execution of statement strptr++

Page 18: Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson

Lecture – Pointers 18

Dynamically Allocating Memory Storage

and

De-allocating Memory Storage

Page 19: Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson

Dynamically Allocating Storage• So far we allocate space for an array at compile time.

– For example:

const int MaxLength = 11;

int num1;

double list[500];

char inputstring[MaxLength];

• Signals to the compiler to reserve enough memory for:

• A pair of integers

• An array of 500 double prcision floating type numbers

• A string containing 11 characters

Lecture – Pointers 19

Page 20: Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson

Dynamically Allocating Storage• This approach to memory allocation is inefficient,

particularly for arrays and strings• It is often the case that

– the size of an array is not known until run-time

– The size varies and although its maximum size may be, say, 500 it could regularly be much smaller

• Rather than – specifying as much memory as one might need at compile time,

thereby regularly wasting lots of memory

– better to specify the array size at run-time and dynamically allocate memory

Lecture – Pointers 20

Page 21: Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson

Dynamically Allocating Storage

• We can dynamically allocate memory space for an

array at run-time by using the new operator

list = new double [listsize]

– Allocates an array containing listsize double precision

numbers

– Assigns list to point to the array where list is of type

double*

Lecture – Pointers 21

Page 22: Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson

dynamic.cpp – code fragment....

int listsize; //input list size

double *list; //pointer to dynamically allocated array

cout << “Enter the array size: ”; //get size of array needed

cin >> listsize;

list = new double[listsize]; //allocate an arrray of specified size

cout << “Enter the array elements: ”; //read in array elements

For(int i=0; i < listsize; i++)

cin >> list[i];

....

Lecture – Pointers 22

Page 23: Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson

De-allocating Storage

• When we no longer require dynamically allocated

memory we have to de-allocate it

• This is achieved using the delete operator

delete [ ] list;

– Note use of [ ] to indicate that list points to an array not a

single double precision number

Lecture – Pointers 23

Page 24: Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson

Lecture – Pointers 24

Summary

• Pointers: Introduction

• Declaring Pointers

• Dereferencing a Pointer

• Pointer Arithmetic

• The Address Operator

• Pointers and Arrays

• Dynamic Memory Allocation

• De-allocating Memory Allocation