+ All Categories
Home > Documents > Main Index

Main Index

Date post: 14-Jan-2016
Category:
Upload: penney
View: 14 times
Download: 0 times
Share this document with a friend
Description:
Chapter 5 – Pointers and Dynamic Memory. 1. Main Index. Contents. Pointer Illustration Vertical / Horizontal View . . . Data Addresses in Memory Declaring Pointer Variables Assigning Values to Pointers Accessing Data with Pointers Arrays and Pointers Operator ‘new’ Operator ‘delete’ - PowerPoint PPT Presentation
22
1 Main Index Conten ts 1 Main Index Conten ts Pointer Illustration Vertical / Horizontal V iew . . . Data Addresses in Memor y Declaring Pointer Varia bles Assigning Values to Poi nters Accessing Data with Poi nters Arrays and Pointers Operator ‘new’ Operator ‘delete’ Illustrating the Destru ctor Copy Constructor / Over loaded Assignment Opera Chapter 5 Chapter 5 Pointers and Dynamic Memory Pointers and Dynamic Memory The Pointer ‘this’ dynamicClass Copy Constructor The C++ Index Operator [] Matrices Summary Slides (3 pages)
Transcript
Page 1: Main Index

1 Main IndexMain Index ContentsContents1 Main IndexMain Index ContentsContents

Pointer Illustration

Vertical / Horizontal View . . .

Data Addresses in Memory

Declaring Pointer Variables

Assigning Values to Pointers

Accessing Data with Pointers

Arrays and Pointers

Operator ‘new’

Operator ‘delete’

Illustrating the Destructor

Copy Constructor / Overloaded Assignment Operator

Declaration of dynamicClass Objects

Chapter 5 Chapter 5 – – Pointers and Dynamic MemoryPointers and Dynamic Memory

The Pointer ‘this’dynamicClass Copy ConstructorThe C++ Index Operator []MatricesSummary Slides (3 pages)

Page 2: Main Index

2 Main IndexMain Index ContentsContents2 Main IndexMain Index ContentsContents

Pointer IllustrationPointer Illustration

A d d res s

D at a co n t en t s

Page 3: Main Index

3 Main IndexMain Index ContentsContents3 Main IndexMain Index ContentsContents

Vertical and Horizontal View of Vertical and Horizontal View of MemoryMemory

A d d res s 0

A d d res s n

A d d res s 2

A d d res s 1

A d d r 0 A d d r 1 A d d r 2 Ad d r n

Vert ical V iew o f M em o ry

H o riz o n t al V iew o f M em o ry

...

. . .

Page 4: Main Index

4 Main IndexMain Index ContentsContents4 Main IndexMain Index ContentsContents

Data Addresses in MemoryData Addresses in Memory

50

in t eger ch ar

5 0 0 0

S

5 0 0 4 5 0 0 5

Page 5: Main Index

5 Main IndexMain Index ContentsContents5 Main IndexMain Index ContentsContents

Declare a pointer by stating the type followed by the variable name, but with a "*" added immediately before the name.

The pointer ptr is a variable whose value is the address of a data item of the designated type.

Declaring Pointer VariablesDeclaring Pointer Variables

int *intPtr;

char *charPtr;

type *ptr;

Page 6: Main Index

6 Main IndexMain Index ContentsContents

Assigning Values to PointersAssigning Values to Pointers

&m is the address of the integer in memory. The assignment statement

sets intPtr to point at an actual data item.

The Figure illustrates the status of the two variables from the declaration statement and the resulting status after the assignment of a value to intPtr.

intPtr = &m;

int m = 50, *intPtr;

5 0m

?in tP tr

?

5 0

m

& m

in tP tr(a) A fte r d e c laratio n (b ) A fte r as s ig n m e n t

Page 7: Main Index

7 Main IndexMain Index ContentsContents7 Main IndexMain Index ContentsContents

Accessing Data with PointersAccessing Data with Pointers

int x = 50, y = 100,

*px = &x, *py = &y;

5 0

x

p x

1 0 0

y

p y

Page 8: Main Index

8 Main IndexMain Index ContentsContents8 Main IndexMain Index ContentsContents

Arrays and PointersArrays and Pointers

a rr

a rr[0 ] a rr[6 ]a rr[5 ]a rr[4 ]a rr[3 ]a rr[2 ]a rr[1 ]

a rr+ 7a rr+ 2a rr+ 1

6000 602860206016601260086004 6032

a rr+ 5

Page 9: Main Index

9 Main IndexMain Index ContentsContents9 Main IndexMain Index ContentsContents

Operator ‘new’Operator ‘new’p = new time24;// *p is 00:00 (midnight)q = new time24(8, 15);// *q is 8:15 AM

H e a p

p .h o u r

q .m in u te

q .h o u r

p .m in u te

00

15

08

00

p

q

Page 10: Main Index

10 Main IndexMain Index ContentsContents

