A shallow copy means that C++ copies each member of the class individually using the assignment operator. When classes are simple (eg. do not contain any dynamically allocated memory), this works very well.

For example, let’s take a look at our simple class:
class Sample
{
private:
int m_Sample;
public:
Sample(int nSample=0)
{
m_Sample = nSample;
}
};
When C++ does a shallow copy of this class, it will copy m_Sample using the standard integer assignment operator. Since this is exactly what we’d be doing anyway if we wrote our own copy constructor or overloaded assignment operator, there’s really no reason to write our own version of these functions!
However, when designing classes that handle dynamically allocated memory, member wise (shallow) copying can get us in a lot of trouble! This is because the standard pointer assignment operator just copies the address of the pointer — it does not allocate any memory or copy the contents being pointed to!
A deep copy duplicates the object or variable being pointed to so that the destination (the object being assigned to) receives it’s own local copy. This way, the destination can do whatever it wants to it’s local copy and the object that was copied from will not be affected. Doing deep copies requires that we write our own copy constructors and overloaded assignment operators.

Copy Constructor :-
1) First, we have to check to make sure cSource even has a string.
2) If it does, then we allocate enough memory to hold a copy of that string.
3) Finally, we have to manually copy the string using strncpy().
Some Example Code for the Copy Constructor Dynamically allocated memory:-
Sample::Sample(const Sample& cSource)
{
// Here m_nLength is not a pointer but a simple data member of type "int", we can shallow copy it
m_nLength = cSource.m_nLength;
// m_pchString is a pointer data member in Class Sample, so we need to deep copy it if it is non-null
if (cSource.m_pchString)
{
m_pchString = new char[m_nLength];
// Copy the string into our newly allocated memory
strncpy(m_pchString, cSource.m_pchString, m_nLength);
}
else
{
m_pchString = 0;
}
}
Overloaded assignment operator:-
1) Need to deallocate any value that this string is holding!
2) Then, we have to check to make sure cSource even has a string.
3) If it does, then we allocate enough memory to hold a copy of that string.
4) Finally, we have to manually copy the string using strncpy() and return the Object reference.
Some Example Code for the Assignment operator:-
Sample& Sample::operator=(const Sample& cSource)
{
//check for self-assignment
if (this == &cSource)
return *this;
delete[] m_pchString;
m_nLength = cSource.m_nLength;
// now we need to deep copy m_pchString
if (cSource.m_pchString)
{
m_pchString = new char[m_nLength];
// Copy the parameter the newly allocated memory
strncpy(m_pchString, cSource.m_pchString, m_nLength);
}
else
{
m_pchString = 0;
}
return *this;
}
Shallow copy lead to memory leak and some other side effects. So it is better to use Deep copy than the shallow copy in C++.
No comments:
Post a Comment