Warning C6011

Dereferencing NULL pointer 'pointer-name'.

Remarks

This warning indicates that your code dereferences a potentially null pointer. If the pointer value is invalid, the result is undefined. To resolve the issue, validate the pointer before use.

Code analysis name: DEREF_NULL_PTR

Example

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

#include <malloc.h>

void f( )
{
  char *p = ( char * ) malloc( 10 );
  *p = '\0';

  // code ...
 free( p );
}

To correct this warning, examine the pointer for a 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 );
  }
}

Functions may have parameters annotated by using the Null property in a Pre condition. Allocate memory inside these functions before you dereference 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 <sal.h>
using namespace vc_attributes;
void f([Pre(Null=Yes)] char* pc)
{
  *pc='\0'; // warning C6011 - pc is null
  // code ...
}

The careless use of malloc and free leads to memory leaks and exceptions. To minimize these kinds of leaks and exception problems altogether, avoid allocating raw memory yourself. Instead, use the mechanisms provided by the C++ Standard Library (STL). These include shared_ptr, unique_ptr, and vector. For more information, see Smart Pointers and C++ Standard Library.

See also