I have written a program for checking array out of bounds for global array and local array.
Case 1:
/* Local and global Aoob demo*/
uint8 problematic_array_global[5] = {22,34,65,44,3};
uint16 adjacent_array_global[3] = {82,83,84};
void aoob_example()
{
/*Global*/
printf(" Before global Aoob , adjacent_array={%u,%u,%u}\n",adjacent_array_global[0],adjacent_array_global[1],adjacent_array_global[2]);
uint8 idx = 0;
for(;idx < 15;idx++)
{
problematic_array_global[idx] = idx;
}
printf(" After global Aoob , adjacent_array={%u,%u,%u}\n",adjacent_array_global[0],adjacent_array_global[1],adjacent_array_global[2]);
And got the result:
Before global Aoob , adjacent_array_global={82,83,84}
After global Aoob , adjacent_array_global={2312,2826,3340}
Case 2:
void aoob_example()
{
uint8 problematic_array[5] = {1,2,3,4,5};
uint8 adjacent_array[3] = {12,13,14};
printf(" Before Aoob var=%u , adjacent_array={%u,%u,%u}\n",var,adjacent_array[0],adjacent_array[1],adjacent_array[2]);
uint8 idx = 0;
for(;idx < 8; idx++)
{
problematic_array[idx] = idx;
}
printf(" After Aoob var =%u , adjacent_array={%u,%u,%u}\n",var,adjacent_array[0],adjacent_array[1],adjacent_array[2]);
And got the result:
Before Aoob var=10 , adjacent_array = {12,13,14}
After Aoob var =10 , adjacent_array = {12,13,14}
Seems with local array we didn't have any array out of bounds although I have declared 2 local arrays nearby and for loop up to 8 iteration.
What is difference between 2 declaration? How is an array stored in memory in this program? What happened here? How to understand this behavior in c?