C6011
更新:2007 年 11 月
警告 C6011:正在取消 NULL 指针 <name> 的引用
此警告意味着正在取消引用 null 指针。如果该指针的值无效,则结果是未定义的。
示例
在下面的代码中,由于对 malloc 的调用可能因没有足够的可用内存而返回 null,因此会生成此警告:
#include <malloc.h>
void f( )
{
char *p = ( char * ) malloc( 10 );
*p = '\0';
// code ...
free( p );
}
若要更正此警告,请检查指针的值是否为 null,如下面的代码所示:
#include <malloc.h>
void f( )
{
char *p = ( char * )malloc ( 10 );
if ( p )
{
*p = '\0';
// code ...
free( p );
}
}
在取消引用参数前,必须在通过在前置条件中使用 Null 属性来批注参数的函数内分配内存。在下面的代码中,由于在尝试在该函数内取消引用 null 指针 (pc) 之前没有分配内存,因此会生成警告 C6011:
#include <codeanalysis\sourceannotations.h>
using namespace vc_attributes;
void f([Pre(Null=Yes)] char* pc)
{
*pc='\0'; // warning C6011 - pc is null
// code ...
}
请参见
概念
Indirection and Address-of Operators