...
Code Block | ||
---|---|---|
| ||
void set_flag(int number, int *sign_flag) { if (sign_flag == NULL) { return; } if (number > 0) { *sign_flag = 1; } else if (number < 0) { *sign_flag = -1; } } void func(int number) { int sign; set_flag(number, &sign); /* ...use sign */ } |
Compilers assume that when the address of an uninitialized variable is passed to a function, the variable is initialized within that function. Because compilers frequently fail to diagnose any resulting failure to initialize the variable, the programmer must apply additional scrutiny to ensure the correctness of the code.
...
Code Block | ||
---|---|---|
| ||
void set_flag(int number, int *sign_flag) { if (sign_flag == NULL) { return; } if (number >= 0) { /* account for number being 0 */ *sign_flag = 1; } else { assert( number < 0); *sign_flag = -1; } } void func(int number) { int sign; set_flag(number, &sign); /* use ...sign */ } |
Non-Compliant Code Example
...