zoukankan      html  css  js  c++  java
  • 对memcpy和memmove的疑惑

    微软编译器中memcpy的文档为:

    Copies bytes between buffers. More secure versions of these functions are available;

    void *memcpy( void *dest, const void *src, size_t count );

    Remarks:

    memcpy copies count bytes from src to dest; wmemcpy copies count wide characters (two bytes). If the source and destination overlap, the behavior of memcpy is undefined. Use memmove to handle overlapping regions.

    也就是说,memcpy的内存拷贝,要求dest和src的内存之间不能有重叠部分。例如

    [A][B][C]

    A、B、C表示三段连续的等长,且长度为L的内存空间,利用memcpy函数将地址为A,长度为2L的内存数据,复制到地址为B的空间是不可行的。MSDN中给出应该用memmove()代替。

    对以上内容进行了测试,结果有些让人疑惑。

    测试代码如下:

    #include <iostream>
    #include <string.h>
    using namespace std;
    
    
    int main(int argc,char ** argv)
    {
    	//s = aaaabbbcccc
    	char s[256] = {'a','a','a','a','b','b','b','c','c','c','c'};
    	
    	//memcpy(s,s+2,6);
    	memmove(s,s+2,6);
    
    	cout<<s<<endl;
    
    	system("pause");
    	return 0;
    }

    memcpy的dst和src之间有重叠部分,但memcpy和memmove得到的结果是相同的。并且从复制的逻辑上,得到的结果也是正确的。

    疑惑,不知道memcpy和memmove的区别到底在哪里。

  • 相关阅读:
    第一章Python 数据模型
    Numpy 之 where理解
    探析熟悉而又困惑的参数:argc && argv
    python读书笔记
    经典进程的同步问题之——读者写者
    经典进程的同步问题之——哲学家进餐
    冰多多团队博客目录
    团队事后分析
    冰多多团队Gamma阶段发布说明
    gamma测试报告
  • 原文地址:https://www.cnblogs.com/raymon/p/2311394.html
Copyright © 2011-2022 走看看