22
Pointers Michael Heron

CPP08 - Pointers

Embed Size (px)

DESCRIPTION

This is an introductory lecture on C++, suitable for first year computing students or those doing a conversion masters degree at postgraduate level.

Citation preview

Page 1: CPP08 - Pointers

PointersMichael Heron

Page 2: CPP08 - Pointers

Introduction• One of the trickiest things in C++ to get your heads around is

the topic of pointers.• I want to introduce this subject early so it doesn’t become a huge

problem down the line.• We don’t need to do an awful lot of pointer work at the

moment.• We’ll need to do more in the future.

• Pointers eventually become second nature.• Honest

Page 3: CPP08 - Pointers

Cube Calculator#include <iostream>

using namespace std;

void calculate_cube (int num) { num = num * num * num;}int main() { int num = 5;

calculate_cube(num);

cout << "Cube is " << num << endl; return 1;}

Page 4: CPP08 - Pointers

Cube Calculator• What will the output of this program be?• We create a variable• We pass that variable to our function• We make that variable equal the cube of itself.

• It seems like the answer should be 125…• … but it won’t be.• Why?

Page 5: CPP08 - Pointers

Functions and Variables• We have spoken about functions in an earlier lecture.• And how we can provide information to functions by passing

parameters.• When we do this, what we pass into the function is a copy of

the data we have.• So we really have two versions of the same data in the program

at any one time.

Page 6: CPP08 - Pointers

Functions and Variables• In our calculate_cube function, we are working with a copy of

num.• We are never directly manipulating the value of num itself.

• This is known as passing by value.• We pass the value of a variable, not the variable itself.

• Every variable in C++ is stored in the computer’s memory.• Sometimes we want to be able to get to that memory location.

• We want to pass things by reference.

Page 7: CPP08 - Pointers

Pointers• Here is where the pointer enters our discussion.• It points to a memory location.

• Pointers come with two new syntax elements to learn.• & which is the reference operator• * which is the dereference operator.

• We use these to swap between the memory location and the value of a memory location.

Page 8: CPP08 - Pointers

Reference Operator• The & symbol is our reference operator.• You can think of it as meaning ‘the address of’

• We can use this to say ‘I want to send a memory location into this function’:

int main() { int num = 5;

calculate_cube(&num);

cout << "Cube is " << num << endl; return 1;}

Page 9: CPP08 - Pointers

Dereference Operator• * is the dereference operator• Think of it as ‘the value of’

• We also use this to indicate that a variable is going to be a pointer.

• When we accept the parameter we are given, we want to be able to indicate we are working with a pointer.

• We need to change our function a little to accommodate that:

Page 10: CPP08 - Pointers

Our Program With Pointers#include <iostream>

using namespace std;

void calculate_cube (int *num) { *num = *num * *num * *num;}int main() { int num = 5;

calculate_cube(&num);

cout << "Cube is " << num << endl; return 1;}

Page 11: CPP08 - Pointers

How Do They Work?• Every variable takes up a certain amount of memory.• This varies from type to type.

• Each variable has a memory address.• That’s how the computer knows where to find it in memory.

• A pointer is a location in memory that contains the memory address of another location.• This is known as indirection.

Page 12: CPP08 - Pointers

Alternative Syntax#include <iostream>

using namespace std;

void calculate_square (int &num) { num = num * num;}

int main() { int num = 5; int num2 = 10;

calculate_square (num2);

cout << "Square is " << num2 << endl; return 1;}

Page 13: CPP08 - Pointers

Do We Want Them?• Yes and no.• They are invaluable for doing certain things.• They are overly complex for many requirements.

• Far better method in most cases to avoid the use of pointers and make use of return values.• This is not possible in all situations.

• When in doubt, try to think of a way to avoid using a pointer.

Page 14: CPP08 - Pointers

Why Use Pointers?• Very efficient.• Copying values, especially things like objects, is very costly.

• More on this in a later lecture.

• Permits functions that return more than one value.• Canonical example of this is a function that swaps the contents

of two variables.• Resolves some issues of data persistance.• Introduces others along the way…

Page 15: CPP08 - Pointers

Why Not Use Pointers?• Additional complexity of code.• Code that uses pointers is almost always more complex.

• Easy to make mistakes.• Pointers let you work with the live ammo of memory addresses.

• You can easily manipulate the memory location rather than the memory contents.

• Permits unintended side effects.• Largely negates the issue of scope.

Page 16: CPP08 - Pointers

Alas…• Sometimes, we have to use pointers.• Because C++ is built around the assumption we will be.

• We’ll see them used relatively freely when we talk about objects in a couple of weeks.• For various reasons, passing by value is not really appropriate for

dealing with objects.

Page 17: CPP08 - Pointers

Pointer Notation• When we want to represent pointers in diagram form, we use

the notation below.• This is a simple representation.• We don’t worry about memory locations• Just the relationship between variables.

*ptr Var

Page 18: CPP08 - Pointers

Pointers Example 1

int main() { int *yPtr, *zPtr; int y, z; y = 10; z = 5; zPtr = &z; yPtr = &y;

cout << "Y is " << y << endl; cout << "yPtr is " << yPtr<< endl; cout << "Z is " << z<< endl; cout << "zPtr is " << zPtr<< endl;

return 1;}

Page 19: CPP08 - Pointers

Pointers Example 2

int main() { int *yPtr, *zPtr; int y, z;

y = 10; z = 5; zPtr = &z; yPtr = &y;

cout << "Y is " << &y << endl; cout << "yPtr is " << *yPtr<< endl; cout << "Z is " << &z << endl; cout << "zPtr is " << *zPtr<< endl;

return 1;}

Page 20: CPP08 - Pointers

Pointers Example 3int main() { int *yPtr, *zPtr; int y, z;

y = 10; z = 5; zPtr = &z; yPtr = zPtr;

*zPtr = 20; cout << "yPtr is " << *yPtr<< endl; cout << "zPtr is " << *zPtr<< endl;

return 1;}

Page 21: CPP08 - Pointers

When Working With Pointers• Always be clear of the relationship between variables.• Draw diagrams if needed

• Always be sure of where your pointers are actually pointing.• Remember, the * means ‘the value of my current memory

location’• Be wary of side effects• Very easy to introduce!

Page 22: CPP08 - Pointers

Summary• C++ has pointers.• Don’t sweat it too much just now…

• They are extremely powerful• They let you do things you couldn’t really otherwise do.

• They are extremely dangerous.• It’s very easy to lose control of your program by using them.

• Be wary!