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

which is platform independent device used in computers

2 Answers  


i ahve v low % in 12th n BSC which is aroun 50 coz science was imposed on me......nw m doin MCA n my aggregate in above 74%,what shud i say if asked about low previous percentage??????

4 Answers  


What are two types of polymorphism?

0 Answers  


why we are declare the function in the abstract class even though we are declaring it in Derived class?

1 Answers   TCS,


i=20;k=0; for(j=1;k-i;k+=j<10?4:3) { cout<<k; } //please comment on the output

0 Answers  






What is polymorphism and types?

0 Answers  


Why can't we have instance(stack) of a class as a member of the same class like eg.Class A{A obj;} as we can have self refential pointer

0 Answers  


What type of Job you are providing?

0 Answers  


tell about copy constructor

3 Answers   Siemens,


Can you inherit a private class?

0 Answers  


DIFFRENCE BETWEEN STRUCTURED PROGRAMING AND OBJCET ORIENTED PROGRAMING.

5 Answers  


hi all..i want to know oops concepts clearly can any1 explain??

0 Answers   Eureka Forbes,


Categories