简体   繁体   English

警告:数组下标超出数组范围[-Warray-bounds]

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

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. DICE_NUMBER在我的头文件中定义为0。

Obviously, 0 is not exceeding the size of the array. 显然,0不超过数组的大小。

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. 我完全不知道为什么要这样做,因为0显然在数组的范围内。 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. 我的引擎代码一半扔,现在这些数组边界错误,我已经得到了他们的约30%,而他们中没有道理给我。

Here is the output from make: 这是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. 我知道DICE_BONUS将会(仅现在实现),但是我一生无法弄清楚DICE_NUMBER是为什么。

D'oh. D'哦。 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. 问题是fprintf上的第三个整数输出是DICE_BONUS但是在另一行,我认为编译器警告我DICE_NUMBER ,警告我BONUS。

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. mana[2]是两个元素的整数数组, DICE_BONUS定义为2,因此mana[2]表示您正在尝试访问第三个元素。

REMEMBER array starts from 0 subscript. REMEMBER数组从0下标开始。 So all warnings related to DICE_BONUS is valid. 因此,与DICE_BONUS相关的所有警告均有效。 You need to redefine your array for three elements. 您需要为三个元素重新定义数组。

Now about DICE_NUMBER that is not causing warning. 现在大约DICE_NUMBER不会引起警告。 Perhaps you have some additional argument on that line which includes DICE_BONUS there too. 也许您在该行上还有一些其他参数,其中也包括DICE_BONUS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM