You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following code generates this warning because variable `i` is only initialized if `b` is true; otherwise an uninitialized `i` is returned:
20
+
The following code generates this warning because variable `i` is only initialized if `b` is true:
22
21
23
22
```cpp
24
23
intf( bool b )
@@ -49,9 +48,7 @@ int f( bool b )
49
48
50
49
## Heuristics
51
50
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.
51
+
The following example shows that passing a variable to a function by reference causes the compiler to assume that it's initialized:
55
52
56
53
```cpp
57
54
voidinit( int& i );
@@ -66,18 +63,13 @@ int f( bool b )
66
63
{
67
64
i = 0;
68
65
}
69
-
return i; // i is assumed to be initialized by init(i)
66
+
return i; // i is assumed to be initialized because it's passed by reference to init()
70
67
}
71
68
```
72
69
73
-
This is to support the pattern of passing a pointer to a variable into
74
-
an initialization function.
70
+
This supports the pattern of passing a pointer to a variable into an initialization function.
75
71
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.
72
+
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:
81
73
82
74
```cpp
83
75
void use( _In_ int& i );
@@ -86,7 +78,7 @@ int f( bool b )
86
78
{
87
79
int i;
88
80
89
-
use(i); // uninitialized variable warning because of _In_ annotation on use
81
+
use(i); // uninitialized variable warning because of the _In_ annotation on use()
0 commit comments