"AfxIsValidAddress" 函数的等效标准函数是什么?
What is the equivalent standard function of the "AfxIsValidAddress" function?
我使用的是 MFC 项目,该项目应在独立于平台的环境中进行保护,使用标准函数而不是 MFC/AFX。
例如:将使用 std::string 代替 CString,而不是 CMutex std::mutex。
什么是与平台无关的 C++11 std::-MFC 函数的等价物 "AfxIsValidAddress"?
标准库中没有类似于 AfxIsValidAddress()
的东西,看起来该函数实际上并没有做那么多验证。
请参阅 AfxIsValidAddress (and Others) Don’t Work as Advertised,其中表示该函数最终只是针对 NULL 进行检查。关于有效地址检查函数系列,它也有这样的说法:
There are several Win32 API similar in functionality: IsBadWritePtr
,
IsBadHugeWritePtr
, IsBadReadPtr
, IsBadHugeReadPtr
, IsBadCodePtr
,
IsBadStringPtr
. It has been known since at least 2004 that these
functions are broken beyond repair and should never be used. The
almighty Raymond Chen and Larry Osterman both discuss the reasons in
detail, so just a short rehash: IsBad*Ptr all work by accessing the
tested address and catching any thrown exceptions. Problem is that a
certain few of these access violations (namely, those on stack guard
pages) should never be caught – the OS uses them to properly enlarge
thread stacks.
我认为最好只遵循标准 C++ 程序来检查指针不是 nullptr
或者更好的是尽可能限制指针的使用。
我使用的是 MFC 项目,该项目应在独立于平台的环境中进行保护,使用标准函数而不是 MFC/AFX。
例如:将使用 std::string 代替 CString,而不是 CMutex std::mutex。
什么是与平台无关的 C++11 std::-MFC 函数的等价物 "AfxIsValidAddress"?
标准库中没有类似于 AfxIsValidAddress()
的东西,看起来该函数实际上并没有做那么多验证。
请参阅 AfxIsValidAddress (and Others) Don’t Work as Advertised,其中表示该函数最终只是针对 NULL 进行检查。关于有效地址检查函数系列,它也有这样的说法:
There are several Win32 API similar in functionality:
IsBadWritePtr
,IsBadHugeWritePtr
,IsBadReadPtr
,IsBadHugeReadPtr
,IsBadCodePtr
,IsBadStringPtr
. It has been known since at least 2004 that these functions are broken beyond repair and should never be used. The almighty Raymond Chen and Larry Osterman both discuss the reasons in detail, so just a short rehash: IsBad*Ptr all work by accessing the tested address and catching any thrown exceptions. Problem is that a certain few of these access violations (namely, those on stack guard pages) should never be caught – the OS uses them to properly enlarge thread stacks.
我认为最好只遵循标准 C++ 程序来检查指针不是 nullptr
或者更好的是尽可能限制指针的使用。