KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I am trying to understand the difference between memcpy() and memmove() . I have read the documentation, that memcpy() doesn't take care of the overlapping source and destination, whereas memmove() does. However, when I execute these two functions on overlapping memory blocks, they both give the same result. For instance, take the following MSDN example on the memmove() help page:- Is there a better example to understand the drawbacks of memcpy and how memmove solves it? // Illustrate overlapping copy: memmove always handles it correctly; // memcpy may handle it correctly. #include <memory.h> #include <string.h> #include <stdio.h> char str1[7] = "aabbcc"; int main( void ) { printf( "The string: %s\n", str1 ); memcpy( str1 + 2, str1, 4 ); printf( "New string: %s\n", str1 ); strcpy_s( str1, sizeof(str1), "aabbcc" ); // reset string printf( "The string: %s\n", str1 ); memmove( str1 + 2, str1, 4 ); printf( "New string: %s\n", str1 ); } Output: memcpy(): The string: aabbcc New string: aaaabb memmove(): The string: aabbcc New string: aaaabb
Tags (comma-separated)
Save Edits
Cancel