指向调用方SAL的指针的输出错误

Output of pointer to caller SAL error

本文关键字:指针 错误 输出 SAL 调用      更新时间:2023-10-16

我正在尝试将SAL添加到我的代码中。。。我按照msdn工作,在msdn的例子中发现了错误,不知道如何处理。

litle将示例"指针到调用者的输出(示例:Outputr Annotation)"从Understanding SAL 改为Understanding

Outputtr用于注释一个参数,该参数旨在返回指针。参数本身不应为NULL,并且调用函数在其中返回一个非NULL指针,该指针指向初始化数据。

我的代码:

#include "stdafx.h"
#include "assert.h"
void GoodOutPtrCallee(_Outptr_ int **pInt)
{
    int *pInt2 = new int;
    if (*pInt != NULL)
    {
        *pInt2 = 1;
    }
    else
    {
        *pInt2 = 2;
    }
    *pInt = pInt2;
}
int _tmain(int argc, _TCHAR* argv[])
{
    int* nullValue = NULL;
    GoodOutPtrCallee(&nullValue); 
    assert(*nullValue == 2);
    int someValue = 22;
    int* someValuePtr = &someValue;
    GoodOutPtrCallee(&someValuePtr); 
    assert(*someValuePtr == 1);
    return 0;
}

如果我在启用代码alalysys的VS2013中编译它,我得到了C6001:使用未初始化的内存

对于

if (*pInt != NULL)

行。

我的注释中出现了什么问题?我该如何修复它?

由于您正在读取通过指针参数pInt传递的值,因此不能使用_Outptr_,因为这描述了一个仅用作输出而不用作输入的参数。请改用_Inout_

您可能需要重新考虑使用SAL。它的文档非常差,因此我不能肯定地说_Inout_实际上是这里使用的最佳注释。我所确信的是,根据微软模糊的描述,这是我能找到的最好的匹配,它消除了警告。当然不会使用注释。

EDIT:我被类似的变量名pIntpInt2弄糊涂了。您可能应该将pInt标记为输入和输出,而不仅仅是输出,因为您正在读取它的值以检查它是否为NULL