Today's Question:  What does your personal desk look like?        GIVE A SHOUT

memcpy() vs memmove() in C

  Pi Ke        2011-04-14 09:05:10       11,022        0    

memcpy() copies the bytes of data between memory blocks. If the block of memory overlaps, the function might not work properly. Use memmove() to deal with overlapping memory blocks. 

memmove() is very much like memcpy() but very flexible as it handles overlapping of memory blocks. 

example : 
char msg[50] = "abcdefghijklmnopqrstuvwxyz"; 
char temp[50]; 
main() 

strcpy(temp, msg); 
printf("Original Msg = %s\n",temp); 

memcpy(temp+4, temp+16, 10); 
printf("After memcpy without overlap = %s\n",temp); 
strcpy(temp , msg); 
memcpy(temp+6, temp+4, 10); 
printf("After memcpy with overlap = %s\n",temp); 

strcpy(temp, msg); 
printf("Original Msg = %s\n",temp); 

memmove(temp+4, temp+16, 10); 
printf("After memmove without overlap = %s\n",temp); 
strcpy(temp , msg); 
memmove(temp+6, temp+4, 10); 
printf("After memmove with overlap = %s\n",temp); 


Original Msg = abcdefghijklmnopqrstuvwxyz 
After memcpy without overlap = abcdqrstuvwxyzopqrstuvwxyz 
After memcpy with overlap = abcdefefefefefefqrstuvwxyz 

Original Msg = abcdefghijklmnopqrstuvwxyz 
After memmove without overlap = abcdqrstuvwxyzopqrstuvwxyz 
After memmove with overlap = abcdefefghijklmnqrstuvwxyz

Read more: http://wiki.answers.com/Q/Difference_between_memcpy_and_memmove_in_C_with_example#ixzz1JVBbVfSG

COMPARISON  MEMORY  MEMCPY  MEMMOVE  C  DIFFE 

Share on Facebook  Share on Twitter  Share on Weibo  Share on Reddit 

  RELATED


  0 COMMENT


No comment for this article.