pointers lec5

27
Nabeel Alassaf: University Of Jordan,Computer Science Department, Data Structure 2 ١ Data Structure 2 Instructor :Nabeel Alassaf Pointers Lecture 5

Upload: tamer-abu-alzenat

Post on 10-Aug-2015

77 views

Category:

Art & Photos


0 download

TRANSCRIPT

Page 1: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department, Data Structure 2 ١

Data Structure 2Instructor :Nabeel Alassaf

PointersLecture 5

Page 2: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٢

Consider the following class

Also consider the following statements

Page 3: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٣

Page 4: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٤

• The object objectOne has a pointer member variable p.

• Suppose that during program execution the pointer p creates a dynamic array.

• When objectOne goes out of scope, all the member variables of objectOne are destroyed.

• However, p created a dynamic array, and dynamic memory must be deallocated using the operator delete.

• If the pointer p does not use the delete operator to deallocate the dynamic array, the memory space of the dynamic array would stay marked as allocated, even though it cannot be accessed.

Page 5: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٥

•How do we ensure that when p is destroyed, the dynamic memory created by p is also destroyed?

•Suppose that objectOne is as shown in Figure below.

Page 6: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٦

• We can put the necessary code in the destructor to ensure that when objectOne goes out of scope, the memory created by the pointer p is deallocated.

• The definition of the destructor for the class pointerDataClassis:

Page 7: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٧

Suppose that objectOne and objectTwo are as shown in Figure below:

Page 8: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٨

• If objectTwo.p deallocates the memory space to which it points, objectOne.p would become invalid.

• To avoid this shallow copying of data for classes with a pointer member variable, C++ allows the programmer to extend the definition of the assignment operator.

Page 9: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٩

• Once the assignment operator is properly overloaded, both objectOne and objectTwo have their own data, as shown in Figure below.

Page 10: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٠

• In this statement, the object objectThree is being declared and is also being initialized by using the value of objectOne.

• The values of the member variables of objectOne are copied into the corresponding member variables of objectThree.

• This initialization is called the default member-wise initialization.

• The default member-wise initialization is due to the constructor, called the copy constructor (provided by the compiler).

• Assume you have the following statement declaration

Page 11: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١١

• Just as in the case of the assignment operator, because the class pointerDataClass has pointer member variables, this default initialization would lead to a shallow copying of the data, as shown in Figure below. (Assume that objectOne is given as before.)

Page 12: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٢

• As parameters to a function, class objects can be passed either by reference or by value.

• The class pointerDataClass has the destructor, which deallocates the memory space pointed to by p. Suppose that objectOne is as shown in below.

Page 13: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٣

• The function destroyList has a formal value parameter, paramObject. Now consider the following statement:

destroyList(objectOne);

Page 14: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٤

• Because objectOne is passed by value, the member variables of paramObject should have their own copy of the data. In particular, paramObject.p should have its own memory space. • Note: If a class has pointer member variables:

1. During object declaration, the initialization of one object using the value of another object would lead to a shallow copyingof the data, if the default member-wise copying of data is allowed.

2. If, as a parameter, an object is passed by value and the default member-wise copying of data is allowed, it would lead to a shallow copying of the data.

3. In both cases, to force each object to have its own copy of the data, we must override the definition of the copy constructor provided by the compiler.

4. This is usually done by putting a statement that includes the copy constructor in the definition of the class, and then writing the definition of the copy constructor.

Page 15: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٥

• The copy constructor automatically executes in three situations (the first two are described previously):• When an object is declared and initialized by using the

value of another object• When, as a parameter, an object is passed by value• When the return value of a function is an object

• General syntax to include the copy constructor in the definition of a class is:

ClassName(const ClassName& otherObject);

Page 16: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٦

Example:

//Header file ptrDataClass.hclass pointerDataClass{public:

void print() const;void insertAt(int index, int num);pointerDataClass(int size = 10); //constructor

~pointerDataClass(); //destructorpointerDataClass (const pointerDataClass& otherObject);

private:int maxSize; int length; int *p;

};

Header File

