各位来讨论一下memcpy和memmove有什么区别????

解决方案 »

  1.   

    void * __cdecl memcpy (
            void * dst,
            const void * src,
            size_t count
            )
    {
            void * ret = dst;#if defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC)
            {
            extern void RtlMoveMemory( void *, const void *, size_t count );        RtlMoveMemory( dst, src, count );
            }
    #else  /* defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC) */
            /*
             * copy from lower addresses to higher addresses
             */
            while (count--) {
                    *(char *)dst = *(char *)src;
                    dst = (char *)dst + 1;
                    src = (char *)src + 1;
            }
    #endif  /* defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC) */        return(ret);
    }void * __cdecl memmove (
            void * dst,
            const void * src,
            size_t count
            )
    {
            void * ret = dst;#if defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC)
            {
            extern void RtlMoveMemory( void *, const void *, size_t count );        RtlMoveMemory( dst, src, count );
            }
    #else  /* defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC) */
            if (dst <= src || (char *)dst >= ((char *)src + count)) {
                    /*
                     * Non-Overlapping Buffers
                     * copy from lower addresses to higher addresses
                     */
                    while (count--) {
                            *(char *)dst = *(char *)src;
                            dst = (char *)dst + 1;
                            src = (char *)src + 1;
                    }
            }
            else {
                    /*
                     * Overlapping Buffers
                     * copy from higher addresses to lower addresses
                     */
                    dst = (char *)dst + count - 1;
                    src = (char *)src + count - 1;                while (count--) {
                            *(char *)dst = *(char *)src;
                            dst = (char *)dst - 1;
                            src = (char *)src - 1;
                    }
            }
    #endif  /* defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC) */        return(ret);
    }The memcpy function copies count bytes of src to dest. If the source and destination overlap, this function does not ensure that the original source bytes in the overlapping region are copied before being overwritten. Use memmove to handle overlapping regions.
      

  2.   

    The memmove function copies count bytes of characters from src to dest. If some regions of the source area and the destination overlap, memmove ensures that the original source bytes in the overlapping region are copied before being overwritten.
    The memcpy function copies count bytes of src to dest. If the source and destination overlap, this function does not ensure that the original source bytes in the overlapping region are copied before being overwritten. Use memmove to handle overlapping regions.
      

  3.   

    其实memcopy 和memmove都可以将内存中的某块中的信息复制到另一区域。当然,也有小的区别。
    如果源地址和目标地址有重叠的地方,memcpy并不提供相应的保护机制,而memmove会提供比较全面的保护工作。
    另外,memcpy的运行速度要比memmove快。
      

  4.   

    函数:void * memcpy (void *to, const void *from, size_t size) 
    这个函数会从数组from的开头处,复制size个字符,并从头写入数组to中。这个函数的参数from和to如果是有重迭的两个数组,那么就会出错。这种情况,请使用memmove函数来代替它。
        Memcpy函数返回to的地址;
        以下是一个使用memcpy复制一个数组中内容的例子:
        struct foo *oldarray, *newarray;
        int arraysize;
        ...
        memcpy (new, old, arraysize * sizeof (struct foo));    函数:void * memmove (void *to, const void *from, size_t size) 
        从from中拷贝size字节到to中,即使这两个内存块有重迭也不影响执行效果。