Skip to content

Commit c5512ae

Browse files
Learn Build Service GitHub AppLearn Build Service GitHub App
authored andcommitted
Merging changes synced from https://github.com/MicrosoftDocs/cpp-docs-pr (branch live)
2 parents 447b5d8 + c6eb738 commit c5512ae

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

docs/code-quality/c6001.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ To correct this warning, initialize the variable as shown in the following code:
3737
```cpp
3838
int f( bool b )
3939
{
40-
int i= -1;
40+
int i = -1;
4141
4242
if ( b )
4343
{
@@ -47,6 +47,57 @@ int f( bool b )
4747
}
4848
```
4949

50+
## Heuristics
51+
52+
Variables are also considered initialized when they're passed by reference to
53+
another function. Thus, the following example would also consider `i` to be
54+
initialized.
55+
56+
```cpp
57+
void init( int& i );
58+
59+
int f( bool b )
60+
{
61+
int i;
62+
63+
init(i);
64+
65+
if ( b )
66+
{
67+
i = 0;
68+
}
69+
return i; // i is assumed to be initialized by init(i)
70+
}
71+
```
72+
73+
This is to support the pattern of passing a pointer to a variable into
74+
an initialization function.
75+
76+
Since many functions expect pointers to point to initialized data already, this
77+
heuristic can lead to false negatives. [SAL annotations] such as `_In_` and
78+
`_Out_` can be used to more precisely describe a function's behavior. For
79+
example, in the following we call an external function that expects its argument
80+
to already be initialized and the warning is still generated.
81+
82+
```cpp
83+
void use( _In_ int& i );
84+
85+
int f( bool b )
86+
{
87+
int i;
88+
89+
use(i); // uninitialized variable warning because of _In_ annotation on use
90+
91+
if ( b )
92+
{
93+
i = 0;
94+
}
95+
return i;
96+
}
97+
```
98+
99+
[SAL annotations]: ./annotating-function-parameters-and-return-values.md
100+
50101
## See also
51102

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

0 commit comments

Comments
 (0)