Compiling the following code snippet: int main(int argc, char **argv) { int i; int asize = 3; int array[3];
for (i = asize - 1; i < asize; i++) array[i] = 42; for (i = 0; i < asize; i++) printf("%i\n", array[i]); } with "gcc -O -Wunreachable-code gccbug.c" shows the following warning message: gccbug.c: In function 'main': gccbug.c:8: warning: will never be executed (Where line 8 is the body of the first for-loop, array[i] = 42;) Though, array[2] is set to 42, as the later printf shows when executing. When replacing the "- 1" of line 7 with an higher value, e.g. "- 2", the warning disappears. Not passing "-O" to the compiler lets the warning disappear, too. I tested it with two versions of gcc. The first: Using built-in specs. Target: x86_64-pc-linux-gnu Configured with: /var/tmp/portage/sys-devel/gcc-4.3.4/work/gcc-4.3.4/configure --prefix=/usr --bindir=/usr/x86_64-pc-linux-gnu/gcc-bin/4.3.4 --includedir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/include --datadir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.3.4 --mandir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.3.4/man --infodir=/usr/share/gcc-data/x86_64-pc-linux-gnu/4.3.4/info --with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.3.4/include/g++-v4 --host=x86_64-pc-linux-gnu --build=x86_64-pc-linux-gnu --disable-altivec --disable-fixed-point --enable-nls --without-included-gettext --with-system-zlib --disable-checking --disable-werror --enable-secureplt --enable-multilib --enable-libmudflap --disable-libssp --enable-libgomp --disable-libgcj --enable-languages=c,c++,treelang,fortran --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu --with-bugurl=http://bugs.gentoo.org/ --with-pkgversion='Gentoo 4.3.4 p1.0, pie-10.1.5' Thread model: posix gcc version 4.3.4 (Gentoo 4.3.4 p1.0, pie-10.1.5) The second: Using built-in specs. Target: arm-elf Configured with: /tmp/gcc.ndS8/gcc-4.4.1/configure --enable-languages=c --with-mpfr=/usr --without-headers --target=arm-elf --prefix=/usr/local/arm-elf --with-local-prefix=/usr/local/arm-elf --disable-werror --disable-multilib --disable-shared --disable-nls --disable-libssp --with-arch=armv4t --with-cpu=arm7tdmi-s Thread model: single gcc version 4.4.1 (GCC) The warning appears with both gcc versions. -- Summary: "warning: will never be executed" about code which is executed with "-Wunreachable-code" Product: gcc Version: 4.4.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: agraf at znc dot in http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42476