Operator ‘delete’Operator ‘delete’

deallocating a dynamic array, use a slightly different form of delete. Place square brackets [] between delete and the pointer variable name. The system deallocates all of the memory originally assigned to the dynamic array.

arr = new T[ARRSIZE];

// allocated space for ARRSIZE objects

delete [] arr;

// deallocate dynamic array storage

Page 11: Main Index

11 Main IndexMain Index ContentsContents11 Main IndexMain Index ContentsContents

Illustrating the DestructorIllustrating the Destructor

m e m be r1= m 1

B e fo re de s tro ying o bj Af te r de s tro ying o bj

m e m be r2m e m be r1= m 1 m e m be r2

m 2 m 2

O n he ap

\\

R e m ains o n the he ap with no thingpo int ing at i t .

Page 12: Main Index

12 Main IndexMain Index ContentsContents

Copy Constructor / Overloaded Copy Constructor / Overloaded Assignment OperatorAssignment Operator

15

30

y

(a ) In i ti a l i z a ti on : ti m e 24 y = x

15

30

x

0

0

y

(b) As s i gn m e n t: z = x

15

30

x

15

30

Initialization and Assignment

both have the effect of producing a data item

that is a copy of an existing item

Page 13: Main Index

13 Main IndexMain Index ContentsContents13 Main IndexMain Index ContentsContents

Declaration of dynamicClass Declaration of dynamicClass ObjectsObjects

o b jA

m em b er1 = 1 m em b er2

* o b jA .m em b er2 = 2

H eap m em o ry

• The byte-by-byte copy performed by default by C++ is inappropriate for dynamic classes• The copy of an objA in ObjB makes two pointers point at the same memory location.

•This creates a memory leak when one of the two objects is deleted.

Page 14: Main Index

14 Main IndexMain Index ContentsContents

The Pointer ‘this’The Pointer ‘this’

*this objA;

this-> memberl

Page 15: Main Index

15 Main IndexMain Index ContentsContents15 Main IndexMain Index ContentsContents

dynamicClass Copy Constructor dynamicClass Copy Constructor AlgorithmAlgorithm

m e m b e r1 = 3

o b jB

m e m be r 2m e m be r1 = 3 m e m be r 2

* o bjB .m e m be r2 = 5

o b jA

* o b jA .m e m b e r2 = 5

# 1 : c o py c o nte nts

# 2 : allo c ate * m e m b e r2

# 3 : c o py c o nte nts

Example:dynamicClass<int> obj(3,5), objB=objA;

Page 16: Main Index

16 Main IndexMain Index ContentsContents

The MiniVectorThe MiniVector

Check the code of the MiniVector– A small version of the Vector Class available in

STL

The MiniVector uses a function called Reserve() to allocate a dynamic array for extra space

Page 17: Main Index

17 Main IndexMain Index ContentsContents

The C++ Index Operator [ ]The C++ Index Operator [ ]

. . . ar r [i ] . . .

ar r + iar r

arr[i] = 30;

// arr[i] is the address into which 30 is copied

t = arr[i] + 4;

// add 4 to the value of the element at arr[i]

Page 18: Main Index

18 Main IndexMain Index ContentsContents18 Main IndexMain Index ContentsContents

MatricesMatrices 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.

8 1 7 - 2

0 - 3 4 6

1 0 - 1 4 1 0

0 1 2 3

01

2

Example:The element mat[0][3] is 2 The element mat[1][2] is 4.

Page 19: Main Index

19 Main IndexMain Index ContentsContents19 Main IndexMain Index ContentsContents

Vectors and MatricesVectors and Matrices

A dynamic Matrix is represented by using the Vector Class.– Visit the matrix.h file

A dynamic Matrix can be described by using dynamic arrays as well. The code is more complex than using vectors.( Think!) - Food for you brain– How would you define a dynamic matrix by using a

dynamic array?

Page 20: Main Index

20 Main IndexMain Index ContentsContents20 Main IndexMain Index ContentsContents

Summary Slide 1Summary 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.

Page 21: Main Index

21 Main IndexMain Index ContentsContents21 Main IndexMain Index ContentsContents

Summary Slide 2Summary Slide 2

§- vector implementation- The miniVector class illustrates the key points.1) It allocates dynamic memory using:

destructor copy constructor

overloaded assignment operator

2) It implements push_back(): Therefore it must control vector capacity in

order to minimize dynamic memory reallocation.

3) It allows access to elements by using an index:

Therefore the class implements an overloaded index operator

Page 22: Main Index

22 Main IndexMain Index ContentsContents22 Main IndexMain Index ContentsContents

Summary Slide 3Summary Slide 3

§- Two dimensional arrays in C++- have the same problems as one-dimensional

arrays:

1) fixed size

2) no size attribute

3) If it is a function argument, it is necessary to specify the number of columns as a constant.


Recommended