vs2013 error:C4996
来源:互联网 发布:手机网络制式有几种 编辑:程序博客网 时间:2024/05/17 23:53
在学习如何编写移动构造函数的时候,照着文中的代码敲,用vs013编译,出现 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'
解决办法如下:
方法一:
在出现该错误的文件中添加:#pragma warning(disable : 4996) 不理它
注意:1)添加位置:在包含头文件语句前添加该语句;
2)#pragma warning(disable:4996)只对当前文件(包括包含了当前文件的文件)起作用,并非对整个工程 。
方法二:
在包含头文件语句前添加:#define _SCL_SECURE_NO_WARNINGS
方法三:
加入预处理器(项目属性--C/C++--预处理--预处理定义)
_SCL_SECURE_NO_WARNINGS
方法四:
修改代码:
// 拷贝构造函数MemoryBlock(const MemoryBlock& other):_length(other._length), _data(new int[other._length]){std::copy(other._data, other._data + _length, stdext::checked_array_iterator<int*>(_data,_length)/*_data*/);std::cout << "Copy Constructor" << std::endl;}
原因:
不安全的函数,象vector的下标操作算子,如果不恰当的调用,通常会导致不明确的行为。为了保护你的发行版本的应用程序,Visual C++2005便引入了_SECURE_SCL符号,用来给那些非安全的函数添加运行时检查。
如果_SECURE_SCL定义为1,不安全的参数使用会造成error,程序终止。如果定义为0,禁用“经检查的迭代器”(checked iterators:Checked iterators ensure that the bounds of your container are not overwritten.)。默认情况下,_SECURE_SCL在release版是0,在debug版是1。
以上述代码中的std::copy(first, last, destination)为例;其中,first和last是定义拷贝范围的迭代参数,destination是输出迭代参数,指示了目标缓冲区的位置。这里有一个潜在危险是destination所对应的目标缓冲区或容器不足够大,无法容纳所要拷贝的元素。如果Destination是一个需要安全检查的迭代参数,类似的错误将被捕获。但是,这仅仅是一个假设。如果destination是一个简单的指针,将无法保证copy运算函数正确运转。
相对而言,方法四更安全,前三种方法只是忽略了这种潜在危险。
关于checked iterators的扩展阅读,可参考:
[1]Checked Iterators[msdn]
[2] C++/CLI中的Checked Iterators
-------------------------------------------------
参考资料:
[1] error C4996: Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct
[2] _SCL_SECURE_NO_WARNINGS[msdn]
[3] 解决use -D_SCL_SECURE_NO_WARNINGS的问题
[4] vs2013编译 protoBuffer编译出现的问题
0 0
- vs2013 error:C4996
- vs2013出现error C4996问题
- vs2013中的error c4996的问题
- VS2013——error C4996: 'std::_Uninitialized_copy0':
- vs2013+Microsoft Speech SDK 5.1+error C4996
- VS2013 error C4996: 'std::_Copy_impl'解决方法
- VS2013中如何解决error C4996: 'fopen'问题
- vs2013 error c4996: This function or variable may be unsafe
- vs2013之错误 1 error C4996: 'fopen'的解决方案
- VS2013中如何解决error C4996: 'fopen'问题及_CRT_SECURE_NO_WARNINGS
- VS2013中如何解决error C4996: 'fopen'问题
- VS2013中如何解决error C4996: 'fopen'问题
- VS2013中如何解决error C4996: 'fopen'问题
- VS2013中如何解决error C4996: 'fopen'问题
- VS2013中如何解决error C4996: 'fopen'问
- error C4996
- error c4996
- error C4996
- makefile之二:符号、函数说明
- java学习—— 制作简单的计算器
- Excel Sheet Column Number 进制转换
- 腾讯实习生笔试题 软件开发-后台开发方向2014.4.20
- JDBC连接Sql server
- vs2013 error:C4996
- 异常处理
- 归并排序递归与非递归的实现
- C#连接数据库
- json_xml
- C++:STL标准入门汇总(http://www.cnblogs.com/shiyangxt/archive/2008/09/11/1289493.html)
- Video Segmentation by Non-Local Consensus Voting 读书笔记
- 使用Grid++report开发报表功能
- Linux复习(四)Shell编程2