Skip to content

Repo sync for protected CLA branch #4535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 5, 2023
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
53 changes: 52 additions & 1 deletion docs/code-quality/c6001.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ To correct this warning, initialize the variable as shown in the following code:
```cpp
int f( bool b )
{
int i= -1;
int i = -1;

if ( b )
{
Expand All @@ -47,6 +47,57 @@ 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.

```cpp
void init( int& i );

int f( bool b )
{
int i;

init(i);

if ( b )
{
i = 0;
}
return i; // i is assumed to be initialized by init(i)
}
```

This is to support 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.

```cpp
void use( _In_ int& i );

int f( bool b )
{
int i;

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

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

[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)