https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91698
Jakub Jelinek <jakub at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |jakub at gcc dot gnu.org --- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> --- Your testcase is invalid: g++ -g -o pr91698 pr91698.C -std=c++11 -g -O2 -fsanitize=undefined [jakub@tucnak gcc]$ ./pr91698 < /tmp/10 pr91698.C:19:26: runtime error: index 8 out of bounds for type 'float [8]' pr91698.C:19:26: runtime error: load of address 0x7ffd384fc3d0 with insufficient space for an object of type 'float' 0x7ffd384fc3d0: note: pointer points here 00 00 20 c1 e0 7f 5d 6d f0 7f 00 00 00 00 00 00 00 00 00 00 f0 16 40 00 00 00 00 00 00 16 40 00 ^ The bug is on the: while (val <= scores[pos] && pos < num) pos++; line where you perform out of bound access val <= scores[8] and when you encounter UB, anything can happen, as the compiler optimizes based on the assumption that UB doesn't happen. Instead, you should write say: while (pos < num && val <= scores[pos]) pos++;