what is Deep Copy, shallow copy and bitwises copy.?

Discussion in 'C++' started by saxenavaibhav17@gmail.com, Aug 30, 2006.

  1. Guest

    what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy?
    and what is the difference between them?


    pls help
    vaibhav
     
    , Aug 30, 2006
    #1
    1. Advertising

  2. mlimber Guest

    mlimber, Aug 30, 2006
    #2
    1. Advertising

  3. On 30 Aug 2006 06:06:40 -0700, "mlimber" <> wrote:
    > wrote:
    >> what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy?
    >> and what is the difference between them?

    >
    >What is a homework question? See this FAQ:


    It doesn't sound like homework. The question is not covered in the C++
    FAQ. I cannot quickly find a link with decent explanations. The
    answers are not entirely trivial (e.g.: For which objects is a bitwise
    copy appropriate? When is a memberwise copy shallow, when deep? ...).
    Nobody should hold back answers.
    BTW, copy constructors almost never need to be implemented but should
    be made private (without implementation).

    Best regards,
    Roland Pibinger
     
    Roland Pibinger, Aug 30, 2006
    #3
  4. Roland Pibinger wrote:

    <snip>

    > BTW, copy constructors almost never need to be implemented but should
    > be made private (without implementation).


    why? Doesn't copy construction very often make semantic sense?


    --
    Nick Keighley
     
    Nick Keighley, Aug 30, 2006
    #4
  5. > what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy?

    (1) Shallow Copy and Memberwise copy (They're equivalent)

    struct MyStruct {
    int *p1;
    double *p2;
    };

    int main()
    {
    MyStruct obj1 = {new int,new double};

    MyStruct obj2;

    obj2 = obj1; /* Shallow or Memberwise */
    }

    (2) Bitwise Copy

    #include <cstdlib>
    using std::memcpy;

    struct MyStruct {
    int *p1;
    double *p2;
    };

    int main()
    {
    MyStruct obj1 = {new int,new double};

    MyStruct obj2;

    memcpy(&obj2,&obj1,sizeof obj2); /* Shallow / Memberwise */
    }

    Note that a Bitwise Copy will also copy any padding between and after
    members.

    (3) Deep Copy

    class MyClass {
    int *p;

    public:

    MyClass() : p(new int) {}

    MyClass(MyClass const &) : p(new int) {}
    };

    int main()
    {
    MyClass obj1,obj2;

    obj2 = obj1; /* Deep Copy */
    }

    --

    Frederick Gotham
     
    Frederick Gotham, Aug 30, 2006
    #5
  6. Frederick Gotham posted:


    > memcpy(&obj2,&obj1,sizeof obj2); /* Shallow / Memberwise */



    That comment should read /* Bitwise Copy */

    --

    Frederick Gotham
     
    Frederick Gotham, Aug 30, 2006
    #6
  7. Frederick Gotham schrieb:
    > (3) Deep Copy
    >
    > class MyClass {
    > int *p;
    >
    > public:
    >
    > MyClass() : p(new int) {}


    MyClass() : p(new int()) {}

    >
    > MyClass(MyClass const &) : p(new int) {}


    MyClass(MyClass const &rhs) : p(new int(*rhs.p)) {}

    Should copy something.

    > };
    >
    > int main()
    > {
    > MyClass obj1,obj2;
    >
    > obj2 = obj1; /* Deep Copy */
    > }
    >


    --
    Thomas
     
    Thomas J. Gritzan, Aug 30, 2006
    #7
  8. On Wed, 30 Aug 2006 17:33:50 GMT, Frederick Gotham
    <> wrote:
    >> what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy?

    >
    >(1) Shallow Copy and Memberwise copy (They're equivalent)


    Are they equivalent? Doesn't 'memberwise' just mean 'member by member'
    which may result in a deep or a shallow copy depending on the members.

    Best wishes,
    Roland Pibinger
     
    Roland Pibinger, Aug 30, 2006
    #8
  9. On 30 Aug 2006 08:50:43 -0700, "Nick Keighley"
    <> wrote:
    >Roland Pibinger wrote:
    >> BTW, copy constructors almost never need to be implemented but should
    >> be made private (without implementation).

    >
    >why? Doesn't copy construction very often make semantic sense?


    For 'value types' the compiler-generated copy constructor is usually
    the best. OTOH, objects (in the OO-sense) typically are non-copyable
    and therfore need no copy constructor implementation, just a
    private(ly) declared copy constructor (to make them non-copyable).

    Best wishes,
    Roland Pibinger
     
    Roland Pibinger, Aug 30, 2006
    #9
  10. loufoque Guest

    Roland Pibinger wrote :

    > OTOH, objects (in the OO-sense) typically are non-copyable


    Please don't confuse your personal beliefs with programming theory.
    I see no reason why objects would typically be non-copyable. Especially
    in C++ which is based on copy semantics.
    On the contrary, most objects should be copyable, unless they manage
    some kind of unique resource (sockets, files, ...).

    Moreover, if you want to make your object non-copyable clearly says so
    instead of saying "write a private constructor". Actually, inheriting
    from boost::noncopyable is probably clearer.
     
    loufoque, Aug 30, 2006
    #10
  11. On Wed, 30 Aug 2006 22:18:30 +0200, loufoque
    <> wrote:
    >Roland Pibinger wrote :
    >
    >> OTOH, objects (in the OO-sense) typically are non-copyable

    >
    >Please don't confuse your personal beliefs with programming theory.


    I don't.

    >I see no reason why objects would typically be non-copyable.


    Why should objects be duplicated? What should it mean? Eg.

    Person dilbert;
    Person wally (dilbert); // ???

    >Especially in C++ which is based on copy semantics.


    and reference/pointer semantics ...

    >On the contrary, most objects should be copyable, unless they manage
    >some kind of unique resource (sockets, files, ...).


    Objects have identity. The can be referenced by multiple refences but
    it hardly makes sense to duplicate them. (Even in OO languages like
    Java objects are hardly ever 'cloned' even though a Clonable interface
    exists).

    >Moreover, if you want to make your object non-copyable clearly says so
    >instead of saying "write a private constructor". Actually, inheriting
    >from boost::noncopyable is probably clearer.


    Making the class dependant on Boost just to make it non-coppyable??

    Best wishes,
    Roland Pibinger
     
    Roland Pibinger, Aug 30, 2006
    #11
  12. Old Wolf Guest

    Roland Pibinger wrote:
    > Why should objects be duplicated? What should it mean? Eg.
    >
    > Person dilbert;
    > Person wally (dilbert); // ???


    Sheep dolly (bill);

    Anyway, copy-constructors are also used for moving objects
    around in memory. For example, returning an object from a
    function, or storing an object in a container than can re-allocate
    its memory. The new object is copy-constructed from the
    original and the original is then destroyed.
     
    Old Wolf, Aug 31, 2006
    #12
  13. Guest

    Roland Pibinger wrote:
    > On Wed, 30 Aug 2006 22:18:30 +0200, loufoque
    > <> wrote:
    > >Roland Pibinger wrote :
    > >
    > >> OTOH, objects (in the OO-sense) typically are non-copyable

    > >
    > >Please don't confuse your personal beliefs with programming theory.

    >
    > I don't.
    >
    > >I see no reason why objects would typically be non-copyable.

    >
    > Why should objects be duplicated? What should it mean? Eg.
    >
    > Person dilbert;
    > Person wally (dilbert); // ???
    >
    > >Especially in C++ which is based on copy semantics.

    >
    > and reference/pointer semantics ...
    >
    > >On the contrary, most objects should be copyable, unless they manage
    > >some kind of unique resource (sockets, files, ...).

    >
    > Objects have identity. The can be referenced by multiple refences but
    > it hardly makes sense to duplicate them. (Even in OO languages like
    > Java objects are hardly ever 'cloned' even though a Clonable interface
    > exists).


    Sorry, but saying objects should not be copied in such a general way
    is just plain BS.

    One (out of many) more example is if you compose your object from
    others. Then you might need to copy the contained objects form several
    others to form a newly composed (individual) object.

    Marc
     
    , Aug 31, 2006
    #13
  14. On 30 Aug 2006 22:21:51 -0700, "Old Wolf" <>
    wrote:
    >Roland Pibinger wrote:
    >> Why should objects be duplicated? What should it mean? Eg.
    >>
    >> Person dilbert;
    >> Person wally (dilbert); // ???

    >
    > Sheep dolly (bill);
    >
    >Anyway, copy-constructors are also used for moving objects
    >around in memory. For example, returning an object from a
    >function, or storing an object in a container than can re-allocate
    >its memory. The new object is copy-constructed from the
    >original and the original is then destroyed.


    What you can do in C++ is different from what is desirable. Having
    duplicate objects (objects (in the OO sense), not values) of the same
    entity in your program is harldy ever a Good Thing (at least I can
    quickly think of no case where it is).

    Best regards,
    Roland Pibinger
     
    Roland Pibinger, Aug 31, 2006
    #14
  15. Kai-Uwe Bux Guest

    Roland Pibinger wrote:

    > On 30 Aug 2006 22:21:51 -0700, "Old Wolf" <>
    > wrote:
    >>Roland Pibinger wrote:
    >>> Why should objects be duplicated? What should it mean? Eg.
    >>>
    >>> Person dilbert;
    >>> Person wally (dilbert); // ???

    >>
    >> Sheep dolly (bill);
    >>
    >>Anyway, copy-constructors are also used for moving objects
    >>around in memory. For example, returning an object from a
    >>function, or storing an object in a container than can re-allocate
    >>its memory. The new object is copy-constructed from the
    >>original and the original is then destroyed.

    >
    > What you can do in C++ is different from what is desirable. Having
    > duplicate objects (objects (in the OO sense), not values) of the same
    > entity in your program is harldy ever a Good Thing (at least I can
    > quickly think of no case where it is).


    Keep in mind, though, that C++ supports various other viable styles of
    programming, where your orignial remark

    > BTW, copy constructors almost never need to be implemented but should
    > be made private (without implementation).


    hardly is applicable. I find that value semantics is the most appropriate
    model for most of my programming and copy constructors need to be defined
    in most cases. In the vast variety of what can be done well in C++, strict
    OO programming is but a small fraction; and advice targeted to OO design
    proper is of very limited scope when talking C++.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Aug 31, 2006
    #15
  16. Jim Langston Guest

    "Roland Pibinger" <> wrote in message
    news:...
    > On 30 Aug 2006 22:21:51 -0700, "Old Wolf" <>
    > wrote:
    >>Roland Pibinger wrote:
    >>> Why should objects be duplicated? What should it mean? Eg.
    >>>
    >>> Person dilbert;
    >>> Person wally (dilbert); // ???

    >>
    >> Sheep dolly (bill);
    >>
    >>Anyway, copy-constructors are also used for moving objects
    >>around in memory. For example, returning an object from a
    >>function, or storing an object in a container than can re-allocate
    >>its memory. The new object is copy-constructed from the
    >>original and the original is then destroyed.

    >
    > What you can do in C++ is different from what is desirable. Having
    > duplicate objects (objects (in the OO sense), not values) of the same
    > entity in your program is harldy ever a Good Thing (at least I can
    > quickly think of no case where it is).


    std::vector<CPlayer> PlayerList;
    CPlayer Player;
    Player.LoadData( SomeFile );
    PlayerList.push_back( Player ); // Ooops, need copy constuctor for that
     
    Jim Langston, Aug 31, 2006
    #16
  17. On Thu, 31 Aug 2006 04:24:55 -0400, Kai-Uwe Bux <>
    wrote:
    >Keep in mind, though, that C++ supports various other viable styles of
    >programming, where your orignial remark
    >
    >> BTW, copy constructors almost never need to be implemented but should
    >> be made private (without implementation).

    >
    >hardly is applicable. I find that value semantics is the most appropriate
    >model for most of my programming and copy constructors need to be defined
    >in most cases.


    What I mean is that copy constructors are either trivial for values
    (and therfore need not be implemented) or should be made private (and
    left unimplemented) for objects.

    Best wishes,
    Roland Pibinger
     
    Roland Pibinger, Aug 31, 2006
    #17
  18. On Thu, 31 Aug 2006 02:21:59 -0700, "Jim Langston"
    <> wrote:
    >std::vector<CPlayer> PlayerList;
    >CPlayer Player;
    >Player.LoadData( SomeFile );
    >PlayerList.push_back( Player ); // Ooops, need copy constuctor for that


    PlayerList.push_back( Player ); // Ooops, why would you duplicate
    your CPlayers??
     
    Roland Pibinger, Aug 31, 2006
    #18
  19. Kai-Uwe Bux Guest

    Roland Pibinger wrote:

    > On Thu, 31 Aug 2006 04:24:55 -0400, Kai-Uwe Bux <>
    > wrote:
    >>Keep in mind, though, that C++ supports various other viable styles of
    >>programming, where your orignial remark
    >>
    >>> BTW, copy constructors almost never need to be implemented but should
    >>> be made private (without implementation).

    >>
    >>hardly is applicable. I find that value semantics is the most appropriate
    >>model for most of my programming and copy constructors need to be defined
    >>in most cases.

    >
    > What I mean is that copy constructors are either trivial for values
    > (and therfore need not be implemented) or should be made private (and
    > left unimplemented) for objects.


    That copy constructors for classes with value semantics are trivial, is not
    true in my experience. It does not apply to any container like class, e.g.,
    a matrix class. Similarly classes that model graphs or cell complexes are
    very much similar to containers -- it would be hard and inefficient to try
    implementing them just using standard containers. Also, whenever you choose
    a COW implementation for efficiency, you will have to deal with your own
    copy constructor.

    The validity of your remark depends very much on the problem domain that you
    are dealing with. The way you state it, to me, seems to be a vast
    over-generalization.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Aug 31, 2006
    #19
  20. Kai-Uwe Bux wrote:
    > Roland Pibinger wrote:
    >
    > > On 30 Aug 2006 22:21:51 -0700, "Old Wolf" <>
    > > wrote:
    > >>Roland Pibinger wrote:
    > >>> Why should objects be duplicated? What should it mean? Eg.
    > >>>
    > >>> Person dilbert;
    > >>> Person wally (dilbert); // ???
    > >>
    > >> Sheep dolly (bill);
    > >>
    > >>Anyway, copy-constructors are also used for moving objects
    > >>around in memory. For example, returning an object from a
    > >>function, or storing an object in a container than can re-allocate
    > >>its memory. The new object is copy-constructed from the
    > >>original and the original is then destroyed.

    > >
    > > What you can do in C++ is different from what is desirable. Having
    > > duplicate objects (objects (in the OO sense), not values) of the same
    > > entity in your program is harldy ever a Good Thing (at least I can
    > > quickly think of no case where it is).

    >
    > Keep in mind, though, that C++ supports various other viable styles of
    > programming, where your orignial remark
    >
    > > BTW, copy constructors almost never need to be implemented but should
    > > be made private (without implementation).

    >
    > hardly is applicable. I find that value semantics is the most appropriate
    > model for most of my programming and copy constructors need to be defined
    > in most cases. In the vast variety of what can be done well in C++, strict
    > OO programming is but a small fraction; and advice targeted to OO design
    > proper is of very limited scope when talking C++.
    >
    >
    > Best
    >
    > Kai-Uwe Bux


    I agree. I only disable copy-ctors in singletons and classes wrapping
    resource handles.

    Diego Martins
    HP
     
    Diego Martins, Aug 31, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VisionSet
    Replies:
    8
    Views:
    4,961
    Tris Orendorff
    Apr 29, 2004
  2. Tony Johansson

    deep and shallow copy

    Tony Johansson, May 19, 2005, in forum: C++
    Replies:
    5
    Views:
    9,869
    Rapscallion
    May 19, 2005
  3. Alex
    Replies:
    2
    Views:
    1,287
  4. Replies:
    1
    Views:
    560
    Victor Bazarov
    Jun 24, 2005
  5. shuisheng
    Replies:
    4
    Views:
    853
    Uenal S. Mutlu
    Dec 17, 2006
Loading...

Share This Page