gcc compiler ignores uninitialized variable warning for debug build. This looks very weird for me, can some one help me to understand this ?
## Program
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i, max;
int count;
if (argc < 2) {
return -1;
}
max = atoi(argv[1]);
for (i = 0; i < max; i++) {
count++;
}
printf("count is %d\n", count);
return 0;
}
gcc a.c -g -Wall -Werror
No warning
gcc a.c -O3 -Wall -Werror
a.c: In function ‘main’:
a.c:8:9: error: ‘count’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
int count;
^~~~~
cc1: all warnings being treated as errors
gcc version: 7.4.0
gcc a.c -O3 -g -Wall -Werror(and throw in-Wextrawhile you're at it, and maybe some other warning flags). That gets you the best of both worlds — debugging information and optimization. I use this combination most of the time. It does mean that if you have to run the debugger, you might get slightly peculiar behaviour as code is moved or removed by the optimizer. But it does work most of the time. If you're in for a serious debugging session, drop the-O3— otherwise, use both.