C1857 Warning Access out of range

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

C1857 Warning Access out of range

Jump to solution
1,110 Views
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

Labels (1)
Tags (4)
0 Kudos
1 Solution
889 Views
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?

View solution in original post

0 Kudos
6 Replies
889 Views
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?

889 Views
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 Kudos
889 Views
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 Kudos
889 Views
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 Kudos
890 Views
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 Kudos
889 Views
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 Kudos