Title: int ptr new int
1What happens here?
- int ptr new int
- ptr 3
- ptr new int
- ptr 4
-
ptr
3
2What happens here?
- int ptr new int
- ptr 3
- ptr new int
- ptr 4
-
ptr
3
4
3Memory Leak
- A memory leak occurs when dynamic memory (that
was created using operator new) has been left
without a pointer to it, by the programmer, and
so is inaccessible.
4Causing a Memory Leak
3000
ptr
ptr2
8
3000
- int ptr new int
- ptr 8
- int ptr2 new int
- ptr2 -5
- // here the storage location
- // containing 8 becomes
- // inaccessible
- ptr ptr2
-
-
1050
1050
-5
5A Dangling Pointer
- occurs when two pointers point to the same object
and delete is applied to one of them. -
- int ptr new int
- ptr 8
- int ptr2 new int
- ptr2 -5
- ptr ptr2
- delete ptr2
-
ptr ptr2
3000
8
1050
1050
-5
1050
6Leaving a Dangling Pointer
ptr
ptr2
3000
- int ptr new int
- ptr 8
- int ptr2 new int
- ptr2 -5
- ptr ptr2
-
- delete ptr2
- ptr2 NULL
8
1050
1050
-5
1050
3000
ptr ptr2
1050
8
??
7Dynamic Objects
- If we have an class called Darray which is
declared as follows - class Darray
- private int Size
- int ArrPtr
- public Darray(int HowBig)
- void ReadArr( )
- . . .
8Constructing Object in a Function
A.ArrPtr
- include "DARRAY.H"
- void DarrayReverse()
- Darray A(Size)
- int I
- for (I0 IltSize I)
- A.ArrPtrI ArrPtrSize-I
- for (I0 IltSize I)
- ArrPtrI A.ArrPtrI
- return
this-gtArrPtr
95
43
67
51
28
9Constructing Object in a Function
A.ArrPtr
- include "DARRAY.H"
- void DarrayReverse()
- Darray A(Size)
- int I
- for (I0 IltSize I)
- A.ArrPtrI ArrPtrSize-I
- for (I0 IltSize I)
- ArrPtrI A.ArrPtrI
- return
28
51
67
43
95
this-gtArrPtr
95
43
67
51
28
10Constructing Object in a Function
A.ArrPtr
- include "DARRAY.H"
- void DarrayReverse()
- Darray A(Size)
- int I
- for (I0 IltSize I)
- A.ArrPtrI ArrPtrSize-I
- for (I0 IltSize I)
- ArrPtrI A.ArrPtrI
- return
28
51
67
43
95
this-gtArrPtr
28
51
67
43
95
11Constructing Object in a Function
- include "DARRAY.H"
- void DarrayReverse()
- Darray A(Size)
- int I
- for (I0 IltSize I)
- A.ArrPtrI ArrPtrSize-I
- for (I0 IltSize I)
- ArrPtrI A.ArrPtrI
- return
28
51
67
43
95
this-gtArrPtr
28
51
67
43
95
12Additional Methods Needed withDynamic Memory
- Destructors - are now needed to deallocate all
dynamic memory before the variable holding their
address goes out of scope. - Copy constructor - needed to allow objects
containing dynamic data to be correctly
initialized, and returned by functions and
methods. - Overloaded assignment operator - needed to
generate a copy of the object not just a pointer
to the object when using the assignment operator.
13Class Destructor
- A special member function of a class that is
implicitly invoked when a class object goes out
of scope. - Class destructor is formed similar to the
constructor except it has a (tilde) placed in
front of its name. - You do not explicitly invoke the destructor.
14Destructor for Darray
15Darray Example
A.ArrPtr
- include "DARRAY.H"
- int main()
- Darray A(5), B(5)
- A.ReadArr()
- B A
-
-
B.ArrPtr
16Darray Example
A.ArrPtr
- include "DARRAY.H"
- int main()
- Darray A(5), B(5)
- A.ReadArr()
- B A
-
-
7
18
2
34
11
B.ArrPtr
17Darray Example
A.ArrPtr
- include "DARRAY.H"
- int main()
- Darray A(5), B(5)
- A.ReadArr()
- B A
-
-
7
18
2
34
11
B.ArrPtr
Problem The storage allocated to B is no longer
accessible !!
18Darray Example
- include "DARRAY.H"
- int main()
- Darray A(5), B(5)
- A.ReadArr()
- B A
-
-
B.ArrPtr
Problem2 After invoking the destructor for A it
invokes the destructor for B!! This results in
an error, which is often fatal.
19Shallow Copy vs. Deep Copy
- A shallow copy copies only the class data
members, and does not copy any pointed-to data. - A deep copy copies not only the class data
members, but also makes separately stored copies
of any pointed-to data.
20Whats the difference?
- A shallow copy shares the pointed to data with
the original class object. - A deep copy stores its own copy of the pointed to
data at different locations than the data in the
original class object.
21Copying Darray
Private Data Size 5 ArrPtr
Private Data Size 5 ArrPtr
50 43 80
items 0 items 1 items 2 items 3 items
4
22Copying Darray
Private Data Size 5 ArrPtr
Private Data Size 5 ArrPtr
50 43 80
50 43 80
23Overloading the assignment operator
- To avoid this problem we need to overload the
assignment operator to copy the data, instead of
copying the pointer to the data, which is what it
does by default.
24Overloading the assignment operator
- Darray Darrayoperator(const Darray rhs)
- int I
- Size rhs.Size
- delete ArrPtr
- ArrPtr new int Size
- for (I0 IltSize I)
- ArrPtrI rhs.ArrPtrI
- return this
-
-
25Copy Constructor
- Similar problems to the problem seen with the
assignment operator, occur when objects that
contain dynamic data are - initialized in a declaration
- passed as a value parameter,
- or returned as a value of a method or function.
- The copy constructor solves this problem.
26Passing Objects by Value
- The default method used for pass by value, to
simply copy the private data members of the
object, is not the best way when a data member
pointer points to dynamic data. - Instead, you should write a copy constructor,
which makes a deep copy of the dynamic data in a
different memory location.
27Copy Constructor
- Copy constructor is a special member function of
a class that is implicitly called in these three
situations - passing object parameters by value,
- initializing an object variable in a
declaration, - returning an object as the return value of a
function.
28More about copy constructors
- When there is a copy constructor provided for a
class, the copy constructor is used to make
copies for pass by value. - You do not call the copy constructor.
- Like other constructors, it has no return type.
- Because the copy constructor properly defines
pass by value for your class, it must use pass by
reference in its definition.
29Copy Constructor
- DarrayDarray(const Darray Source)
- int I
- Size Source.Size
- ArrPtr new intSize
- for (I0 IltSize I)
- ArrPtrI Source.ArrPtrI