warning: array subscript is above array bounds [-Warray-bounds]
mana[2]
is an integer array for two elements and DICE_BONUS
is defined as 2 so mana[2]
means you are trying to access third element.
REMEMBER array starts from 0
subscript. So all warnings related to DICE_BONUS
is valid. You need to redefine your array for three elements.
Now about DICE_NUMBER
that is not causing warning. Perhaps you have some additional argument on that line which includes DICE_BONUS
there too.
Related videos on Youtube
OBLE Codemonkey
Updated on June 04, 2022Comments
-
OBLE Codemonkey almost 2 years
I'm having a problem with the following piece of code throwing warnings and hoping you could help me:
fprintf (fp, "%dd%d+%d ", pMobIndex->mana[DICE_NUMBER],
DICE_NUMBER
is defined in my header file as 0.Obviously, 0 is not exceeding the size of the array.
The array is defined as.
int mana[2];
I have absolutely no idea why it would be doing this, as 0 is clearly within the bounds of the array. Half of my engines code is throwing these array bound errors now, I've got about 30 of them, and NONE of them make sense to me.
Here is the output from make:
gcc -O3 -s -Wall -c -o obj/olc_save.o olc_save.c olc_save.c: In function 'save_mobile': olc_save.c:234:13: warning: array subscript is above array bounds [-Warray-bounds] fprintf (fp, "%dd%d+%d ", pMobIndex->mana[DICE_NUMBER], ^
it also happens:
db1.c: In function 'create_mobile': db1.c:2056:30: warning: array subscript is above array bounds [-Warray-bounds] + pMobIndex->mana[DICE_BONUS];
and
olc_act.c: In function 'medit_manadice': olc_act.c:6500:15: warning: array subscript is above array bounds [-Warray-bounds] pMob->mana[DICE_BONUS] = atoi (bonus);
The definition in my header file:
/* dice */ #define DICE_NUMBER 0 #define DICE_TYPE 1 #define DICE_BONUS 2
I'm aware
DICE_BONUS
will be (realizing it only now) but I for the life of me cannot figure out whyDICE_NUMBER
is.D'oh. The problem is that the third integer output there on the
fprintf
isDICE_BONUS
but its on another line, I thought the compiler was warning me aboutDICE_NUMBER
, it was warning me about BONUS.-
jrok over 10 yearsSSCCE please, or it didn't happen.
-
spiritwolfform over 10 yearsDICE_BONUS will obviously be out of bounds in your case
-
-
SirGuy over 7 yearsIn fact, considering that his format string takes three integers meant to display stuff like
4d6 + 1
it's seems likely that the rest of theprintf
function call includespMobIndex->mana[DICE_BONUS]
further on.