Aligned access to c structures (how to avoid)

Bjorn Engsig bengsig at dk.oracle.com
Thu Feb 14 21:03:37 AEST 1991


Article <5053 at lure.latrobe.edu.au> by CCHD at lure.latrobe.edu.au (Huw Davies - La Trobe University Computer Centre) says:
|
|struct test
|{
|    short a;
|    int b;
|} c;
|
|        printf("a: %x\nb: %x\nSize of a: %d\n", &c.a, &c.b, 
|                                                  (int) &c.b - (int) &c.a);
|
|a: 2003e460
|b: 2003e464
|Size of a: 4
|
|I would have liked the difference in address to be 2, not 4.
The ANSI C standard does actually allow the implementor to add padding
between any structure member (not before the first one), so you should not
make assumptions about this.  I'm afraid you will need to rewrite your code
to make it behave the way you want, i.e. declare the structure differently
and do the packing in and out yourself.
| I have searched high and low all
|over our CD-ROM and can't find an option to the c compiler to force
|the slow (but nice) way.
Personally, I don't see what is nice about it, but I do of course not know
your application.
-- 
Bjorn Engsig, ORACLE Corporation, E-mail: bengsig at oracle.com, bengsig at oracle.nl

            "Stepping in others footsteps, doesn't bring you ahead"



More information about the Comp.unix.aix mailing list