Followers 0

# C++ std::move() vs std::memcpy()

## 14 posts in this topic

I was working on a vector-like container, and while writing a resize() function came across an interesting question.

When moving an object between locations, when would std::move() not give the same results as simply a std::memcpy()?

There were only two situations that I could think of.  One was when the move constructor (or assignment operator) was intentionally used to do something strange (ie. not actually move).  The other that when using std::move() the src object could (in theory) be reused, or at the very least has to be destroyed, where-as with the std::memcpy() neither applies.

But in every other case I could think of (pointers, handles, reference counting) simply copying the bits should have the same result as calling std::move().?

1

##### Share on other sites

You might want to read the documentation of std::is_trivially_copyable.

I know the difference between POD and non-POD types.  Clearly when copying you can't just memcpy(), things like pointers, handles, COM interfaces, reference counting, ect... need to be updated.

Closer to the topic is: http://en.cppreference.com/w/cpp/types/is_move_constructible.  Which has std::is_trivially_move_constructable.  Apart from throwing exceptions (which move is not supposed to do, much like destructors) what does calling std::move give you that std::memcpy() doesn't?

0

##### Share on other sites

Actually, my link does list all the important information. As the notes say:

Objects of trivially-copyable types are the only C++ objects that may be safely copied with std::memcpy or serialized to/from binary files with std::ofstream::write()/std::ifstream::read(). In general, a trivially copyable type is any type for which the underlying bytes can be copied to an array of char or unsigned char and into a new object of the same type, and the resulting object would have the same value as the original.

The properties listed above that cover the possible reasons you have already sliced into a bit in a formal way.

1

##### Share on other sites

Actually, my link does list all the important information. As the notes say:

Objects of trivially-copyable types are the only C++ objects that may be safely copied with std::memcpy or serialized to/from binary files with std::ofstream::write()/std::ifstream::read(). In general, a trivially copyable type is any type for which the underlying bytes can be copied to an array of char or unsigned char and into a new object of the same type, and the resulting object would have the same value as the original.

The properties listed above that cover the possible reasons you have already sliced into a bit in a formal way.

A) I'm talking about std::move() vs std::memcpy(), not copying.  What you've posted covers copies, not moves.

B) I want to know WHY is shouldn't be done.

1

##### Share on other sites
Are you talking in general, or concerning POD types only?

If in general, then you can't memcpy objects. If specifically POD, then they're likely equivalent.

i.e. You're not allowed to use memcpy on non-POD objects, so the question only applies to POD objects....
Assuming you're breaking the rules by memcpyijg non-POD objects, haven't you answered your own question -- any time that the move constructor does any real work (refcounting, swapping pointers, etc), then obviously this work won't be done by a memcpy.
1

##### Share on other sites

If specifically POD, then they're likely equivalent.

in pod land, as i recall, copy is a straight copy, and move is used for overlapping memory addresses (IE uses an intermediate buffer of some sort). since i never deal with overlapping addresses, i only use memcpy and then really only via ZeroMemory()

.

0

##### Share on other sites

Clearly I'm not being clear.

I understand what a POD type is, and what it allows you to do.  I'm not talking about copying.  No copies.  Forget copying.  I'm only talking about move here.  I also understand what a copy constructor is, what a move constructor is, and why you would use one over another.

What I want to know is: under what circumstances would using std::memcpy() not yield the same results as std::move()?  Again, NOT copy, but move.  The ones that I can thinks of are:

1) std::move() throws an exception (either through the move constructor or the move assignment operator, whichever one you're using), which is generally considered bad, like destructor throwing bad.

2) in the case of hardware mapped memory, like an object place-constructed in a particular segment of memory by the OS

3) where a move constructor (or assignment operator) is intentionally deleted or not accessible, but again this kinda leads back into the original question, cause I can't think of any situation where copy would be allowed but moving disabled apart from 2

4) where the src object of a move has to (or can be) re-used

I understand that std::move(), std::memcpy(), and normal copying all copy bytes around.  But in the context of C++ a move is different than a copy.  So yes I am talking about std::memcpy(), but I'm talking about move semantics not copy semantics.  POD types and std::is_trivially_copyable refer to copy semantics.  For example a class like:

struct Object {
char* data;
Object () : data(new char[10]);
virtual ~Object () { delete[] data; }
};

is not trivially copyable (you'd have two objects pointing to the same data on the heap), but... is it trivially moveable?  There are no exceptions thrown.  The pointer will move properly with a std::memcpy(), as there will be still only one owner.  As long as the src isn't destructed we don't have a data leak.  Does the hidden v-table pointer get copied properly?  Will something else get mangled?

I hope that makes my question clearer.

1

##### Share on other sites

One situation where memcpy() won't work but std::move() will is when the object's constructors and destructor handle self-registration. For example, if it registers itself as part of the root set to a garbage collector by giving the garbage collector it's address. If you just copy the the memory without updating the root set for the GC, the GC will be trying to use the old memory location for its sweeps.

Didn't think of that one, thank-you.

In adition to SiCrane's example, another thing that could complicate moving is pointers into the object itself. For example, a small vector might have a pointer that points within the object when there are few members, and to allocated memory when there are lots. That pointer would need to be updated to point within the moved object.

Another good point.
0

##### Share on other sites

I'm not sure if someone mentioned this, but a clear difference when your class allocates memory is that std::memcpy will not remove ownership from the original. So when your object dies, it will release the memory, but the object you copied will still hold a pointer to the released memory, causing a crash down the road. std::move, on the other hand, will release ownership from the original object (zeroing pointers, etc) so that it can be destroyed without affecting the state of the target object.

A move affects both the source and the destination, while a copy only affects the destination.

1

##### Share on other sites

I'm not sure if someone mentioned this, but a clear difference when your class allocates memory is that std::memcpy will not remove ownership from the original. So when your object dies, it will release the memory, but the object you copied will still hold a pointer to the released memory, causing a crash down the road. std::move, on the other hand, will release ownership from the original object (zeroing pointers, etc) so that it can be destroyed without affecting the state of the target object.

A move affects both the source and the destination, while a copy only affects the destination.

Exactly.

struct Object {
char* data;
Object () : data(new char[10]);
virtual ~Object () { delete[] data; }
};
is not trivially copyable (you'd have two objects pointing to the same data on the heap), but... is it trivially moveable?  There are no exceptions thrown.  The pointer will move properly with a std::memcpy(), as there will be still only one owner.  As long as the src isn't destructed we don't have a data leak.  Does the hidden v-table pointer get copied properly?  Will something else get mangled?

The pointer will NOT move properly. It gets copied properly. There are now 2 owners, not 1. You said "as long as src isn't destructed" but that's an important detail that highlights a major difference between memcpy and move.

Edited by achild
1

## Create an account

Register a new account