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 Files\Microsoft Visual Studio 11.0\VC\include\xutility(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 Files\Microsoft Visual Studio 11.0\VC\include\xutility(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/

转载于:https://www.cnblogs.com/imzhstar/p/4139497.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值