[英]When to use _Notnull_ in C++?
有人可以提供何時使用_Notnull_
的見解嗎? 我正在使用 Visual Studio 2019,這是我的代碼:
#include <windows.h>
#include <vector>
void WriteIt(_Notnull_ CONST WCHAR* sMsg, _Notnull_ CONST WCHAR* sFileName)
{
FILE* stream;
errno_t err = _wfopen_s(&stream, sFileName, L"a+");
if (err == 0)
if (stream)
{
fwprintf_s(stream, L"%s", sMsg);
fclose(stream);
}
}
int main()
{
WCHAR *sMessage = new WCHAR[16]();
WCHAR *sFile = NULL;
sFile = NULL;
WriteIt(sMessage, sFile);
}
然而, _Notnull_
讓 function 運行。 我找不到任何關於如何/為什么將其與示例一起使用的文檔。
謝謝@François Andrieux。 看起來它是在 Visual Studio 中進行代碼分析時使用的,分析器可以查明傳入指針是否為 NULL。以下是代碼分析器所說的包含_Notnull_
的內容: Warning C6387 'sFile' could be '0': this does not adhere to the specification for the function 'WriteIt'.
非常有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.