What is the difference between memcpy and memmove? Which is more
expensive & why?
plz comment on this...
thanks
What is the difference between memcpy and memmove? Which is more
expensive & why?
plz comment on this...
thanks
What have you managed to determine so far by reading the manual pages?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Quick googling could bring you the answer.
memcpy() is faster, but it's not safe for moving blocks of memory where the source and destination overlap. memmove() can be used to move memory in those cases.
well I got lot of answers, even befor eposting here. But I was looking for something, in depth reply (from programmer's perspective)...unfortunately I dint get it here...I myself will find out & will post here ....
well nice example you have given but that was not enough for me....
thanks for your comment
Why do you presume that one is more expensive than the other?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I think the usage and cost is situational, presuming one is more expensive than the other and to call that statement generic will not be wise.
Last edited by zombiezparadize; 08-03-2007 at 03:15 AM.
I agree with Zombie
It is quite possible to implement memmove such that if the data doesn't overlap,it's using memcpy() anyways (it's a TINY bit of overhead, but assuming we have a decent size of data to copy, that will be in the noise). And copying "backwards" (which you have to do under overlap circumstances) can be slower than copying forwards, but it does depend on the processor architecture, as well as the actual implementation in the C-library.
There is no strict way to say that one is more expensive than the other. The chances are that memcpy() is faster (less expensive) because it's more frequently used, and there are plenty of "optimized memcpy" implementations out there, whilst memmove is less frequently used, so the chances are that it's slightly less optimized in the first place, and has added complexity, whcih can cause it to be slower in itself.
--
Mats
As a general rule, a more specialized function should always be at least as efficient as a less specialized one, since the former can be implemented in terms of the latter. Also, using the more specialized function whenever possible helps make the code self-documenting - when done consistently, memcpy() says "the source and dest never overlap", and memmove() says "the source and dest can overlap". And the more specialized version is often easier to read - for example, sqrt(x) is easier to read than pow(x, 0.5). So as a general rule, the most specialized version that applies should be used - in this case, memmove() if source and dest can overlap, memcpy() if they never do.
From what I read it, memcpy() is most likely implemented directly moving the blocks of memory from src to dest. memmove(), on the other hand, might be implemented to utilize an extra buffer of sorts which would automatically incur extra cost.
It was assumption on my part based on this. I probably should not have included it in my post.
You can USUALLY cope with overlapping copy by doing something like this:
The above code is as an example, there are a bunch of better ways to do this (like checking if it can be done as "long" instead of char, etc, etc.Code:char *s; char *d; s = src; d = dest; if (overlap(src, dest, len)) s += len; d += len; while(len--) { *--s = *--d; } } else memcpy(src, dest, len); }
Last edited by matsp; 08-03-2007 at 04:43 AM. Reason: Fix loop condition...