Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
New Contributor
andriusKa
Posts: 3
Registered: ‎01-09-2013
My Device: PlayBook

Bus error during packed structure modification


QNX® Momentics IDE for BlackBerry® Native SDK for Tablet OS
Version: 2.1.0 .

Trying to run following program on PlayBook. I MUST use packed structure.


struct _MESSAGE
{

unsigned char a;
unsigned char b;
double c;
} __attribute__((packed));


int
main(int argc, char **argv)
{
double * ptr;
struct _MESSAGE mes;
ptr= &mes.c;
*ptr=0; //< SIGBUS:bus error

return 0;
}

Does exist the way to solve this error without source modification? May be pragma or compiler flag? This is definitely compiler for arm platform bug, because on x86 everything is ok.


Please use plain text.
Developer
Zmey
Posts: 1,512
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: Bus error during packed structure modification

On ARM it's not safe to take the pointer to misaligned data. On x86 misaligned access is handled by hardware.

Try referencing the variable directly (mes.c = 0), without storing the address in a pointer. This should work as GCC will generate additional code to access it correctly.

Andrey Fidrya, @zmeyc on twitter
Please use plain text.
New Contributor
andriusKa
Posts: 3
Registered: ‎01-09-2013
My Device: PlayBook

Re: Bus error during packed structure modification

This method, of course, will help. But, it may require during porting lot of code changes: poiters, passing arguments to functions, etc. It turned out that the error occurs only for debug version with compiler option-O0. In the case of optimization -01 or- 02 there is no error. I think that is oblivious compiler bug.
Please use plain text.