Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

memmove and memcpy 4

Status
Not open for further replies.

FR

Programmer
Jul 15, 2000
1
0
0
KR
hi there,

i was trying to explain myselft the difference between memcpy and memmove. But i couldn't site any good example or at least site any difference between 'em. the only thing i got from K&R C is that the difference lies when source and destination for copying overlap. i tried do that but there was no difference of use(with both memcpy and memmove)
can anybody please explain this scenario.

thanks in advance for sparing time,
satya
 
Well, i'm not sure what you miss about the 2 functions...
It's very clear to me... memcpy COPIES bytes...
memmove... MOVES bytes...
Or maybe i am missing the point of your question?
 
That's simply incorrect. Both memcpy() and memmove() copy.

From C99:

Synopsis

1 #include <string.h>

void *memmove(void *s1, const void *s2, size_t n);

Description

2 The memmove function copies n characters from the object pointed to by s2 into the object pointed to by s1. Copying takes place as if the n characters from the object pointed to by s2 are first copied into a temporary array of n characters that does not overlap the objects pointed to by s1 and s2, and then the n characters from the temporary array are copied into the object pointed to by s1.

The difference is the behavior is undefined if you use memcpy() where the source and destination overlap.

e.g.:

char src[]=&quot;hello&quot;;
char *ptr=src+2;

memmove(src,ptr,2); /* ok, src now is &quot;llllo&quot; */

char src[]=&quot;hello&quot;;
char *ptr=src+2;

memcpy(src,ptr,2); /* undefined behavior, src and
* ptr point to the same chunk
* of memory (they overlap)
*/

So, the functions produce exactly the same results *if* the source and destination pointers don't overlap. If they do overlap, memmove() is guaranteed to work where memcpy() isn't. memcpy() might work (as it apparently did in satya's test), but it isn't something you should rely on.

If you suspect that the source and destination might overlap, use memmove(), otherwise use memcpy() (or strcpy() if you're copying strings). Also memmove() is generally slower than memcpy(), which is a reason not to use it for all copying.
Russ
bobbitts@hotmail.com
 
Russ, you mentioned that memmove is slower than memcpy. (obviously, because memmove creates a temporary buffer during the copy)

Do you know how much slower it is? If I have a program that has 1000 memcpy's, and I replace them with memmove's, how much slower can I anticipate the program to be? I'm guessing that it will depend on the size of memory that I'm moving. Let's assume a size of 8 bytes.
 
> (obviously, because memmove creates a temporary buffer during the copy)
It would be a pretty useless implementation if it did this.

Read the text from the standard carefully.
---
Copying takes place as if the n characters from the object pointed to by s2 are first copied into a temporary array
---
It's describing the effect, not the actual implementation.

The problem can be solved by looking at the type of overlap, and deciding whether to copy in start->end order, or end->start order.

Examples
Code:
src:              ++++++++++
dst: ----------
No overlap, copy in any order

Code:
src: ++++++++++
dst:    ----------
Start of dst overlaps, copy in end->start order

Code:
src:    ++++++++++
dst: ----------
End of dst overlaps, copy in start->end order

> Do you know how much slower it is?
Depends entirely on your implementation of memmove() and memcpy(), and what sort of overlap you have (see above).
In one of the overlap cases, memmove() will simply call memcpy()
 
Thanks for the info and correcting my misinterpretation!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top