Click to Rate and Give Feedback
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Visual Studio Team System
C6308

warning C6308: 'realloc' may return null pointer: assigning a null pointer to <variable>, which is passed as an argument to 'realloc', will cause the original memory block to be leaked

This warning indicates a memory leak that is the result of the incorrect use of a reallocation function. Heap reallocation functions do not free the passed buffer if reallocation is unsuccessful. To correct the defect, assign the result of the reallocation function to a temporary, and then replace the original pointer after successful reallocation.

The following sample code generates this warning:

#include <malloc.h>
#include <windows.h>

void f( )
{
  char *x;
  x = (char *) malloc(10);
  if (x != NULL)
  {
    x = (char *) realloc(x, 512);
    // code...
    free(x);
  }   
}

To correct this warning, use the following code:

#include <malloc.h>
#include <windows.h>

void f()
{
  char *x, *tmp;

  x = (char *) malloc(10);

  if (x != NULL)
  {
    tmp = (char *) realloc(x,512);
    if (tmp != NULL) 
    {
      x = tmp;
    }
    free(x);
  }
}

This warning might generate noise if there is a live alias to the buffer-to-be-reallocated at the time of the assignment of the result of the reallocation function.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker