While reading about memmove I read that it can handle *MEMORY OVERLAPS*but I am unable to get how a memmory overlap can occur between two strings and how can this function still copy the block of memory correctly.
+2
A:
memmove(p+1, p, 42);
Which requires moving the bytes starting at the end. Memcpy() makes a mess of it.
Hans Passant
2010-09-03 16:28:31
Strictly speaking, `memcpy` has undefined behavior if used this way. It could conceivably work, for example if it were written assuming backwards copying is faster, or if `memcpy` were just implemented as a call to `memmove`, but you should never rely on such things. :-)
R..
2010-09-04 01:05:30
why would one ever use memcpy if he knows memmove ;)
fahad
2010-09-04 01:08:24
Because memcpy() is faster.
Hans Passant
2010-09-04 06:21:35
@hans Does memmove() copy the all the block at once? I mean it takes the block and copies it or copies byte by byte?
fahad
2010-09-05 07:16:41
Usually 4 bytes at a time, it depends on the implementation. Why don't you just take a look at the source code?
Hans Passant
2010-09-05 07:36:53
+2
A:
"Memory overlap" does not occur by itself. It is you who can supply the memmove
function with memory regions that overlap. Take two pointers into the same array and you can easily end up with overlapping memory regions.
Of course, you can also easily create overlapping objects through unions.
It is not clear what you mean by the second part of the question ("how can this function still copy the block of memory correctly"). Where do you see the problem here?
AndreyT
2010-09-03 16:37:31