Question:
what is the difference between the functions memmove() and memcpy()?
Answer:
Source: CoolInterview.com
MemCpy.
It copies count characters from the array pointed from source to destination. If the array overlaps the behaviour of memcpy is undefined.
Memmove
It copies count characters from the array pointed from source to destination. If the array overlaps the copy will take place correctly.
EX: char a[]="Hello World"; memmove(&a[2],"She",3); prints "HeShe world" memcpy(&a[2],"llo",3); some times o/p is unexpected. Source: CoolInterview.com
Answered by: Santosh Inamdar | Date: 2/4/2009
| Contact Santosh Inamdar
both are same
memmove() and memcpy() copies specified number of characters from one buffer to another Source: CoolInterview.com
Answered by: Nikhil | Date: 9/10/2009
| Contact Nikhil
Its similar to typing without insert key ON and OFF str1="strings are good"); memmove(str1+8,str1+11,4); returns------ strings are are good memcpy(str+8,str1+11,4) returns------ strings are read
memcpy - just copies form source to destination. memmove - copies from source to destination if buffers overlap ,every character is read before another character is written to the same location..
above example will give same output in both the cases as source and destination areas doesn't overlap also `are' will not be appended as claimed in above example.
instead use below example:
str = "0123456789" memmove(str+2, str+1, 3); returns: 0112356789
memcpy(str+2, str+1, 3); returns: 0111356789
As we could see in second example of memcpy `1' is copied twice, from memory-location: 1 and then from memory-location:2 which was updated to `1' by memcpy, where as memmove uses a temporary buffer hence this problem didn't occur. Source: CoolInterview.com
Answered by: clara phillips | Date: 6/24/2010
| Contact clara phillips
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|