memcpy和memmove

来源:互联网 发布:淘宝标签怎么设置 编辑:程序博客网 时间:2024/04/30 19:38

下面是一个memcpy的结构体的复制,有点类似于C++中调用类的拷贝构造函数,用一个对象去初始化另一个对象!

例:

#include "stdio.h"
#include "string.h"
void main(void)
{
struct student
{
    char *name;
    int age;
}stu1={"wangqiao",24};
struct student stu2;
memcpy(&stu2,&stu1,sizeof(stu1));
printf("stu2.name = %s/nstu2.age = %d/n",stu2.name,stu2.age);
getch();
}

 

 

 

;===========================================

memmove

原型:extern void *memmove(void *dest, const void *src, unsigned int count);          用法:#include <string.h>    功能:由src所指内存区域复制count个字节到dest所指内存区域。    说明:src和dest所指内存区域可以重叠,但复制后src内容会被更改。函数返回指向dest的指针。    举例:      // memmove.c            #include <syslib.h>      #include <string.h>      main()      {        char *s="Golden Global View";                clrscr();                memmove(s,s+7,strlen(s)-7);        s[strlen(s)-7]=0;        printf("%s",s);        getchar();        return 0;      }        相关函数:memccpy,memcpy,strcpy

 

 

;===============================================================================

区别:

从DESCRIPTION看来,两者的功能基本相同,唯一不同的是,当 dest 和 src 有重叠的时候选用不同的函数可能会造成不同的结果。不妨写个小程序来测一下:
0 #i nclude <string.h>
1 #i nclude <stdio.h>
2
3 int main()
4 {
5    int i = 0;
6    int a[10];
7  
8    for(i; i < 10; i++)
9    {
10        a[i] = i;
11   }
12
13   memcpy(&a[4], a, sizeof(int)*6);
14
15   for(i = 0; i < 10; i++)
16   {
17       printf("%d ",a[i]);
18   }
20  
21    printf("/n");
22    return 0;
23 }
很简单的小程序!不过已经足以达到我的目的了:)将上面代码gcc之后再运行,结果为:0 1 2 3 0 1 2 3 0 1 。
再把第13行改成:memmove(&a[4], a, sizeof(int)*6),重新gcc再运行,结果为:0 1 2 3 0 1 2 3 4 5 !
呵呵,两者的区别出现了。不过其实这样还不够,继续修改13行: memmove(a, &a[4], sizeof(int)*6) //也就是将源、目的置换一下而已
重新gcc编译再运行,结果为:4 5 6 7 8 9 6 7 8 9 。
还不够,继续修改13行为: memcpy(a, &a[4], sizeof(int)*6); gcc并运行,结果仍为: 4 5 6 7 8 9 6 7 8 9 !
至此真相已经大白了。对比上面四个结果,不难得出以下结论:
1. 当 src 和 dest 所指内存区有重叠时,memmove 相对 memcpy 能提供保证:保证能将 src 所指内存区的前 n 个字节正确的拷贝到 dest 所指内存中;
2. 当 src 地址比 dest 地址低时,两者结果一样。换句话说,memmove 与 memcpy 的区别仅仅体现在 dest 的头部和 src 的尾部有重叠的情况下;
memcpy

 

代码:
;***
;memcpy.asm - contains memcpy and memmove routines
;
;       Copyright (c) 1986-1997, Microsoft Corporation. All right reserved.
;
;Purpose:
;       memcpy() copies a source memory buffer to a destination buffer.
;       Overlapping buffers are not treated specially, so propogation may occur.
;       memmove() copies a source memory buffer to a destination buffer.
;       Overlapping buffers are treated specially, to avoid propogation.
;
;*******************************************************************************
;***
;memcpy - Copy source buffer to destination buffer
;
;Purpose:
;       memcpy() copies a source memory buffer to a destination memory buffer.
;       This routine does NOT recognize overlapping buffers, and thus can lead
;       to propogation.
;       For cases where propogation must be avoided, memmove() must be used.
;
;       Algorithm:

       void* memcpy(void* dest, void* source, size_t count)

      {

           void* ret = dest;

          //copy from lower address to higher address

          while (count--)

                  *dest++ = *source;

 

           return ret;

      }

 

 

memmove

memmove - Copy source buffer to destination buffer
;
;Purpose:
;       memmove() copies a source memory buffer to a destination memory buffer.
;       This routine recognize overlapping buffers to avoid propogation.
;       For cases where propogation is not a problem, memcpy() can be used.
;
;   Algorithm:

    void* memmove(void* dest, void* source, size_t count)

   {

       void* ret = dest;

 

      if (dest <= source || dest >= (source + count))

       {

          //Non-Overlapping Buffers
         //copy from lower addresses to higher addresses
    

         while (count --)

               *dest++ = *source++;

     }

     else

     {

        //Overlapping Buffers
       //copy from higher addresses to lower addresses

 

       dest += count - 1;

       source += count - 1;

       while (count--)

                *dest-- = *source--;l

     }

      return ret;

   }

 

另一种实现:

void* mymemcpy( void* dest, const void* src, size_t count )
{
     char* d = (char*)dest;
     const char* s = (const char*)src;
   //   int n = (count + 7) / 8; // count > 0 assumed
     int n = count >> 3;
     switch( count & 7 )
     {
               do {   *d++ = *s++;
     case 7:         *d++ = *s++;
     case 6:         *d++ = *s++;
     case 5:         *d++ = *s++;
     case 4:         *d++ = *s++;
     case 3:         *d++ = *s++;
     case 2:         *d++ = *s++;
     case 1:         *d++ = *s++;
     case 0           } //while (--n > 0);
                  while (n-- > 0)
     }

     return dest;
}