No Slide Title

Download Report

Transcript No Slide Title

Chapter 5 Pointers Data Addresses in Memory Declaring Pointer Variables Assigning Values to Pointers Arrays and Pointers Dynamic Memory Management new & delete

CSE 331 – Lecture 8

The Destructor The Copy Constructor An Overloaded Assignment Operator The Pointer ‘this’ The miniVector Class Matrices The matrix Class Summary Slides (3 pages)

2

Quick Review & Demo

 Demonstrate use of g++, make and ddd  Demonstrate Mandelbrot program  Answer questions about next assignment

Main Index

Contents

Pointer Illustration

// x is an int and // ip points to an int

int x, *ip; x = 37; the data ip = &x;

Data contents Address // fp points to a // dynamically allocated and

the pointer

// nameless float

float *fp; fp = new float; *fp = -87.5;

Data Addresses in Memory

5000 integer

50

char

S

5004 5005

Declaring Pointer Variables

 Pointer declaration format * ;  The declared pointer is a variable whose value is the address of a data item of the designated type. int *intPtr; char *charPtr;

6

Assigning Values to Pointers

int m = 50, *intPtr;

// here intPtr “points to” nothing

intPtr = &m;

//

&m is the address of m

// now intPtr “point to” m

?

?

intPtr (a) Afte r de claration 50 m 50 m &m intPtr (b) Afte r assignme nt

Main Index

Contents

Arrays and Pointers

arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] arr[6] 6000 6004 6008 6012 6016 6020 6028 6032 arr arr+1 arr+2 arr+5 arr+7 The amount the pointer is incremented is based on the sizeof() the base type of the array Ex:

arr+5 is really &arr[0] + 5*sizeof(element_type)

8

Dynamic Memory

 Pointer – Variable whose value is the address of another data value  Heap – System managed store of memory available to be allocated and deallocated while program is running  New – operator to dynamically allocate memory from the heap for program use  Delete – operator to dynamically deallocate memory no longer needed by program and return it to the heap

Main Index

Contents

9

“new” and “delete” operators

Allocating & deallocating a single data location

int *p = new int; // allocates it delete p; // deallocates it 

Allocating & deallocating a dynamic array

– Note square brackets used with delete operator. These ensure system deallocates ALL locations in array, not just the first one.

// allocate space for 300 ints

int *arr = new int[300];

// deallocate all 300 int locations

delete [] arr;

Main Index

Contents

10

Pointers and classes

 Use # 1 – Dynamically allocate and deallocate class objects  Use # 2 – Dynamically allocate and deallocate storage used by class member variables  Why is this important?

– When execution exits a block within which an object is declared, a destructor is called automatically.

– – The default destructor takes care of returning memory to the heap in usage # 1.

In usage # 2 we must write our own destructor to return the memory used by member variables, else it is lost to the program creating a memory leak.

Main Index

Contents

Illustrating the Destructor

Dynamic class from text (member 1 is data -- member 2 is a pointer) Before destroying obj After destroying obj member1=m1 member2 member1=m1 member2 m2 On heap \\ m2 Remains on the heap with nothing pointing at it.

12

The destructor

// super simple vector class

template class myVector { public:

// the constructor allocates the array

myVector(int n=0) : dSize(n), data(NULL) { if (dSize > 0) data = new T(dSize); }

// the destructor deallocates the array

~myVector() { if (data != NULL) delete [] data; } private: T *data;

// the dynamically allocated array

}; int dSize;

// the size of the array

Main Index

Contents

13

Assignment & Copy Constructor

 C++ provides a default destructor, assignment operator and copy constructor for every class  All of these are “shallow” – They only copy or deallocate the member variables themselves, NOT what they “point to”  For all classes that contain member variables that point to dynamically allocated memory, we must create our own destructor, assignment operator and copy constructor

Main Index

Contents

Shallow Copy

// object A has 10 data locations // and B is a copy of A (but BOTH point to // the single dynamically allocated array)

myVector A(10), B(A); A B nSize data nSize data 14

dynamically allocated array (10 locations)

Main Index

Contents

15

Copy Constructor / Overloaded Assignment Operator

15 15 y 30 30 0 y x (a) Initialization: time24 y = x 15 15 0 30 30 x (b) Assignment: z = x

Main Index

Contents

16

Copy Constructors

The Copy Constructor is called whenever ….

 1) objects are passed as value parameters  2) an object is returned as the function value  3) an object is created and initialized with another object of the same class

Main Index

Contents

17

Copy Constructor

Example: expanded myVector class  Note: parameter MUST be a reference parameter, because the copy constructor itself is used in passing by value

template myVector::myVector(const myVector& v) : dSize(v.dSize) { data = new T[v.dSize];

// allocate space

for(int i=0; i

// copy values

}

Main Index

Contents

*this

The Pointer ‘this’

objA;

18

this->

Main Index

Contents

memberl

Overloading Assignment

Example: expanded myVector class

19 template myVector& myVector::operator=(const myVector& v) { if (*this == v)

// copying itself

return *this; if (dSize < v.dSize)

// existing array too small

{ delete [] data;

// deallocate

data = new T[v.dSize];

// allocate enough space

} for (int i=0; i

// copy values

dSize = v.dSize; return *this; }

Main Index

Contents

miniVector Class

 The miniVector class is an implementation of a vector container class illustrating all of these dynamic memory issues  Ford & Topp, Ch 5: d_vector.h

20

Main Index

Contents

0 1 2

Matrices

 A Matrix is a two-dimensional array that corresponds to a row-column table of entries of a specified data type.

 Matrices are referenced using a pair of indices that specify the row and column location in the table. 0 1 2 3 8 1 7 -2 0 10 -3 -14 4 1 6 0 Example: The element mat[0][3] is 2 The element mat[1][2] is 4.

22

Matrix Class

 The matrix class is an implementation of a 2D matrix container class using a vector of vectors  It is dynamically allocated and resizable  Ford & Topp, Ch 5: d_matrix.h

 NOTE: This class is NOT complete. It is missing a destructor, copy constructor and assignment operator.

Main Index

Contents

Summary Slide 1

§-

Pointers contain the address of data in memory…

- Data is accessed by applying the dereference operator * §- Operators such as +, ++, and += apply to pointers.

§- With such operators, pointers can be used for algorithms involving array traversal, but their primary application is in the allocation and maintenance of dynamic memory.

Summary Slide 2

§-

vector implementation

- The miniVector class illustrates the key points.

1) 2) 3) It allocates dynamic memory using: destructor copy constructor overloaded assignment operator It implements push_back(): Therefore it must control vector capacity in order to minimize dynamic memory reallocation. It allows access to elements by using an index: Therefore the class implements an overloaded index operator

Summary Slide 3

§-

Two dimensional arrays in C++

- have the same problems as one-dimensional arrays: 1) fixed size 2) 3) no size attribute If it is a function argument, it is necessary to specify the number of columns as a constant.