C6011

warning C6011: dereferencing NULL pointer <name>

This warning indicates that a null pointer is being dereferenced. If the pointer value is invalid, the result is undefined.

Example

The following code generates this warning because a call to malloc might return null if there is insufficient memory available:

#include <malloc.h>

void f( )
{ 
  char *p = ( char * ) malloc( 10 );
  *p = '\0';
  
  // code ...
 free( p );
}

To correct this warning, examine the pointer for null value as shown in the following code:

#include <malloc.h>
void f( )
{
  char *p = ( char * )malloc ( 10 );
  if ( p ) 
  {
    *p = '\0';
    // code ...
    
    free( p );
  }
}

You must allocate memory inside the function whose parameters are annotated by using the Null property in a Pre condition before dereferencing the parameter. The following code generates warning C6011 because an attempt is made to dereference a null pointer (pc) inside the function without first allocating memory:

#include <codeanalysis\sourceannotations.h>
using namespace vc_attributes;
void f([Pre(Null=Yes)] char* pc)
{
  *pc='\0'; // warning C6011 - pc is null
  // code ...
}

See Also

Reference

Null

NULL (CRT)

malloc

free

Concepts

Annotation Overview

Indirection and Address-of Operators