Referencing outside the bounds of an array does not generate an error
Hi all,
When I accidentaly tried to reference outside the bounds of an array in C, I surprisingly discovered that not only the compiler (GCC) didn't generate an error, but the reference indeed seemed valid as I got the assigned value with printf.
In particular, I had the following:
Code:
uint32_t ArrayA[10][2];
void test_wrong_referece(void);
void test_wrong_referece(void)
{
uint32_t x=100;
ArrayA[0][1]=x;
ArrayA[0][2]=x+100;
ArrayA[0][3]=x+200;
printf("ArrayA[0][1] = %d\n", ArrayA[0][1]);
printf("ArrayA[0][2] = %d\n", ArrayA[0][2]);
printf("ArrayA[0][3] = %d\n", ArrayA[0][3]);
}
main(){
test_wrong_referece();
return 0;
}
Output:
ArrayA[0][1] = 100
ArrayA[0][2] = 200
ArrayA[0][3] = 300
The 'ArrayA[0][2]=x+100;' and 'ArrayA[0][3]=x+200;' shoudn't be valid as they exceed the bounds of ArrayA so I would expect a run-time error to be generated when I tried to access (and of course GCC to inform me before that).
Could someone help me on that please?:confused:
Thanks in advance.