Operating System - HP-UX
1830041 Members
1720 Online
109998 Solutions
New Discussion

Re: Pointers to unaligned variables in cc(bundled)

 
Gilles Allard
Advisor

Pointers to unaligned variables in cc(bundled)

My application requires access to variables through pointers. There is a "bus error" when a short is on a odd address or when a long is not on an address divisible by 4.
I know that ANSI-C compiler provides a +u1 switch but I absolutely need to use the bundled compiler. Is there an equivalent way?
I actually use macros to access those variables but I would prefer a nicer way.
Thanks
1 REPLY 1
A. Clay Stephenson
Acclaimed Contributor

Re: Pointers to unaligned variables in cc(bundled)

Hi Gilles,

There is no direct workaround for this. You really need the +u1 option which is not available.

One workaround (it's not pretty) is to put all the variables in question inside a struct and then use the #praga HP_ALIGN NOPADDING PUSH

This has the effect of aligning the struct members on byte boundaries and saving the current alignment. You then use the #pragma HP_ALIGN POP to restore the original alignment. The #pragma's must be at the file scope level and thus any of your routines which manipulate the variables should be inside a function.

Years ago I had to use macros like you describe for just this purpose. Make sure that you put some defines in your code so that the macros can handle both bigg-endian and little-endian data representation.

Regards, Clay
If it ain't broke, I can fix that.