zoukankan      html  css  js  c++  java
  • error C4996: 'std::_Copy_impl'

    以下代码段在VS2008编译可以通过,只是会提示不安全;

    std::vector<unsigned char> fileData ="asdfsfsfsfsdf";//随便打的
    //文件数据大小
    int size = fileData.size();
    //字节数组
    char* data = new char[size + 1];
    //把二进制数据复制到数组
    std::copy(fileData.begin(), fileData.end(), data);
    data[size] = 0;

    但在VS2012就不能编译通过,VS2012进行了强制类型检查

    1>C:Program FilesMicrosoft Visual Studio 11.0VCincludexutility(2176): error C4996: 'std::_Copy_impl': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
    1>          C:Program FilesMicrosoft Visual Studio 11.0VCincludexutility(2157) : 参见“std::_Copy_impl”的声明
    1>          HashParser.cpp(43): 参见对正在编译的函数 模板 实例化“_OutIt std::copy<std::_Vector_iterator<_Myvec>,char*>(_InIt,_InIt,_OutIt)”的引用
    1>          with
    1>          [
    1>              _OutIt=char *,
    1>              _Myvec=std::_Vector_val<std::_Simple_types<unsigned char>>,
    1>              _InIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<unsigned char>>>
    1>          ]

    解决:修改代码如下

    std::vector<unsigned char> fileData = "sdfasdfasdfasf";
    	//文件数据大小
    	int size = fileData.size();
    	//字节数组
    	char* data = new char[size + 1];
    	//把二进制数据复制到数组
    	std::copy(fileData.begin(), fileData.end(), stdext::unchecked_array_iterator<char*>(data));
    	data[size] = 0;

    参考:

    http://choorucode.com/2010/08/30/visual-c-c4996-warning-on-copy-with-array-parameters/

  • 相关阅读:
    python高级之操作数据库
    算法之矩阵计算斐波那契数列
    Mysql操作初级
    python高级之生成器&迭代器
    python高级之装饰器
    python高级之多进程
    python高级之多线程
    操作系统IO模型
    python高级之网络编程
    python高级之面向对象高级
  • 原文地址:https://www.cnblogs.com/imzhstar/p/4139497.html
Copyright © 2011-2022 走看看