Lecture 21 End of Semester Review part 3 - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

Lecture 21 End of Semester Review part 3

Description:

for a variable at run time (as opposed to static allocation. at compile time) Program ... The same is also valid for statically allocated arrays. ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 35
Provided by: nikolan
Category:

less

Transcript and Presenter's Notes

Title: Lecture 21 End of Semester Review part 3


1
Lecture 21End of Semester Review (part 3)
memory
address
int intPointerint anotherPointerint
oneIntPtr, twoInt
33
int alpha 8
intPointer alpha
25
is called address-of operator
intPointer 25
is called dereference operator
2
Dynamic Allocation of Memory (concept)
Dynamic Allocation is allocation of memory space
for a variable at run time (as opposed to static
allocation at compile time)
Program
Data allocated statically at compile time
Free Store (Heap) used for dynamic allocation
Memory
3
Dynamic Allocation of Memory in C
address
memory
int intPointer
intPointer new int
150
Alternativellyint intPointer new int
?
part of the heap
0150
4
Dynamic Allocation of Memory in C (more)
bool truth NULLfloat money NULL
  • We assign NULL to a pointer if we want a pointer
    to point tonothing
  • NULL is equivalent to the int value 0
  • NULL is defined in the header file ltcstdlibgt

5
Dynamic Allocation of Memory in C (more)
truth new booltruth truemoney new
float money 33.46float myMoney new float
6
Dynamic Allocation of Memory in C (more)
Pointer variables can be compared for equality
and can be assigned one to another as long as
they point to variables of the same data type.
7
Garbage Collection
delete myMoney
Note that delete does not delete the pointer
variable, but the variable (i.e. the memory) to
which the pointer points.
myMoney money
myMoney
myMoney
33.46
8
Array Names and Pointers
char alpha20char alphaPtrchar
letterPtrvoid Process(char arrayParam) alph
aPtr alphaletterPtr alpha0Process(alpha
)
Structs (classes) and Pointers
struct MoneyType int euros int
centsMoneyType moneyPtr new
MoneyTypemoneyPtr -gt euros 1000 // or
(moneyPtr).euros1000 moneyPtr -gt cents 50
9
Dynamically Allocated Arrays
To allocate an array of type ItemType in the
heap, you 1. declare a pointer ptr to a
variable of type ItemTypeItemType ptr 2.
and allocate space in the heap with the
commandptr new ItemTypesizewhere size is
the number of elements in the array you are
allocating.
ptr points to the first element in the array,
i.e. ptr0ptr k points to (i.e. is the
address of) ptrk
10
ItemType ptr new ItemTypesize
ptr 0 is the same as (ptr0)ptr 1 is the
same as (ptr1) ptr 2 is the same as
(ptr2) etc.
ptr is the same as ptr0 (ptr 1) is the
same as ptr1(ptr 2) is the same as
ptr2etc.
ItemType ptr2 ptr 5ptr ptr 1ptr
The same is also valid for statically allocated
arrays.The name of an array is a pointer to the
0 element of the array!
We deallocate a dynamically allocated array with
the command delete ptr
11
Specifications for ADT Queue
ifndef QUEUE_TYPE_Hdefine QUEUE_TYPE_H
templateltclass ItemTypegt class
QueueTypepublic QueueType() // constructs
an empty queue QueueType() void
MakeEmpty() bool IsFull() const bool
IsEmpty() const void Enqueue(ItemType
newItem) void Dequeue(ItemType
item)private endif
QueueType.h
templateltclass ItemTypegtstruct NodeType
NodeTypeltItemTypegt qFrontNodeTypeltItemTypegt
qRear
12
Implementing Queue by a Chain of Linked Nodes in
the Heap
queue item
pointertonode
A single node in the heap
templateltclass ItemTypegtstruct NodeType
ItemType info NodeType next
goes to QueueType.cc
A chain of linked nodes in the heap
13
Constructor (creates an empty queue)
templateltclass ItemTypegtQueueTypeltItemTypegtQueu
eType() qFront NULL qRear NULL
qRear
qFront
IsEmpty (checks whether the queue is empty)
templateltclass ItemTypegtbool QueueTypeltItemTypegt
IsEmpty() const return (qRear NULL)
14
Enqueue
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Enqueue(ItemType newItem)
NodeTypeltItemTypegt newNodePtr newNodePtr
new NodeTypeltItemTypegt newNodePtr-gtinfo
newItem
copy
newItem
newNodePtr
15
Enqueue
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Enqueue(ItemType newItem)
NodeTypeltItemTypegt newNodePtr newNodePtr
new NodeTypeltItemTypegt newNodePtr-gtinfo
newItem newNodePtr-gtnext NULL
newItem
newNodePtr
16
Enqueue
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Enqueue(ItemType newItem)
NodeTypeltItemTypegt newNodePtr newNodePtr
new NodeTypeltItemTypegt newNodePtr-gtinfo
newItem newNodePtr-gtnext NULL if (qRear
NULL) qFront newNodePtr else qRear-gtnext
newNodePtr
newItem
newNodePtr
17
Enqueue
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Enqueue(ItemType newItem)
NodeTypeltItemTypegt newNodePtr newNodePtr
new NodeTypeltItemTypegt newNodePtr-gtinfo
newItem newNodePtr-gtnext NULL if (qRear
NULL) qFront newNodePtr else qRear-gtnext
newNodePtr qRear newNodePtr
newItem
newNodePtr
18
Enqueue (inserting an item into an empty queue)
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Enqueue(ItemType newItem)
NodeTypeltItemTypegt newNodePtr newNodePtr
new NodeTypeltItemTypegt newNodePtr-gtinfo
newItem newNodePtr-gtnext NULL if (qRear
NULL) qFront newNodePtr else qRear-gtnext
newNodePtr qRear newNodePtr
qRear
qFront
newNodePtr
19
Enqueue (inserting an item into an empty queue)
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Enqueue(ItemType newItem)
NodeTypeltItemTypegt newNodePtr newNodePtr
new NodeTypeltItemTypegt newNodePtr-gtinfo
newItem newNodePtr-gtnext NULL if (qRear
NULL) qFront newNodePtr else qRear-gtnext
newNodePtr qRear newNodePtr
qFront
newNodePtr
20
Dequeue
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Dequeue(ItemType item) NodeTypeltItemTypegt
tempPtr tempPtr qFront item
qFront-gtinfo
qFront
copy
item
tempPtr
21
Dequeue
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Dequeue(ItemType item) NodeTypeltItemTypegt
tempPtr tempPtr qFront item
qFront-gtinfo qFront qFront-gtnext

