- #include <stdio.h>
- #include <stdlib.h>
- #ifndef MY_S_IS_LOCAL
- struct my_s
- {
- int a[128];
- int b[128];
- int c[128];
- int d[128];
- } my_s;
- #endif /* MY_S_IS_LOCAL */
- int main(int ac, char *av[])
- {
- #ifdef MY_S_IS_LOCAL
- struct my_s
- {
- int a[128];
- int b[128];
- int c[128];
- int d[128];
- } my_s;
- #endif /* MY_S_IS_LOCAL */
- /* silence warnings about unused variables*/
- (void)ac;
- (void)av;
- (void)my_s;
- for(int i=0; i< 256; i++)
- {
- my_s.b[i]=0xdeadbeef;
- }
- /* prevent optimiser from eliminating |my_s| */
- (void)&my_s;
- return EXIT_SUCCESS;
- }
- ##############################################
- # warnings are generated for global variables...
- $ g++ -g -gdwarf-2 -O2 -Wall -Wextra -Waggressive-loop-optimizations x.cpp
- x.cpp: In function ‘int main(int, char**)’:
- x.cpp:33:23: warning: iteration 128u invokes undefined behavior [-Waggressive-loop-optimizations]
- my_s.b[i]=0xdeadbeef;
- ^
- x.cpp:31:2: note: containing loop
- for(int i=0; i< 256; i++)
- ^
- # ... but NO warnings are generated for local variables
- $ g++ -g -gdwarf-2 -DMY_S_IS_LOCAL -O2 -Wall -Wextra -Waggressive-loop-optimizations x.cpp
- $
- # EOF.
gcc does not warn about writes beyond the maximum array index
Posted by Anonymous on Fri 18th Oct 2019 09:59
raw | new post
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.