inckka inckka - 4 months ago 18
C Question

Strange different results when dump two adjacent C arrays on Intel X_86 and ARM architectures

I'm getting two different results when dumping the same two arrays with C, in two different below computer architectures. Can anyone explain me the reason?


  1. Architecture One: x86_64 (ordinary 64bit intel PC with fedora 23)

  2. Architecture Two: armv7l (raspberry pi with raspbian)



here's sample the code

double ytops[7];
double ybots[7];

// Assigning values loop
for(int i=0;i<8;i++)
{
frame_grabber(); // some custom function that gives sensor data with two variables: ruler_top_cal_preset, ruler_bottom_cal_preset integer

ytops[i] = ruler_top_cal_preset;
ybots[i] = ruler_bottom_cal_preset;

printf("ytops[%i]: %f \n", i, ytops[i]);
printf("ybots[%i]: %f \n", i, ybots[i]);
}

printf("---raw-array--- \n");

for(int i = 0; i<8; i++ ){
printf("ytops[%i]: %f \n", i, ytops[i]);
}

printf("--------------- \n");

for(int i = 0; i<8; i++ ){
printf("ybots[%i]: %f \n", i, ybots[i]);
}

printf("-------end----- \n");


Architecture 1(X_86) gives this results:

ytops[0]: 143.000000
ybots[0]: 211.000000
ytops[1]: 143.000000
ybots[1]: 211.000000
ytops[2]: 143.000000
ybots[2]: 175.000000
ytops[3]: 144.000000
ybots[3]: 175.000000
ytops[4]: 144.000000
ybots[4]: 175.000000
ytops[5]: 144.000000
ybots[5]: 175.000000
ytops[6]: 144.000000
ybots[6]: 172.000000
ytops[7]: 143.000000
ybots[7]: 172.000000
---raw-array---
ytops[0]: 143.000000
ytops[1]: 143.000000
ytops[2]: 143.000000
ytops[3]: 144.000000
ytops[4]: 144.000000
ytops[5]: 144.000000
ytops[6]: 144.000000
ytops[7]: 143.000000
---------------
ybots[0]: 211.000000
ybots[1]: 211.000000
ybots[2]: 175.000000
ybots[3]: 175.000000
ybots[4]: 175.000000
ybots[5]: 175.000000
ybots[6]: 172.000000
ybots[7]: 172.000000
-------end-----


Architecture 2 (ARMV7l) gives this:

ytops[0]: 206.000000
ybots[0]: 413.000000
ytops[1]: 206.000000
ybots[1]: 411.000000
ytops[2]: 205.000000
ybots[2]: 427.000000
ytops[3]: 206.000000
ybots[3]: 415.000000
ytops[4]: 205.000000
ybots[4]: 416.000000
ytops[5]: 205.000000
ybots[5]: 421.000000
ytops[6]: 206.000000
ybots[6]: 411.000000
ytops[7]: 206.000000
ybots[7]: 418.000000
---raw-array---
ytops[0]: 418.000000
ytops[1]: 206.000000
ytops[2]: 205.000000
ytops[3]: 206.000000
ytops[4]: 205.000000
ytops[5]: 205.000000
ytops[6]: 206.000000
ytops[7]: 206.000000
---------------
ybots[0]: 413.000000
ybots[1]: 411.000000
ybots[2]: 427.000000
ybots[3]: 415.000000
ybots[4]: 416.000000
ybots[5]: 421.000000
ybots[6]: 411.000000
ybots[7]: 418.000000
-------end-----


Of course the numerical values are different when comparing the both result sets due to the sensor data which were at that movement. Its acceptable.

On architecture one, everything is as assigned by the value assigned loop. Its correct. However look at the architecture two's result set's 'ytops[0]' value in "value assigning loop" and the same value of "raw-array" print.

ytops[0]
is getting the value of
ybots[7]


How this is possible? What is this phenomenon is called? how to avoid this?

LPs LPs
Answer

All your for are wrong.

Using array indexes start from 0 to length_of_array-1

So, using

for(int i=0;i<8;i++)

you are accessing the array out of bounds due to last i value that is 7: your array start from ytops[0] to ytops[6]. This is Undefined Behavior

You must use

for(int i=0;i<7;i++)

Best if you do, for example using ytops array

for(size_t i=0;i<sizeof(ytops)/sizeof(ytops[0]);i++)