What is the difference between memmove and memcpy?

前端 未结 9 1708
Happy的楠姐
Happy的楠姐 2020-11-28 02:13

What is the difference between memmove and memcpy? Which one do you usually use and how?

相关标签:
9条回答
  • 2020-11-28 02:34

    With memcpy, the destination cannot overlap the source at all. With memmove it can. This means that memmove might be very slightly slower than memcpy, as it cannot make the same assumptions.

    For example, memcpy might always copy addresses from low to high. If the destination overlaps after the source, this means some addresses will be overwritten before copied. memmove would detect this and copy in the other direction - from high to low - in this case. However, checking this and switching to another (possibly less efficient) algorithm takes time.

    0 讨论(0)
  • 2020-11-28 02:34

    memmove can handle overlapping memory, memcpy can't.

    Consider

    char[] str = "foo-bar";
    memcpy(&str[3],&str[4],4); //might blow up
    

    Obviously the source and destination now overlap, we're overwriting "-bar" with "bar". It's undefined behavior using memcpy if the source and destination overlap so in this case cases we need memmove.

    memmove(&str[3],&str[4],4); //fine
    
    0 讨论(0)
  • 2020-11-28 02:34

    There are two obvious ways to implement mempcpy(void *dest, const void *src, size_t n) (ignoring the return value):

    1. for (char *p=src, *q=dest;  n-->0;  ++p, ++q)
          *q=*p;
      
    2. char *p=src, *q=dest;
      while (n-->0)
          q[n]=p[n];
      

    In the first implementation, the copy proceeds from low to high addresses, and in the second, from high to low. If the range to be copied overlaps (as is the case when scrolling a framebuffer, for example), then only one direction of operation is correct, and the other will overwrite locations that will subsequently be read from.

    A memmove() implementation, at its simplest, will test dest<src (in some platform-dependent way), and execute the appropriate direction of memcpy().

    User code can't do that of course, because even after casting src and dst to some concrete pointer type, they don't (in general) point into the same object and so can't be compared. But the standard library can have enough platform knowledge to perform such a comparison without causing Undefined Behaviour.


    Note that in real life, implementations tend to be significantly more complex, to gain the maximum performance from larger transfers (when alignment permits) and/or good data cache utilisation. The code above is just to make the point as simply as possible.

    0 讨论(0)
  • 2020-11-28 02:36

    memmove can deal with overlapping source and destination regions, while memcpy cannot. Among the two, memcpy is much more efficient. So, better to USE memcpy it if you can.

    Reference: https://www.youtube.com/watch?v=Yr1YnOVG-4g Dr. Jerry Cain, (Stanford Intro Systems Lecture - 7) Time: 36:00

    0 讨论(0)
  • 2020-11-28 02:37

    The main difference between memmove() and memcpy() is that in memmove() a buffer - temporary memory - is used, so there is no risk of overlapping. On the other hand, memcpy() directly copies the data from the location that is pointed by the source to the location pointed by the destination. (http://www.cplusplus.com/reference/cstring/memcpy/)

    Consider the following examples:

    1. #include <stdio.h>
      #include <string.h>
      
      int main (void)
      {
          char string [] = "stackoverflow";
          char *first, *second;
          first = string;
          second = string;
      
          puts(string);
          memcpy(first+5, first, 5);
          puts(first);
          memmove(second+5, second, 5);
          puts(second);
          return 0;
      }
      

      As you expected, this will print out:

      stackoverflow
      stackstacklow
      stackstacklow
      
    2. But in this example, the results will not be the same:

      #include <stdio.h>
      #include <string.h>
      
      int main (void)
      {
          char string [] = "stackoverflow";
          char *third, *fourth;
          third = string;
          fourth = string;
      
          puts(string);
          memcpy(third+5, third, 7);
          puts(third);
          memmove(fourth+5, fourth, 7);
          puts(fourth);
          return 0;
      }
      

      Output:

      stackoverflow
      stackstackovw
      stackstackstw
      

    It is because "memcpy()" does the following:

    1.  stackoverflow
    2.  stacksverflow
    3.  stacksterflow
    4.  stackstarflow
    5.  stackstacflow
    6.  stackstacklow
    7.  stackstacksow
    8.  stackstackstw
    
    0 讨论(0)
  • 2020-11-28 02:38

    simply from the ISO/IEC:9899 standard it is well described.

    7.21.2.1 The memcpy function

    [...]

    2 The memcpy function copies n characters from the object pointed to by s2 into the object pointed to by s1. If copying takes place between objects that overlap, the behavior is undefined.

    And

    7.21.2.2 The memmove function

    [...]

    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.

    Which one I usually use acording to the question, depends on what functionallity I need.

    In plain text memcpy() doesn't allow s1 and s2 to overlap, while memmove() does.

    0 讨论(0)
提交回复
热议问题