What is deep and shalow copy?

Answer Posted / naga

Consider two objects, A and B, which each refer to two
memory blocks xi and yi (i = 1, 2,...). Think of A and B as
strings and of xi and yi (i = 1, 2,...) as the characters
they contain.





Shallow copy
One of them is the shallow copy. In this process B is
attached to the same memory block as A. This is otherwise
known as address copy

Deep copy
An alternative is a deep copy. Here the data is actually
copied over.

Example:
class base
{
public:
int i;
base()
{
i=0;
}
base(int j)
{
i=j;
}
};
main()
{
using namespace std;
base *p1=new base(23);
base *p2;
//Shallow copy, here we are using pointers and we are
copying content of one pointer to another.so address will
get copied.
p2=p1; // address is copied
cout<<"\naddress of P1:"<<p1;
cout<<"\nvalue at p1:"<<p1->i;
cout<<"\naddress of P2:"<<p2;
cout<<"\nvalue at p2:"<<p2->i;
delete p2;
cout<<"\naddress of P1 after delete:"<<p1;
cout<<"\nvalue in P2 after delete:"<<p2->i;

//DEEP copy, here we are creating objects and we are
copying content of one object to another.So content will
get copied.

base o1(67);
base o2;
o2=o1;//contents are copied. But, the addresses
remained different
cout<<"\nvalue in o1:"<<o1.i;
cout<<"\nvalue in o2 after copy:"<<o2.i<<endl;
return 0;
}

output:
address of P1:0x00323C88
value at p1:23
address of P2:0x00323C88
value at p2:23
address of P1 after delete:0x00323C88
value in P2 after delete:-572662307
value in o1:67
value in o2 after copy:67

Is This Answer Correct ?    18 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are oops functions?

585


write a program to find 2^n+1 ?

1553


What is pure oop?

599


what type of question are asked in thoughtworks pair programming round ?

1766


Is data hiding and abstraction same?

569






What is encapsulation and abstraction? How are they implemented in C++?

637


What is class in oop with example?

623


Why do we use oops?

594


What does enum stand for?

617


#include #include #include #include void select(char *items, int count); int main(void) { char s[255]; printf("Enter a string:"); gets(s); select(s, strlen(s)); printf("The sorted string is: %s.\n", s); getch(); return 0; } void select(char *items, int count) { register int a, b, c; int exchange; char t; for(a = 0; a < count-1; ++a) { exchange = 0; c = a; t = items[ a ]; for(b = a + 1; b < count; ++b) { if(items[ b ] < t) { c = b; t = items[ b ]; exchange = 1; } } if(exchange) { items[ c ] = items[ a ]; items[ a ] = t; } } } design an algorithm for Selection Sort

2070


What is the real time example of inheritance?

645


What exactly is polymorphism?

612


What is the importance of oop?

614


Why oops is important?

616


They started with the brief introduction followed by few basic C++ questions on polumorphism, inheritance and then virtual functions. What is polymorphims? How you will access polymorphic functions in C? How virtual function mechanism works?

1395