What is the difference between bitwise and memberwise copying? Surely if you copy the members then you will end up copying the bits representing the members anyway?
-
Do you mean using memcpy instead of assigning each member individually? – cppguy Feb 27 '13 at 22:15
-
Member-wise copying means that you copy an object by copying all of its data members. This may be equivalent to bit-wise copying if the members' types do *not* define a copy constructor. However, for those members which do have a copy-constructor, the copying logic is defined by the copy constructor and may not be equivalent to bit-wise copying. – Andy Prowl Feb 27 '13 at 22:16
-
possible duplicate of [What happens (exactly) if you leave out the copy-constructor in a C++ class?](http://stackoverflow.com/questions/5134155/what-happens-exactly-if-you-leave-out-the-copy-constructor-in-a-c-class) – Bo Persson Feb 27 '13 at 22:21
-
@BoPersson That other SO link you posted doesn't even contain the word "bit" once- therefore I don't consider it a duplicate.... – user997112 Feb 27 '13 at 22:27
-
@user - Consider what `memcpy` does - a bitwise copy – Bo Persson Feb 27 '13 at 22:30
-
@BoPersson I'm not saying memcpy doesn't do bitwise copy- I'm saying somebody who asks "whats the difference between bitwise and memberwise copying?" may not realise "bitwise" = memcpy and therefore the questions are targeted differently. – user997112 Feb 27 '13 at 22:43
4 Answers
class MyClass
{
public:
MyClass () : m_p (new int (5)) {}
~MyClass () {delete m_p;}
int* m_p;
};
MyClass a;
MyClass b;
memcpy (&a, &b, sizeof (a));
I just leaked the allocated int in 'a' by rewriting it's member variable without freeing it first. And now 'a' and 'b' have an m_p that is pointing to the same memory location and both will delete that address on destruction. The second attempt to delete that memory will crash.

- 3,611
- 2
- 21
- 36
-
In this example there are 2 allocations (different addresses) and 2 deletions of the same address. SO... one leaks and one gets deleted twice – cppguy Feb 27 '13 at 22:29
-
1You have two allocations and two deallocations. But you apply both deallocations to the same allocated object. This way you leak one of the two dynamically allocated objects, while the other is doubly deleted. – JoergB Feb 27 '13 at 22:29
-
- Bitwise copying: copy the object representation of an object as an uninterpreted sequence of bytes.
- Memberwise copying: copy each subobject of an object as appropriate for its type. For objects with a non-trivial copy constructor that means to invoke the copy constructor. For subobjects of trivially copyable type, that means bitwise copy.
Both are the same, so that the entire object is trivially copyable, if all subobjects are trivially copyable. (Class (sub)objects also must not have virtual member functions or virtual base classes.)

- 4,383
- 21
- 19
If you are binary copying an object then there may be internals such as reference counters that should not be copied. A bitwise copy would break this. A member copy will use the correct functions.

- 2,997
- 21
- 20
You might get in trouble when bitwise copying reference or pointer type members. Depends on what you really need and can handle by means of having a shallow or deep copy for the resulting class instance.

- 1
- 13
- 116
- 190