zoukankan      html  css  js  c++  java
  • 内存拷贝探究

    函数原型:
    errno_t memcpy_s(
       void *dest,
       size_t numberOfElements,
       const void *src,
       size_t count 
    );

    参数:

    dest

    新的缓冲区。

    numberOfElements

    目标缓冲区的大小。

    src

    要复制的缓冲区。

    count

    字符数。的副本。

    测试代码:

    // memcpyTest.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include "string" //strlen()需要的头文件
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	//-----memcpyPchar_Test_Begin-----------------------------
    	// Populate a2 with squares of integers
    	char *psourcechar = "aaa";
    	char *ptargetchar = /*NULL*/new char[6]; //内存拷贝,需要保证target目标有空间,且要保证空间的大小
    
    	// Tell memcpy to copy 10 ints (40 bytes)(the size of a1)
    	memcpy(ptargetchar,psourcechar,sizeof(ptargetchar));    
    	ptargetchar[strlen(psourcechar)]='';
    
    	printf("%s ", ptargetchar);
    
    	printf("
    ");
    	//-----memcpyPchar_Test_End-----------------------------
    
    	
    
    	//-----memcpy_Test_Begin-----------------------------
    	int memcpya1[10], memcpya2[100], i;
    
    	// Populate a2 with squares of integers
    	for (i = 0; i < 100; i++)
    	{
    		memcpya2[i] = i*i;
    	}
    
    	// Tell memcpy to copy 10 ints (40 bytes)(the size of a1)
    	memcpy(memcpya1,memcpya2,sizeof(memcpya1) );    
    	
    	for (i = 0; i < 10; i++)
    		printf("%d ", memcpya1[i]);
    
    	printf("
    ");
    	//-----memcpy_Test_End-----------------------------
    
    
    	//-----memcpy_s_Test_Begin-----------------------------
    	int a1[10], a2[100], j;
    	errno_t err;
    
    	// Populate a2 with squares of integers
    	for (j = 0; j < 100; j++)
    	{
    		a2[j] = j*j;
    	}
    
    	// Tell memcpy_s to copy 10 ints (40 bytes), giving
    	// the size of the a1 array (also 40 bytes).
    	err = memcpy_s(a1, sizeof(a1), a2, 10 * sizeof (int) );    
    	if (err)
    	{
    		printf("Error executing memcpy_s.
    ");
    	}
    	else
    	{
    		for (j = 0; j < 10; j++)
    			printf("%d ", a1[j]);
    	}
    	printf("
    ");
    	//-----memcpy_s_Test_End-----------------------------
    
    	return 0;
    }
    
    


     

  • 相关阅读:
    iOS 基础复习
    iOS项目立项
    Cocos2d-JS项目之四:UI界面的优化
    Cocos2d-JS项目之三:使用合图
    Cocos2d-JS项目之二:studio基础控件的使用
    Cocos2d-JS项目之一:环境(IDE 运行js-tests、IDE 和 studio 统一工程)
    字节对齐导致的iOS EXC_ARM_DA_ALIGN崩溃
    cocos2dx 2.x 骨骼动画优化
    Cocos2dx 把 glview 渲染到 Qt 控件上(Mac 环境)
    [leetcode 周赛 160] 1240 铺瓷砖
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3260635.html
Copyright © 2011-2022 走看看