warning: array subscript is above array bounds [-Warray-bounds]

12,379

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.

Share:
12,379

Related videos on Youtube

OBLE Codemonkey
Author by

OBLE Codemonkey

Updated on June 04, 2022

Comments

  • OBLE Codemonkey
    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 why DICE_NUMBER is.

    D'oh. The problem is that the third integer output there on the fprintf is DICE_BONUS but its on another line, I thought the compiler was warning me about DICE_NUMBER, it was warning me about BONUS.

    • jrok
      jrok over 10 years
      SSCCE please, or it didn't happen.
    • spiritwolfform
      spiritwolfform over 10 years
      DICE_BONUS will obviously be out of bounds in your case
  • SirGuy
    SirGuy over 7 years
    In fact, considering that his format string takes three integers meant to display stuff like 4d6 + 1 it's seems likely that the rest of the printf function call includes pMobIndex->mana[DICE_BONUS] further on.

Related