File tree Expand file tree Collapse file tree 1 file changed +52
-1
lines changed Expand file tree Collapse file tree 1 file changed +52
-1
lines changed Original file line number Diff line number Diff line change @@ -37,7 +37,7 @@ To correct this warning, initialize the variable as shown in the following code:
37
37
```cpp
38
38
int f( bool b )
39
39
{
40
- int i= -1;
40
+ int i = -1;
41
41
42
42
if ( b )
43
43
{
@@ -47,6 +47,57 @@ int f( bool b )
47
47
}
48
48
```
49
49
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
+
50
101
## See also
51
102
52
103
[ Compiler Warning (level 1 and level 4) C4700] ( ../error-messages/compiler-warnings/compiler-warning-level-1-and-level-4-c4700.md )
You can’t perform that action at this time.
0 commit comments