What is deep and shalow copy?

Answers were Sorted based on User's Feedback



What is deep and shalow copy?..

Answer / rajk

A shallow copy of an object copies all of the member field
values. This works well if the fields are values, but may
not be what you want for fields that point to dynamically
allocated memory. The pointer will be copied. but the
memory it points to will not be copied -- the field in both
the original object and the copy will then point to the
same dynamically allocated memory, which is not usually
what you want. The default copy constructor and assignment
operator make shallow copies.

A deep copy copies all fields, and makes copies of
dynamically allocated memory pointed to by the fields. To
make a deep copy, you must write a copy constructor and
overload the assignment operator.

Deep copy needs,
If an object has pointers to dynamically allocated memory,
and the dynamically allocated memory needs to be copied
when the original object is copied, then a deep copy is
required.

A class that requires deep copies will generally need:

i )a destructor to delete the dynamically allocated memory.

ii) a copy constructor to make a copy of the dynamically
allocated memory.

iii) an overloaded assignment operator to make a copy of
the dynamically allocated memory.

Is This Answer Correct ?    20 Yes 0 No

What is deep and shalow copy?..

Answer / 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

What is deep and shalow copy?..

Answer / hemanth

Deep Copy and Shallow Copy

The terms "deep copy" and "shallow copy" refer to the way
objects are copied, for example, during the invocation of a
copy constructor or assignment operator. In a deep copy
(also called "memberwise copy"), the copy operation
respects object semantics. For example, copying an object
that has a member of type std::string ensures that the
corresponding std::string in the target object is copy-
constructed by the copy constructor of class std::string.


class A
{
string s;
};
A a;
A b;
a=b; //deep copy
When assigning b to a, the compiler-generated assignment
operator of class A first invokes the assignment operator
of class std::string. Thus, a.s and b.s are well-defined,
and they are probably not binary-identical. On the other
hand, a shallow copy (also called "bitwise copy") simply
copies chunks of memory from one location to another. A
memcpy() operation is an example of a shallow copy. Because
memcpy() does not respect object semantics, it will not
invoke the copy constructor of an object. Therefore, you
should never use memcpy() to copy objects. Use it only when
copying POD (Plain Old Data) types: ints, floating point
numbers, and dumb structs.

Is This Answer Correct ?    4 Yes 0 No

Post New Answer

More OOPS Interview Questions

Can static class have constructor?

0 Answers  


What is abstraction with example?

0 Answers  


why constructor cannt be declar virtually? why destructor cannt be overloaded?

2 Answers   Infosys,


There are 2 empty jars of 5 and 3 liters capacity. And a river is flowing besides. I want to measure 4 liters of wanter using these 2 jars. How do you do this?

4 Answers  


What are the 4 pillars of oop?

0 Answers  






Why is oop better than procedural?

0 Answers  


Iam doing my project on instant messaging , if you any new ideas regarding this project ,please suggest it?

2 Answers  


What are the OOPS concepts?

106 Answers   A1 Technology, Bajaj, CTS, EDS, HP, Infosys, Intel, Microsoft, MNC, Persistent, PlanetSoft, ProConstructor, TCS, Virtusa, Wipro, YSoft Solutions,


How do you answer polymorphism?

0 Answers  


what is new operator in c++

1 Answers  


What is difference between multiple inheritance and multilevel inheritance?

0 Answers  


What are the components of marker interface?

0 Answers  


Categories