在Visual Studio 2005 C++编译器上,当我的代码使用fopen和此类调用时,我收到以下警告.
1>foo.cpp(5) : warning C4996: 'fopen' was declared deprecated
1> c:\program files\microsoft visual studio 8\vc\include\stdio.h(234) : see declaration of 'fopen'
1> Message: 'This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
Run Code Online (Sandbox Code Playgroud)
我该如何防止这种情况?
我有一个简单的C++与Boost像这样:
#include <boost/algorithm/string.hpp>
int main()
{
std::string latlonStr = "hello,ergr()()rg(rg)";
boost::find_format_all(latlonStr,boost::token_finder(boost::is_any_of("(,)")),boost::const_formatter(" "));
Run Code Online (Sandbox Code Playgroud)
这很好用; 它取代了每次出现的(),用""
但是,我在编译时收到此警告:
我正在使用MSVC 2008,Boost 1.37.0.
1>Compiling...
1>mainTest.cpp
1>c:\work\minescout-feat-000\extlib\boost\algorithm\string\detail\classification.hpp(102) : warning C4996: 'std::copy': 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 (x86)\microsoft visual studio 9.0\vc\include\xutility(2576) : see declaration of 'std::copy'
1> c:\work\minescout-feat-000\extlib\boost\algorithm\string\classification.hpp(206) : see …
Run Code Online (Sandbox Code Playgroud) 得到了一些不是我的代码并且它产生了这个警告atm:
iehtmlwin.cpp(264) : warning C4996: 'std::basic_string<_Elem,_Traits,_Ax>::copy': 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'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
c:\program files (x86)\microsoft visual studio 8\vc\include\xstring(1680) : see declaration of 'std::basic_string<_Elem,_Traits,_Ax>::copy'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]
Run Code Online (Sandbox Code Playgroud)
这是有问题的代码:
HRESULT STDMETHODCALLTYPE Read(void __RPC_FAR *pv, ULONG cb, ULONG __RPC_FAR *pcbRead)
{ …
Run Code Online (Sandbox Code Playgroud) 我知道这个问题在SO中被多次询问过,但这与其他问题有所不同.
xutility(2227):警告C4996:'std :: _ Copy_impl'
失败的代码片段
DWORD dwNumberOfNames = pExportDirectory->NumberOfNames;
LPDWORD dwNames = (LPDWORD)((LPBYTE)hDLL + pExportDirectory->AddressOfNames);
std::vector< std::string > exports;
std::copy(
dwNames,
dwNames + dwNumberOfNames,
[&exports, &hDLL](DWORD dwFuncOffset)
{
std::string fname = std::string((PCHAR)((PBYTE)hDLL + dwFuncOffset));
exports.push_back(fname);
}
);
Run Code Online (Sandbox Code Playgroud)
编译错误
错误1错误C4996:'std :: _ Copy_impl':带有可能不安全的参数的函数调用 - 此调用依赖于调用者来检查传递的值是否正确.要禁用此警告,请使用-D_SCL_SECURE_NO_WARNINGS.请参阅有关如何使用Visual C++'Checked Iterators'的文档:C:\ Program Files(x86)\ Microsoft Visual Studio 11.0\VC\include\xutility 2176
题
考虑到,C4996,意味着该功能已标记为已弃用问题在哪里?
std::copy
用C Array
?注意
我知道,如何压制警告,但我很想知道,问题的根本原因是什么?
此外,同样重要的是,我知道,在不影响代码质量的情况下处理此问题的便携方式.
我在VS2010中编写代码,我碰巧在编译后看到编译器为strcpy和sprintf调用提供了C4996警告("此函数或变量可能不安全").
但是,我无法获得memcpy的类似警告(可能在代码中有更多类似的'不安全'函数调用)
int _tmain(int argc, _TCHAR* argv[])
{
char buf1[100], buf2[100];
strcpy (buf1, buf2); // Warning C4996 displayed here asking to use strcpy_s instead
memcpy (buf1, buf2, 100); // No warning here asking to use memcpy_s
memcpy_s(buf1, 100, buf2, 100);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么会这样?如何在代码中为所有可能的不安全呼叫打开C4996警告?
我有一个功能;
void foo(const char* format, ...)
{
char buffer[1080];
// Supposed way to handle C Variable Arguments?
va_list argptr;
va_start(argptr, format);
sprintf(buffer, format, argptr);
va_end(argptr);
printf_s("%s.\n", buffer);
}
int main()
{
int val = 53;
foo("%d", val);
}
Run Code Online (Sandbox Code Playgroud)
每次运行时,我都会得到每次运行时都会发生变化的大量数字.12253360
, 5306452
等我不明白为什么.
这是我的sprintf
电话,还是我正在做的事情va_list argptr;
?我buffer
太大了吗?
谢谢.