[FROM MSDN && 百科]
原型: void *memmove( void* dest, const void* src, size_t count );
#include<string.h>
由src所指内存区域复制count个字节到dest所指内存区域。
src和dest所指内存区域可以重叠,但复制后dest内容会被更改。函数返回指向dest的指针。
Copies the values of num bytes from the location pointed by source to the memory block pointed by destination. Copying takes place as if an intermediate buffer were used, allowing the destination and source to overlap.
memmove的处理措施:
(1)当源内存的首地址等于目标内存的首地址时,不进行任何拷贝
(2)当源内存的首地址大于目标内存的首地址时,实行正向拷贝
(3)当源内存的首地址小于目标内存的首地址时,实行反向拷贝
DEMO:
- void *mymemmove(void *dest,const void *src,size_t count)
- {
- char *d=(char *)dest;
- char *s=(char *)src;
- assert( NULL !=src && NULL !=dest);
- if (d <= s || d >= s + count)
- {
- while(count--)
- {
- *d++ = *s++;
- }
- }
- else
- {
- d += count-1;
- s += count-1;
- while(count--)
- {
- *d-- = *s--;
- }
- }
- return dest;
- }