qFront
item
tempPtr
22
Dequeue
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Dequeue(ItemType item) NodeTypeltItemTypegt
tempPtr tempPtr qFront item
qFront-gtinfo qFront qFront-gtnext
delete tempPtr
qFront
item
tempPtr
23
Dequeue (removing the last node)
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Dequeue(ItemType item) NodeTypeltItemTypegt
tempPtr tempPtr qFront item
qFront-gtinfo qFront qFront-gtnext if
(qFront NULL) qRear NULL delete
tempPtr
qFront
qRear
item
tempPtr
24
Dequeue (removing the last node)
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Dequeue(ItemType item) NodeTypeltItemTypegt
tempPtr tempPtr qFront item
qFront-gtinfo qFront qFront-gtnext if
(qFront NULL) qRear NULL delete
tempPtr
qFront
qRear
item
tempPtr
25
Dequeue (removing the last node)
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
Dequeue(ItemType item) NodeTypeltItemTypegt
tempPtr tempPtr qFront item
qFront-gtinfo qFront qFront-gtnext if
(qFront NULL) qRear NULL delete
tempPtr
qFront
qRear
item
tempPtr
26
MakeEmpty
templateltclass ItemTypegtvoid QueueTypeltItemTypegt
MakeEmpty() NodeTypeltItemTypegt tempPtr
while (qFront ! NULL) tempPtr qFront
qFront qFront-gtnext delete tempPtr
qRear NULL
templateltclass ItemTypegtQueueTypeltItemTypegtQue
ueType() MakeEmpty()
27
Shallow Copy
class SomeType public int x float y
int w2 SomeType a, b a.x 1 a.y
2.0 a.w0 200 a.w1 201 b a
28
Shallow Copy
class SomeType public int x float y
int w2 SomeType a, b a.x 1 a.y
2.0 a.w0 200 a.w1 201 b a
Memory

x 1y 2.0w 200 201
Object a
x y w
Object b
29
Shallow Copy
class SomeType public int x float y
int w2 SomeType a, b a.x 1 a.y
2.0 a.w0 200 a.w1 201 b a
Memory

x 1y 2.0w 200 201
Object a
x 1y 2.0w 200 201
Object b
30
Shallow Copy (more)
template ltclass ItemTypegtclass QueueType
private NodeTypeltItemTypegt qFront
NodeTypeltItemTypegt qRear QueueType q1,
q2
31
Shallow Copy (more)
template ltclass ItemTypegtclass QueueType
private NodeTypeltItemTypegt qFront
NodeTypeltItemTypegt qRear QueueType q1,
q2 q2 q1
This operation could be dangerous!!!
Memory

qRear
qFront
q1
qFront
qRear
q2
Heap
32
Solution deep copy by overloading the operator
Specification in QueueType.hvoid
operator(QueueTypeltItemTypegt otherQueue)
Implementation in QueueType.cc template ltclass
ItemTypegt void QueueTypeltItemTypegtoperator(Que
ueTypeltItemTypegt otherQueue)
NodeTypeltItemTypegt tmpPtr otherQueue.qFront
MakeEmpty() while (tmpPtr ! NULL)
Enqueue(tmpPtr-gtinfo) tmpPtr
tmpPtr-gtnext
This function performs deep copy, i.e. it not
only copies one class object to another but also
makes copies of any pointed-to data
33
Copy constructor
  • In order to be able to
  • make initialization in a variable declaration
    QueueTypeltintgt q2 q1
  • have functions whose parameters are objects of
    class QueueType passed by value void
    operator(QueueTypeltItemTypegt otherQueue)
  • have functions which return objects of type
    QueueType return q2

we need to define and implement a copy
constructor of the type ClassName(const
ClassName anotherObject)
34
Copy constructor (more)
Specification in QueueType.hQueueType(const
QueueTypeltItemTypegt otherQueue)
Implementation in QueueType.cc template ltclass
ItemTypegt QueueTypeltItemTypegtQueueType(const
QueueTypeltItemTypegt otherQueue)
NodeTypeltItemTypegt tmpPtr otherQueue.qFront
qFront NULL qRear NULL while (tmpPtr !
NULL) Enqueue(tmpPtr-gtinfo) tmpPtr
tmpPtr-gtnext
The rule of the big 3 If one of the three
class destructor, copy constructor, or
overloaded operator is necessary, then most
likely all three are necessary.
Write a Comment
User Comments (0)
About PowerShow.com