why -> warning: array subscript is above array bounds

田部 syokandake@gmail.com
Thu May 28 19:41:00 GMT 2015


Hi there
I Japanese.
Poor English.
I like gcc.
But,
Why,
gcc is, in this code, or put out a warning?
-O4 only???

+ cat -n bug.c
      1    #include <stdio.h>
      2    int    ary[2][12] ;
      3    void    func(int    x,int     y)
      4    {
      5        int    i,j ;
      6        for(i = 0 ; i < x ; i++) {
      7            for(j = 0 ; j < y ; j++) {
      8                ary[i][j + y]  = 0 ;
      9            }
     10        }
     11    }
     12    int    main()
     13    {
     14        func(2,2) ;
     15        return    0 ;
     16    }
     17
+ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-cygwin/4.9.2/lto-wrapper.exe
Target: x86_64-pc-cygwin
Configured with: 
/cygdrive/i/szsz/tmpp/gcc/gcc-4.9.2-3.x86_64/src/gcc-4.9.2/configure 
--srcdir=/cygdrive/i/szsz/tmpp/gcc/gcc-4.9.2-3.x86_64/src/gcc-4.9.2 
--prefix=/usr --exec-prefix=/usr --bindir=/usr/bin --sbindir=/usr/sbin 
--libexecdir=/usr/libexec --datadir=/usr/share --localstatedir=/var 
--sysconfdir=/etc --libdir=/usr/lib --datarootdir=/usr/share 
--docdir=/usr/share/doc/gcc --htmldir=/usr/share/doc/gcc/html -C 
--build=x86_64-pc-cygwin --host=x86_64-pc-cygwin 
--target=x86_64-pc-cygwin --without-libiconv-prefix 
--without-libintl-prefix --libexecdir=/usr/lib --enable-shared 
--enable-shared-libgcc --enable-static 
--enable-version-specific-runtime-libs --enable-bootstrap 
--enable-__cxa_atexit --with-dwarf2 --with-tune=generic 
--enable-languages=ada,c,c++,fortran,lto,objc,obj-c++ --enable-graphite 
--enable-threads=posix --enable-libatomic --enable-libgomp 
--disable-libitm --enable-libquadmath --enable-libquadmath-support 
--enable-libssp --enable-libada --enable-libgcj-sublibs 
--disable-java-awt --disable-symvers 
--with-ecj-jar=/usr/share/java/ecj.jar --with-gnu-ld --with-gnu-as 
--with-cloog-include=/usr/include/cloog-isl --without-libiconv-prefix 
--without-libintl-prefix --with-system-zlib --enable-linker-build-id
Thread model: posix
gcc version 4.9.2 (GCC)
+ gcc -Wall -W -O2 bug.c
+ echo 'Why  warn?'
Why  warn?
+ gcc -Wall -W -O4 bug.c
bug.c: In function 'func':
bug.c:8:10: warning: array subscript is above array bounds [-Warray-bounds]
     ary[i][j + y]  = 0 ;
           ^
bug.c:8:10: warning: array subscript is above array bounds [-Warray-bounds]
bug.c:8:10: warning: array subscript is above array bounds [-Warray-bounds]
bug.c:8:10: warning: array subscript is above array bounds [-Warray-bounds]
bug.c:8:10: warning: array subscript is above array bounds [-Warray-bounds]
bug.c:8:10: warning: array subscript is above array bounds [-Warray-bounds]



More information about the Gcc-bugs mailing list