



























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




























CMSC 202, Version 3/
CMSC 202, Version 3/
a copy of an existing object4) When assigning objects (x = y)
CMSC 202, Version 3/
CMSC 202, Version 3/
CMSC 202, Version 3/
-^
pointer for the copy and give it the same value asthe original pointer. So the copy and original willboth point to the same data. This is a grosssemantic error: changing the copy will change theoriginal! How can this create a serious securityproblem?
CMSC 202, Version 3/
-^
a pointer to the data instead of a copy of the data?This can be a hard error to catch in languages likeJava, which blur the distinctions of which variablesare pointers and which are not.
-^
CMSC 202, Version 3/
CMSC 202, Version 3/
CMSC 202, Version 3/
13
a
m_size m_theData
CMSC 202, Version 3/
When Does the Memory Get
Deallocated?
CMSC 202, Version 3/
16
a
m_size m_theData
a
m_size m_theData
Because the default copy constructoronly copied the contents of m_theDatain^
a into m_theData in
a
, both point
to the array allocated in
a1.
(Possible
dangling pointer
or other
logic
problem!)
CMSC 202, Version 3/
17
a
m_size m_theData
a
m_size m_theData
CMSC 202, Version 3/
When Is the Copy Constructor
Invoked?
Silently by the compiler when we• Pass by value:void someFunction(SmartArray array);• Return by value:
CMSC 202, Version 3/
When Is the Copy Constructor
Invoked? (cont’d)
SmartArray a1;// constructing a2 as a copy of a1SmartArray a2 = a1;
OR
SmartArray a2(a1);