Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions docs/code-quality/c6001.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ title: Warning C6001
ms.date: 10/04/2022
f1_keywords: ["C6001", "USING_UNINIT_VAR", "__WARNING_USING_UNINIT_VAR"]
helpviewer_keywords: ["C6001"]
ms.assetid: 55e779f1-7295-48f7-8ce1-b43898b36cd8
---
# Warning C6001

Expand All @@ -18,7 +17,7 @@ Code analysis name: `USING_UNINIT_VAR`

## Example

The following code generates this warning because variable `i` is only initialized if `b` is true; otherwise an uninitialized `i` is returned:
The following code generates this warning because variable `i` is only initialized if `b` is true:

```cpp
int f( bool b )
Expand Down Expand Up @@ -49,9 +48,7 @@ int f( bool b )

## Heuristics

Variables are also considered initialized when they're passed by reference to
another function. Thus, the following example would also consider `i` to be
initialized.
The following example shows that passing a variable to a function by reference causes the compiler to assume that it's initialized:

```cpp
void init( int& i );
Expand All @@ -66,18 +63,13 @@ int f( bool b )
{
i = 0;
}
return i; // i is assumed to be initialized by init(i)
return i; // i is assumed to be initialized because it's passed by reference to init()
}
```

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

Since many functions expect pointers to point to initialized data already, this
heuristic can lead to false negatives. [SAL annotations] such as `_In_` and
`_Out_` can be used to more precisely describe a function's behavior. For
example, in the following we call an external function that expects its argument
to already be initialized and the warning is still generated.
This heuristic can lead to false negatives because many functions expect pointers that point to initialized data. Use [SAL annotations](annotating-function-parameters-and-return-values.md), 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:

```cpp
void use( _In_ int& i );
Expand All @@ -86,7 +78,7 @@ int f( bool b )
{
int i;

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

if ( b )
{
Expand All @@ -96,8 +88,6 @@ int f( bool b )
}
```

[SAL annotations]: ./annotating-function-parameters-and-return-values.md

## See also

[Compiler Warning (level 1 and level 4) C4700](../error-messages/compiler-warnings/compiler-warning-level-1-and-level-4-c4700.md)