Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

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.

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 <sal.h>
using namespace vc_attributes;
void f([Pre(Null=Yes)] char* pc)
{
  *pc='\0'; // warning C6011 - pc is null
  // code ...
}

The use of malloc and free have many pitfalls in terms of memory leaks and exceptions. To avoid these kinds of leaks and exception problems altogether, use the mechanisms that are provided by the C++ Standard Template Library (STL). These include shared_ptr, unique_ptr, and vector. For more information, see Smart Pointers (Modern C++) and Standard C++ Library Reference.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.