Why uninitialized instead of out-of-bounds?

I believe this could be the case here: in the first code, GCC notices that you don’t need the entire char array at all, just b[9], so it can replace the code with

char b_9; // = ???
printf("b[9] = %d\n", b_9);

Now, this is a completely legal transform, because as the array was accessed out of bounds, the behaviour is completely undefined. Only in latter phase does it then notice that this variable, which is a substitute for b[9], is uninitialized, and issues the diagnostics message.

Why I believe this? Because if I add just any code that will reference the array’s address in memory, for example printf("%p\n", &b[8]); anywhere, the array now is fully realized in memory, and compiler will diagnose array subscript is above array bounds.


What I find even more interesting is that GCC does not diagnose out-of-bounds access at all unless optimizations are enabled. This would again suggest that whenever you’re writing a program new program you should compile it with optimizations enabled to make the bugs highly visible instead of keeping them hidden with debug mode 😉

Leave a Comment

techhipbettruvabetnorabahisbahis forumuedueduseduedusedusedusedusedusedusedus