Hi,
let me put some code,
// this is an example of shallow copying you should avoid if you have members
// which are pointers
struct Sample
{
char * ptrData;
};
main()
{
Sample s1,s2;
s1.ptrData = malloc(100); // allocate memory
strcpy(s1.ptrData,"hello world");
s1 = s2; // both s1.ptrData and s2.ptrData point to the same memory location
// due to shallow or member wise copying of data
free(s1.ptrData); // delete the memory pointed by s1.ptrData
strcpy(s2.ptrData,"god help this code"); // get ready for crash
}
// now a good practise to have a method which does deep copying when we have
// pointer as members
struct Sample
{
char * ptrData;
};
main()
{
Sample s1,s2;
s1.ptrData = malloc(100);
strcopy(s1.ptrData,"hello world");
CopyStructures(&s1,&s2)
free(s1.ptrData); // delete the memory pointed by s1.ptrData
strcopy(s2.ptrData,"smart code"); // will work no issues here since seperate
// memory
}
void CopyStructures(Sample * src,Sample * dest)
{
// allocate memory for destination
dest->ptrData = malloc(100);
// now copy data for src to destination
strcpy(dest->ptrData,src->ptrData)
}
so that deep copying, hope that help, i have explained you in context of structures and this holds good for c++ also when you use classes but i will not go into that now