I have a small program, below, which uses an uninitialized field in a struct. I compile the program with -Wuninitialized -Wmissing-field-initializers -Wall -Wextra -Werror
(Godbolt link below), and the program compiles OK and runs, but prints uninitialized garbage values when reading the struct.
Is there a way to get a warning for this kind of programming error, where an uninitialized variable is used?
#include <stdio.h>
enum FWPixelDepth {
FWPixelDepthColor8888,
};
struct FWBitmapDecodeOptions {
FWPixelDepth pixelDepth;
float scale;
bool decodeWithoutPremultiply;
};
static void p(FWBitmapDecodeOptions opts) {
printf("%d, %f, %d", opts.pixelDepth, opts.scale, opts.decodeWithoutPremultiply);
}
int main() {
FWBitmapDecodeOptions opts;
opts.decodeWithoutPremultiply = true;
p(opts);
}
Clang 8.0.0 flags: -O3 -Wuninitialized -Wmissing-field-initializers -Wall -Wextra -Werror
Sample output:
-1557482600, 0.000000, 1