If destination and source are the same, does memmove
still "move" the data (or does it return directly)? What about realloc
; what if the new size is the same as the old size?
Return Value The memmove() function returns a pointer to dest .
Description. The memmove() function copies count bytes of src to dest. This function allows copying between objects that might overlap as if src is first copied into a temporary array.
That memmove might be slower than memcpy is because it is able to handle overlapping memory, but memmove still only copies the data once. profile it on the platform you're interested in the timings for. However, the chances of you writing a better memmove than memmove seems unlikely.
Answer: memcpy() function is is used to copy a specified number of bytes from one memory to another. memmove() function is used to copy a specified number of bytes from one memory to another or to overlap on same memory.
That's really going to be implementation-specific. It would be good practice to do so, sure, but it really depends which implementation you mean.
It's going to work either way, but presumably a suitably clever implementation would check for overlapping segments (and particularly for the case where source == dest
) and deal with it appropriately.
As far as I know no standard gives any promises about returning immediately in such case, so you should not expect this behavior.
Better do not pass invalid pointers in hope it's not going to access the data ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With