tags:

views:

50

answers:

2

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
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..
why would one ever use memcpy if he knows memmove ;)
fahad
Because memcpy() is faster.
Hans Passant
@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
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
+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
I dont get how would a function handle memory overlapping problem
fahad