Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Programming in C++

Similar presentations


Presentation on theme: "Object-Oriented Programming in C++"— Presentation transcript:

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

2 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 discuss dynamic memory creating objects using new deleting objects

3 Pointers in C++ Address name value 1245020 letter 'c' 1245012 num 5
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 letter 'c' num 5 age 25

4 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 &num is &age is

5 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= We are not (usually) interested in knowing the absolute value of an address

6 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".

7 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

8 Pointer variables int main() { int num = 10, y, *pNum; } pNum = #
pNum = &y; } address name value new value 5024 5012 5000 num 5 10 y 10 pNum 5024 5012

9 Pointer variables 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; } address name value new value num 5 10 y 10 pNum

10 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

11 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

12 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

13 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

14 Swap function – pass by pointer
int main() { int x = 3; int y = 5; swap(&x, &y); } void swap(int * a, int * b) { int temp = *a; *a = *b; *b = temp; name value new value x 3 5 y 5 3 b a temp 3

15 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

16 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:

17 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);

18 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)) { …

19 Returning a Cheque a copy of ch is created and stored in myCheque
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 syntax Cheque myCheque = myAccount.issueCheque(30); this works – the Cheque is returned by value a copy of ch is created and stored in myCheque

20 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?

21 Returning by reference
Cheque & 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

22 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

23 Returning a pointer usage:
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;

24 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

25 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

26 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

27 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

28 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

29 Summary introduced pointer variables discussed dynamic memory
In 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


Download ppt "Object-Oriented Programming in C++"

Similar presentations


Ads by Google