Partager via


C6011

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

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 <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 and C++ Standard Library.

See Also

Annotation Overview
Null
NULL
Indirection and Address-of Operators
malloc
free