指向调用方SAL的指针的输出错误
Output of pointer to caller SAL error
我正在尝试将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:我被类似的变量名pInt
和pInt2
弄糊涂了。您可能应该将pInt
标记为输入和输出,而不仅仅是输出,因为您正在读取它的值以检查它是否为NULL
相关文章:
- 为什么我在空指针错误(链表)中获取成员访问权限
- std::函数指针错误:无法将 &A::a 转换为类型 std::function<>&&
- 为什么我得到 munmap_chunk():无效的指针:错误
- 成员模板函数指针错误
- 删除指针时,未分配被释放的 C++ 指针错误
- Visual Studio 中的"指向引用的指针"错误不会按发生位置的文件名显示
- C++数组指针错误无法将“int*”转换为“int**”
- C++ 通过函数指针错误调用成员函数
- 从 BST 中删除元素(指针错误)
- 随机生成的多维布尔指针错误
- 在测试中使用unique_ptr时指针错误无效
- 使用 malloc 而不是 new 会导致 free():指针错误无效
- 非标准语法;使用 '&' 创建指向成员的指针错误,将成员函数分配给向量时出错
- 抽象类和唯一指针 c++ 错误
- 返回函数指针错误而不带类型定义
- 练习中出现空指针错误
- 指针错误导致的分段错误(简单..)
- free():C++中的指针错误无效
- 使用布尔值数组"表达式必须具有指向对象类型的指针"错误C++
- 指针错误的C 数组