memcpy Vs memmove

This is a discussion on memcpy Vs memmove within the C Programming forums, part of the General Programming Boards category; What is the difference between memcpy and memmove? Which is more expensive & why? plz comment on this... thanks...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    25

    memcpy Vs memmove

    What is the difference between memcpy and memmove? Which is more
    expensive & why?

    plz comment on this...

    thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    25
    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

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    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 support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Day Dreamer
    Join Date
    Apr 2007
    Posts
    45
    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.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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.

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by Salem View Post
    Why do you presume that one is more expensive than the other?
    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.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MacGyver View Post
    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:
    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);
    }
    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.
    Last edited by matsp; 08-03-2007 at 04:43 AM. Reason: Fix loop condition...

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Think you messed up your condition there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does memmove actually do?
    By tytelizgal in forum C Programming
    Replies: 9
    Last Post: 10-17-2008, 11:53 AM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Memcpy(); Errors...
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2006, 10:35 AM
  4. memcpy with 128 bit registers
    By grady in forum Linux Programming
    Replies: 2
    Last Post: 01-19-2004, 05:25 AM
  5. memcpy
    By doubleanti in forum C++ Programming
    Replies: 10
    Last Post: 02-28-2002, 03:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21