C1857 Warning Access out of range

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

C1857 Warning Access out of range

ソリューションへジャンプ
1,117件の閲覧回数
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 解決策
896件の閲覧回数
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 返答(返信)
896件の閲覧回数
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?

896件の閲覧回数
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 件の賞賛
896件の閲覧回数
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 件の賞賛
896件の閲覧回数
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 件の賞賛
897件の閲覧回数
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 件の賞賛
896件の閲覧回数
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 件の賞賛