object-oriented programming in c++

29
Object-Oriented Programming in C++ Week 2 Pointer Variables Dynamic Memory

Upload: naif

Post on 12-Jan-2016

18 views

Category:

Documents


0 download

DESCRIPTION

Object-Oriented Programming in C++. Week 2 Pointer Variables Dynamic Memory. Introduction. Last lecture we looked at more aspects of functions constants, parameter passing and operator overloading introduced reference types This lecture we will introduce pointer variables - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Object-Oriented Programming in C++

Object-Oriented Programming in C++

Week 2

Pointer Variables

Dynamic Memory

Page 2: Object-Oriented Programming in C++

IntroductionLast lecture we•looked at more aspects of functions

– constants, parameter passing and operator overloading

•introduced reference typesThis lecture we will •introduce pointer variables•discuss dynamic memory

– creating objects using new– deleting objects

Page 3: Object-Oriented Programming in C++

Pointers in C++• Variables have two values associated with them:

– their content – and their address.

• A pointer is a variable which stores the memory address of another variable.

• Consider a program which includes the following declarations:char letter = 'c';

int num = 5, age = 25;

note: the address values given are arbitrary examples

Address name value

1245020 letter 'c'

1245012 num 5

1245000 age 25

Page 4: Object-Oriented Programming in C++

Addresses• The actual address is allocated by the system

– not under programmer control

• Unlike the contents, the address of a variable can not change during its lifetime.

• However the address of a given variable may differ each time the program is run

• The & operator gives the address of a variable.

• Using the previous example:

&letter is 1245020

&num is 1245012

&age is 1245000

Page 5: Object-Oriented Programming in C++

Example

• The following code displays a variable’s contents and its address:

int num = 32;

cout << "num = " << num << " at address " << &num<< endl;

• output:num = 32, address= 1245024

• We are not (usually) interested in knowing the absolute value of an address

Page 6: Object-Oriented Programming in C++

Pointer variables• Pointer variables are used to store addresses of other

variables.int num = 5;

char letter = 'c'; int * pNum;

char * pLetter;pNum= &num;pLetter = &letter;

• pNum is a variable which will hold the address of an integer.

• pLetter is a variable which will hold the address of a character

• The declaration int * pNum

means "*pNum is an integer, therefore pNum is a pointer to an integer".

Page 7: Object-Oriented Programming in C++

Indirection operator

• The * symbol is an operator in C called the indirection or dereference operator– it means the value at the location pointed to

• The * operator is the inverse of the & operator

• * pNum is the data at the memory location stored in pNum

Page 8: Object-Oriented Programming in C++

Pointer variables

address name value new value

5024

5012

5000

num

y

pNum

510

10

5024 5012

int main() {

int num = 10, y, *pNum;

pNum = &num;

y = *pNum;

*pNum = 5;

pNum = &y;

}

Page 9: Object-Oriented Programming in C++

Pointer variables

address name value new value

num

y

pNum

510

10

• we are not interested in the memory address stored by a pointer variable

• just that it points to a particular chunk of memory

#include <stdio.h>int main() {

int num = 10, y, *pNum;pNum = &num;y = *pNum;*pNum = 5;pNum = &y;

}

Page 10: Object-Oriented Programming in C++

Pointers vs. references

• what's the difference between a pointer and a reference variable?

• both don't hold actual data– they hold the address of where to find data

• that address might have multiple references / pointers to it

• the syntax of using a reference variable of a particular type is the same as the type itself

Page 11: Object-Oriented Programming in C++

Pointers vs. references• reference variables must be initialised when

they are declared– always point to the same address– although the data at that address can be changed

• the address stored in a pointer can be accessed and changed

• reference variables are– safer to use than pointers– have nicer syntax– are less flexible than pointers

Page 12: Object-Oriented Programming in C++

Recall swap function – pass by reference

void swap(int & a, int & b) {int temp = a;a = b;b = temp;

}int main() {

int x =3, y = 5;swap(x, y);cout << "After swap function, x = " << x << " and y = " << y << endl;

}

After swap function, x = 5 and y = 3

Page 13: Object-Oriented Programming in C++

Swap function – pass by pointer

void swap(int * a, int * b);int main() {

int x = 3;int y = 5; swap(&x, &y);cout << "After swap function, x = " << x << " and y = " << y << endl;

}

void swap(int * a, int * b) {int temp = *a;*a = *b;*b = temp;

}

After swap function, x = 5 and y = 3

Page 14: Object-Oriented Programming in C++

Swap function – pass by pointer

name value new value

x

y

temp

b

a

3

5

3

5

3

int main() {int x = 3;int y = 5;swap(&x, &y);

}void swap(int * a, int * b) {

int temp = *a;*a = *b;*b = temp;

}

Page 15: Object-Oriented Programming in C++

Array addresses• an array variable is actually a pointer variable

int num[ 5 ] = { 3, 1, 5, 2, 4 };

• the variable num is a pointer to an int– it stores the address of the first element of the

array• same as &num[0]