Copy Constructor

Page 17: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٧

#include <iostream>#include <cassert>

#include "ptrDataClass.h"

using namespace std;

void pointerDataClass::print() const{

for (int i = 0; i < length; i++)cout << p[i] << " ";

}

Implementation File

Page 18: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٨

void pointerDataClass::insertAt(int index, int num){

//if index is out of bounds, terminate the programassert(index >= 0 && index < maxSize);

if (index < length)p[index] = num;

else{

p[length] = num;length++;

}}

Page 19: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ١٩

pointerDataClass::pointerDataClass(int size){

if (size <= 0){

cout << "The array size must be positive." << endl;cout << "Creating an array of the size 10." << endl;

maxSize = 10;}else

maxSize = size;

length = 0;

p = new int[maxSize];}

Page 20: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٢٠

pointerDataClass::~pointerDataClass(){

delete [ ] p;}

//copy constructorpointerDataClass::pointerDataClass

(const pointerDataClass& otherObject){

maxSize = otherObject.maxSize;length = otherObject.length;

p = new int[maxSize];

for (int i = 0; i < length; i++)p[i] = otherObject.p[i];

}

Page 21: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٢١

#include <iostream>#include "ptrDataClass.h"

using namespace std;

void testCopyConst(pointerDataClass temp);

int main(){

pointerDataClass listOne; //Line 1

int num; //Line 2int index; //Line 3

Main user File

Page 22: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٢٢

cout << "Line 4: Enter 5 integers." << endl; //Line 4

for (index = 0; index < 5; index++) //Line 5{

cin >> num; //Line 6listOne.insertAt(index, num); //Line 7

}cout << "Line 8: listOne: "; //Line 8

listOne.print(); //Line 9cout << endl; //Line 10

Page 23: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٢٣

//Declare listTwo and initialize it using listOnepointerDataClass listTwo(listOne); //Line 11

cout << "Line 12: listTwo: "; //Line 12listTwo.print(); //Line 13cout << endl; listTwo.insertAt(5, 34); //Line 15listTwo.insertAt(2, -76); //Line 16

cout << "Line 17: After modifying listTwo: "; //Line 17listTwo.print(); //Line 18cout << endl;

Page 24: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٢٤

cout << "Line 20: After modifying listTwo, " << "listOne: "; //Line 20

listOne.print(); //Line 21cout << endl; //Line 22

cout << "Line 23: Calling the function testCopyConst"<< endl; //Line 23

//Call function testCopyConsttestCopyConst(listOne);

Page 25: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٢٥

cout << "Line 25: After a call to the function "<< "testCopyConst, " << endl<< " listOne is: "; //Line 25

listOne.print(); //Line 26cout << endl; //Line 27

return 0; //Line 28} //main

Page 26: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٢٦

void testCopyConst(pointerDataClass temp){

cout << "Line 29: *** Inside the function "<< "testCopyConst ***" << endl; //Line 29

cout << "Line 30: Object temp data: "; //Line 30temp.print(); //Line 31cout << endl; //Line 32

temp.insertAt(3, -100); //Line 33cout << "Line 34: After changing temp: "; //Line 34temp.print(); //Line 35cout << endl; //Line 36

cout << "Line 37: *** Exiting the function "<< "testCopyConst ***" << endl; //Line 37

}

Page 27: Pointers lec5

Nabeel Alassaf: University Of Jordan,Computer Science Department,Advanced Programming ٢٧

Line 4: Enter 5 integers.1 2 3 4 5Line 8: listOne: 1 2 3 4 5Line 12: listTwo: 1 2 3 4 5Line 17: After modifying listTwo: 1 2 -76 4 5 34Line 20: After modifying listTwo, listOne: 1 2 3 4 5Line 23: Calling the function testCopyConstLine 29: *** Inside the function testCopyConst ***Line 30: Object temp data: 1 2 3 4 5Line 34: After changing temp: 1 2 3 -100 5Line 37: *** Exiting the function testCopyConst ***Line 25: After a call to the function testCopyConst,

listOne is: 1 2 3 4 5

Output: