C语言 我如何修复这个溢出?



写超限出现超限错误。

> #define FLOORSNUMBER             128 
> #define ILAFLOORSNUMBER          40
> #else
> #define ILAFLOORSNUMBER          40
> 
> uint8 downCallSide[ILAFLOORSNUMBER]; extern uint16  
> callLightTimerAside[FLOORSNUMBER]; extern uint16  
> callLightTimerBside[FLOORSNUMBER];

for(i = 0;i <= FLOORSNUMBER;i++)
{

**CID 18019 (#1 of 3): Out-of-bounds write (OVERRUN)
overrun-local: Overrunning array ilaByPass.downCallSide of 40 bytes at byte offset 128 using index i (which evaluates to 128)**
ilaByPass.downCallSide[i] = OFFSTATE;

#ifndef NA 

**CID 17746 (#1 of 3): Out-of-bounds write (OVERRUN)**
**overrun-local: Overrunning array callLightTimerAside of 128 2-byte elements at element index 128 (byte offset 257) using index i (which evaluates to 128).**
callLightTimerAside[i] = OFFSTATE;

我知道当我们尝试downcallside[i]时,会出现溢出,因为i的值上升到128,而downcallside的大小只有40。我该如何解决?

对于callLightTimerAside[i],大小似乎与floornumber相同,但仍然存在溢出

这是一些奇怪的C.不太确定发生了什么,但是

for(i = 0;i <= FLOORSNUMBER;i++)

这几乎肯定是一个错误。循环遍历索引0-128,而声明的数组长度为128,索引为0-127。第128项索引不存在。

for (i = 0; i < FLOORSNUMBER; i++)

最新更新