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/

posted @ 2014-12-03 11:14  A.Kun  阅读(1047)  评论(0编辑  收藏  举报