gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > warning: array subscript is above array bounds

#156312 - Quirky - Thu May 08, 2008 8:58 pm

Anyone know if this is a "feature" of gcc 4.3? It happens with DKA r22 and this code:

Code:

#include <algorithm>

const static int COUNT(8);
unsigned char vals[COUNT] = {0};

bool doStuff(unsigned char a)
{
  unsigned char *begin = vals;
  unsigned char *end = (vals+COUNT);
  return std::find(begin, end, a) != end;
}

/* Compiled with these flags:
$DEVKITARM/bin/arm-eabi-g++ -c -O2 -Wall fn.cpp -o fn.o
*/


This is a bit daft - std::find should work with normal C arrays too. Compiling with -O1 "fixes" the problem.

#156314 - silent_code - Thu May 08, 2008 9:06 pm

EDIT: you are right. skip this post then. stl container's .end() points past the last element. that makes the problem even more interesting. ;^p

sidenote: iirc, the last stable (might only apply to the mingw version) gcc release was 3.4.5. makes perfect sense to me if that is why 4.3 might behave a bit buggy here.

anyways, good luck!


origianl post:

end is out of bounds. it should be at vals + COUNT - 1. :^)
(take pen and paper and test it yourself.) ;^)

i don't know why O1 fixes it, though. maybe a different allocation scheme is used or semething. (i don't know much about how the optimization switches work in detail, just how to use them and the general optimizations they do, so the following is totally fictional: imagine instead of allocating the two words you specified, it silently allocates four words to improve memory alignment.)

Code:
mem  val  index
---------------
0x17 AAAA 0 (start)
0x18 BBBB 1
0x19 CCCC 2 (end)

count = 3
start = 0x17
end = 0x17 + 2


@ PeterM: good point, i'll check that.


Last edited by silent_code on Thu May 08, 2008 9:30 pm; edited 3 times in total

#156315 - PeterM - Thu May 08, 2008 9:16 pm

I believe for C++ algorithms, "end" should point to one past the last element.
_________________
http://aaiiee.wordpress.com/

#156316 - Quirky - Thu May 08, 2008 9:20 pm

Yes, the end of an array is one past the last element. Normally you would use vectors or something similar where begin() and end() would hide this implementation detail, but the principal is the same for c-arrays. http://www.sgi.com/tech/stl/stl_introduction.html

Back to the question... -Wno-array-bounds is the way to go for now, as the bounds checking gives false positives, misses cases, etc. Seems the array bounds checking is still a bit buggy in gcc 4.3.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35587