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
已解决! 转到解答。
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?
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?
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
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?
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
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?