Today's Question:  What are you most afraid of as a programmer?        GIVE A SHOUT

Technical Article => Programming =>  C

memcpy() vs memmove() in C

  Pi Ke      2011-04-14 09:05:10      6,685    0    1

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

  SAVE AS PDF   MARK AS READ   MARK AS IMPORTANT

Share on Facebook  Share on Twitter  Share on Google+  Share on Weibo  Share on Reddit  Share on Digg  Share on Tumblr    Delicious

  RELATED


  0 COMMENT


No comment for this article.


  WRITE ARTICLE

Why do Java programmers wear glasses?

By sonic0002
Why do Java programmers wear glasses? Because they don't C#(Because they don't see sharp)