On Tue, 31 Aug 2021 at 09:11, Utkarsh Singh wrote: > > Hello GCC mailing list, > > In one of my friend's C programming class, they asked him a question on > the topic of array bounds based on the follwing code snippet: > > #include <stdio.h> > > int main(void) > { > char str[] = {'G' , 'C' , 'C' }; > str[3] = '\0' ; /* Isn't this invalid? */ > printf("%s\n", str); > } > > In an ideal case, str[3] should be a case of out-of-bound array access. > But when compiling the above with -Wall option flag GCC shows no > warning. So, am I missing something?
This question belongs on the gcc-help mailing list, not here. The code has undefined behaviour. Some GCC warnings depend on checks done during optimization. GCC will warn about this code if you use -Wall -O2 and you will get a runtime error if you compile with -fsanitize=undefined