• num[0] gives the value of the first element– same as *num

• num + 1 is the address of the second element– *(num + 1) is its value

Page 16: Object-Oriented Programming in C++

Arrays and pointers

• this means we can use pointers to access array elements

• what does this do?int num[5 ] = { 3, 1, 5, 2, 4 };int * pa;for ( pa = num; pa < (num + 5); pa++)

cout << *pa << " ";

Output: 3 1 5 2 4

Page 17: Object-Oriented Programming in C++

Passing arrays to functions• in C and C++, arrays are passed to functions by

reference– because the array variable is really a pointer

• unlike in Java, the array does NOT carry any information about its size– need to also pass the array size to the function

• a function to output an array:void outputArray(int a[ ], int size) {int i;for (i=0; i < size; i++)

cout << a[i] << " ";}

• to call the function:outputArray(num, 5);

Page 18: Object-Oriented Programming in C++

Tutorial Question • Modify your Account class, using overloaded

methods where appropriate, so that a cheque for a given amount can be withdrawn from the account

• A sensible function prototype for this function:bool issueCheque(double amount,

Cheque & cheque);

• usage:Cheque myCheque(0, true);if (myAccount.issueCheque(30, myCheque)) { …

Page 19: Object-Oriented Programming in C++

Returning a Cheque• what if we wanted to return a newly created cheque

object?– instead of passing one in?

Cheque issueCheque(double amount) { Cheque ch(amount, true); return ch;}• this could be used with a more natural syntaxCheque myCheque = myAccount.issueCheque(30);

• this works – the Cheque is returned by value• a copy of ch is created and stored in myCheque

Page 20: Object-Oriented Programming in C++

Returning a Cheque by reference

• the only problem with the previous method is that it can be inefficient

• we create a cheque• then when it is returned:

– another cheque is created– the attributes of the original cheque are copied to it– the original cheque is destroyed

• why not return a reference to the cheque created in the method instead?

Page 21: Object-Oriented Programming in C++

Returning by referenceCheque & issueCheque(double amount) { Cheque ch(amount, true); return ch;}•usage:Cheque myCheque = myAccount.issueCheque(30);•warning C4172: returning address of local variable or temporary•heed the warning

– ch has gone out of scope– the memory it occupied could soon be used for

something else

Page 22: Object-Oriented Programming in C++

Heap memory• it would nice to be able to create an object in a

function that doesn't go out of scope when the function returns

• so far all the objects we have constructed have been on the stack

• heap memory is a separate area of memory that is managed separately from the stack

• we can construct an object here and store a pointer to it on the stack

• the object will exist until we explicitly delete it– even if all pointers to it go out of scope

Page 23: Object-Oriented Programming in C++

Returning a pointer

Cheque * Account::issueCheque(double amount) { Cheque * ch = new Cheque(amount, true); return ch;}

•usage:Cheque * myCheque = myAccount.issueCheque(30);cout << myCheque->getAmount() << endl;delete myCheque;

Page 24: Object-Oriented Programming in C++

Dynamically allocating objects• using the keyword new tells the Cheque

constructor to create the Cheque object ch on the heap

• the constructor returns a pointer to the object– the address of where to find it

• we then return the pointer from the issueCheque function– returns a copy of the address of ch to myCheque

• we can access the myCheque members using the indirection operator ->myCheque->getAmount()

• same effect as (*myCheque).getAmount()• but much nicer to use

Page 25: Object-Oriented Programming in C++

Deallocating objects

• now we don't need to worry about the Cheque object going out of scope

• it will stay on the heap forever

• or until the program terminates– if we are lucky

• It is important to release memory once it is no longer required

• Otherwise we have a memory leak– the memory is not available to create new objects

Page 26: Object-Oriented Programming in C++

Deallocating objects

• The C++ keyword delete provides this facility:

delete myCheque;• delete calls the myCheque destructor• and releases the memory pointed to by myCheque • myCheque will still contain a pointer to the memory

previously occupied by the Cheque object• at some point this memory might be allocated to

another object

Page 27: Object-Oriented Programming in C++

Memory leaks

• it is easy to lose a reference to allocated memory– if the variable pointing to it is reassigned– when the variable goes out of scope

• the allocated memory is NOT automatically released

• if not released, it cannot be reused until the program terminates– in some systems, not even then

• this is a MEMORY LEAK– a very bad thing

Page 28: Object-Oriented Programming in C++

Memory management in Java and C#

• in Java and C#, heap memory is allocated with the keyword new– when creating arrays and objects– the reference to these objects is like a pointer

• Java does not rely on the programmer to manage memory allocation – instead, a garbage collector is provided

• objects which are no longer reachable (for example, by going out of scope) remain in memory

• at intervals (or when requested) the garbage collector runs– frees up this space – compacts the heap memory

Page 29: Object-Oriented Programming in C++

SummaryIn this lecture we have:

•introduced pointer variables

•discussed dynamic memory– creating objects using new– deleting objects

In the tutorial we will•practice using pointers and dynamic memory allocation