C1857 Warning Access out of range

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

C1857 Warning Access out of range

跳至解决方案
1,124 次查看
hucklim
Contributor III

Hello everyone,

When I compile my program using Codewarrior version 5.1, I get the following warning:

 

"Warning : C1857: Access out of range

MAIN_Func.c line 62"

 

Here is MAIN_Func.c line 62:

 

position_value[2] = (byte)(dw_DecXPosition & 0x000000FF);

 

position_value[2] is declared as byte, and dw_DecXPosition is declared as double word.

 

I was wondering, if anyone can tell me how to 'improve' the above instruction, so that I would not get this Access out or range warning?

 

Yours sincerely,

Huck

标签 (1)
标记 (4)
0 项奖励
1 解答
903 次查看
BlackNight
NXP Employee
NXP Employee

The compiler is correct :-) you are writing out of bounds.

Your array has two ([2]), elements, so index 0 [0] and 1 [1] are valid indices.

So position_value[2] is clearly out of bounds: you are only allowed to access [0] and [1].

I hope this is clear now?

在原帖中查看解决方案

0 项奖励
6 回复数
903 次查看
TICS_Fiona
NXP Employee
NXP Employee

The Compiler has detected that there is an array access outside of the array bounds.  Could you please check carefully such access that are out of range? How the length of array position_value[]is defined?

903 次查看
hucklim
Contributor III

Hello TICS_Fiona,

The position_value[2] is declared as byte. dw_DecXPosition is declared as double word (4 bytes). Since I am casting dw_DecXPosition by byte, it should not be out of bound, no?

Thanks for your assistance.

Yous sincerely,

Huck

0 项奖励
903 次查看
BlackNight
NXP Employee
NXP Employee

Hi Huck,

it all depends on the details of the position_value declaration (and size of that array). How is it used? global variable? parameter?

Here is what I tried (and works without warning):

unsigned char position_value[6];

unsigned long dw_DecXPosition;

void foo(void) {

  position_value[2] = (byte)(dw_DecXPosition & 0x000000FF);

}

I assume your example must be something different. Can you provide such a snippet?

0 项奖励
903 次查看
hucklim
Contributor III

Hello Eric Styger,

thanks for sharing your example. The following is what I have implemented:

typedef unsigned long dword;

function()

{

     dword dw_DecXPosition = 0;

     byte position_value[2];

     ...

     position_value[2] = (byte)(dw_DecXPosition & 0x000000FF);

     ...

    

}

Do you see anything suspicious from the snippet above? I have similar implementation on other part of the program, but I do not get any warning there.

Yours sincerely,

Huck

0 项奖励
904 次查看
BlackNight
NXP Employee
NXP Employee

The compiler is correct :-) you are writing out of bounds.

Your array has two ([2]), elements, so index 0 [0] and 1 [1] are valid indices.

So position_value[2] is clearly out of bounds: you are only allowed to access [0] and [1].

I hope this is clear now?

0 项奖励
903 次查看
hucklim
Contributor III

Hello Styger,

yes, ... you are right! I miss it big time! How can I have missed this. You come to my rescue again. Thanks!!!

Yours sincerely,

Huck

0 项奖励