In an iOS app, I have a structure that looks like this:
typedef struct _Pixel {
signed char r;
signed char g;
signed char b;
} Pixel;
In my code, I allocate an array of them using calloc:
Pixel* buff = calloc(width * height, sizeof(Pixel));
Now this works fine in the simulator, but on the device, if I try to access buff[width * height - 1](i.e. the last element in buff), I get EXC_BAD_ACCESS.
This didn't make sense to me, so after a few hours of debugging, I wondered if this was some kind of alignment problem, so by the whim I tried:
typedef struct _Pixel {
signed char r;
signed char g;
signed char b;
signed char padding;
} Pixel;
making the pixel size equal to two.
EXC_BAD_ACCESS, . - , ? , , ( , , ).