Results 1 to 5 of 5
Hi ,
I gone through man page of memcpy and memmove , from there i got one difference as :-
memcpy :- The memory areas should not overlap.
Use mem‐ ...
- 09-01-2009 #1Just Joined!
- Join Date
- Dec 2007
- Location
- Bangalore >> India
- Posts
- 28
What is the difference between memcpy and memmove ?
Hi ,
I gone through man page of memcpy and memmove , from there i got one difference as :-
memcpy :- The memory areas should not overlap.
Use mem‐ move(3) if the memory areas do overlap.
memmove:-The memory areas may overlap
But, it's not happening in practical , for this , please have a look the given example ,
Waiting solution with good examples.
Thanks,
-Amaresh
- 09-01-2009 #2Just Joined!
- Join Date
- Dec 2007
- Location
- Bangalore >> India
- Posts
- 28
Code is :--
amaresh@eOdissa:~/Documents/Test$ echo ;echo ;echo "----------------memcpy.c------------------";cat memcpy.c ; ./memcpy ;echo "-------------memmove.c------------------";cat memmove.c ;echo "Result:-"; ./memmove
----------------memcpy.c------------------
#include<stdio.h>
#include<string.h>
int
main()
{
char str1[]="123456789";
char str2[]="abcdefghi";
memcpy(str1+1,str2+1,4);
printf("memcpy= %s\n",str1);
return 0;
}
memcpy= 1bcde6789
-------------memmove.c------------------
#include<stdio.h>
#include<string.h>
int
main()
{
char str1[]="123456789";
char str2[]="abcdefghi";
memcpy(str1+1,str2+1,4);
printf("Result String: = %s\n",str1);
return 0;
}
Result:-
Result String: = 1bcde6789
- 09-01-2009 #3Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
The variables str1 and str2 are not overlapping memory segments. Hence, memcpy and memmove appear to work the same. If you were to do this:
memmove(str1+1, str1+0, 4)
vs.
memcpy(str1+1, str1+0, 4)
Then you would have a problem with memcpy since it would be writing over itself.Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 09-03-2009 #4Just Joined!
- Join Date
- Dec 2007
- Location
- Bangalore >> India
- Posts
- 28
- 09-03-2009 #5
This isn't what Rubberman was talking about. Overlapping means you copy characters from a place that gets overwritten in the progress of copying.
Like you have "1234" and want to copy the "34" part to the position of "23".
In that case the position of "3" is overlapping.
You used memcpy in both case, by the way.Debian GNU/Linux -- You know you want it.


Reply With Quote
