Click to See Complete Forum and Search --> : deep and shallow copy
engrsanjiv
June 3rd, 2003, 12:44 PM
How deep and shallo copy works internally in copy of class data.
If i use copy constuctor then all data is copied by deep concept and when I use "=" equal to operator data is copied by shallow concept. what exactly is the differance between these two concepts.
Sanjiv
Linenoise
June 3rd, 2003, 01:34 PM
As I understand it, a shallow copy only copies the data members itself. Lets say you have a class that contains a single pointer, pString, to an array of characters. In a shallow copy, Copy.pString == Original.pString - the pointer is simply assigned the same value. In a deep copy, new memory is allocated for Copy.pString, and the data in Original.pString copied over to it. Copy.pString and Original.pString point to different locations.
Andreas Masur
June 3rd, 2003, 04:48 PM
By default the copy constructor and assignment operator are making so-called shallow copies of the object meaning that all of the member values will be copied. This works well if the members are values, but causes problems for members which actually points to dynamically allocated memory. The pointer will be copied - but not the memory it points to...this will result in having both members pointing to the same dynamically allocated memory.
Therefore, if you have a class with dynamic memory allocation a deep copy is required to guarantee that all members gets copied properly. To make a deep copy, you must write a copy constructor and overload the assignment operator.
class CFoo()
{
public:
// Constructor
CFoo() { m_pArray = new char[100]; }
// Destructor
~CFoo() { delete [] m_pArray; }
// Copy constructor
CFoo(const CFoo &refcSource)
{
// Allocate new space
m_pArray = new char[strlen(refcSource.m_pArray) + 1];
// Copy values
strcpy(m_pArray, refcSource.m_pArray);
}
// Assignment operator
CFoo& operator=(const CFoo &refcSource)
{
// Check whether it is the same instance
if(refcSource != this)
{
// Release old memory
delete [] m_pArray;
// Allocate new space
m_pArray = new char[strlen(refcSource.m_pArray) + 1];
// Copy values
strcpy(m_pArray, refcSource.m_pArray);
}
return *this;
}
private:
char *m_pArray;
};
galathaea
June 3rd, 2003, 05:55 PM
Note also that the distinction between copies applies to any resource sharing, not just pointers. When you program for various operating systems, you will find that the API exposes its resources to you in various formats (like the various forms of handles in some OSes). You must decide whether it is appropriate when your class is copied whether you desire only the resource identifier gets copied, or whether it is appropriate to create a whole new resource (as dtors may want to clean up the resource).
Graham
June 4th, 2003, 04:20 AM
Can I also point out that copying as a concept often does not apply to resource-owning classes, so one important consideration in the design phase is whether to disable copying or not.
engrsanjiv
June 5th, 2003, 04:23 PM
Thanks to all for their reply.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.