-2

I read the MSDN article on memmove here:

http://msdn.microsoft.com/en-us/library/aa246469%28v=vs.60%29.aspx

and I cannot tell from their example how memmove differ from memcpy. they both give the same outcome though the example is to show the difference. please help.

CaTx
  • 1,421
  • 4
  • 21
  • 42
  • They only differ when the source and destination overlap. – Barmar Jan 02 '15 at 18:10
  • yes Barmar, but in the overlapping examples, I see no difference. – CaTx Jan 02 '15 at 18:14
  • oh thanks for the negative points guys. the suggested link simply does not answer my question. I am asking about the article on MSDN, not about the functions themselves. – CaTx Jan 02 '15 at 18:20
  • 2
    The results quoted in the article are different: `Function: memmove with overlap … Result: The quick quick brown fox jumps over the lazy dog` versus `Function: memcpy with overlap … Result: The quick quick brown dog jumps over the lazy fox`. Also, using `memcpy()` on overlapping data leads to undefined behaviour — that includes the possibility of 'working as expected'. – Jonathan Leffler Jan 02 '15 at 18:33
  • JonathanLeffler, thank you so much. I came to the same conclusion minutes ago. that was a poorly written article by MSDN. – CaTx Jan 02 '15 at 18:35
  • how do I accept a comment that is the answer? =/ – CaTx Jan 15 '15 at 22:37
  • Hmmm...I see the disclaimer that the answer is 'an extended comment, not an answer', but it does provide an accurate assessment of the problem. So does the duplicate, and the proposed duplicate for the duplicate answer -- [What is the difference between memmove and memcpy?](http://stackoverflow.com/questions/1201319/what-is-the-difference-between-memmove-and-memcpy) – Jonathan Leffler Jan 15 '15 at 22:52

1 Answers1

1

This is an extended comment not an answer. The MSDN example is a poor one, confused by two similar source strings "The quick brown fox jumps over the lazy dog" and "The quick brown dog jumps over the lazy fox". My MS Visual C gives the correct result with memcpy() when the source and destination do overlap, but as @PaulRoub wrote (now deleted), just because one compiler codes it correctly does not mean that another will.

#include <stdio.h>
#include <string.h>

int main()
{
    char str [] = "abcdefghijklmnopqrstuvwxyz";
    printf ("%s\n", str);    

    memcpy (str, str+1, 25);    // copy down
    printf ("%s\n", str);    

    memcpy (str+1, str, 25);    // copy up
    printf ("%s\n", str);    

    return 0;
}

Program output

abcdefghijklmnopqrstuvwxyz
bcdefghijklmnopqrstuvwxyzz
bbcdefghijklmnopqrstuvwxyz
Weather Vane
  • 33,872
  • 7
  • 36
  • 56