Redaguoti

Bendrinti naudojant


Warning C6001

Using uninitialized memory 'variable'.

Remarks

This warning is reported when an uninitialized local variable is used before it's assigned a value. This usage could lead to unpredictable results. You should always initialize variables before use.

Code analysis name: USING_UNINIT_VAR

Example

The following code generates this warning because variable i is only initialized if b is true:

int f( bool b )
{
   int i;
   if ( b )
   {
      i = 0;
   }
   return i; // i is uninitialized if b is false
}

To correct this warning, initialize the variable as shown in the following code:

int f( bool b )
{
   int i = -1;

   if ( b )
   {
      i = 0;
   }
   return i;
}

Heuristics

The following example shows that passing a variable to a function by reference causes the compiler to assume that it's initialized:

void init( int& i );

int f( bool b )
{
   int i;

   init(i);

   if ( b )
   {
      i = 0;
   }
   return i; // i is assumed to be initialized because it's passed by reference to init()
}

This supports the pattern of passing a pointer to a variable into an initialization function.

This heuristic can lead to false negatives because many functions expect pointers that point to initialized data. Use SAL annotations, such as _In_ and _Out_, to describe the function's behavior. The following example calls a function that expects its argument to be initialized, so a warning is generated:

void use( _In_ int& i );

int f( bool b )
{
   int i;

   use(i); // uninitialized variable warning because of the _In_ annotation on use()

   if ( b )
   {
      i = 0;
   }
   return i;
}

See also

Compiler Warning (level 1 and level 4) C4700