Aligned access to c structures (how to avoid)

John Plattner johnp at shared.uucp
Sat Feb 16 09:02:46 AEST 1991


In article <1254 at dkunix9.dk.oracle.com> bengsig at dk.oracle.com (Bjorn Engsig) writes:
>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.

>Personally, I don't see what is nice about it, but I do of course not know
>your application.

If you REALLY want to bash IBM a little bit, get their COBOL (shudder shudder)
compiler and create an external COBOL structure with :

      01  test external.
	  02 a pic 9(4).
	  02 b pic 9(9).

and try to share it with the equivalent C global structure. You will find that
the COBOL compiler does NOT boundary align, and you get nice weird results.

IBM's only response is "well, you'll have to manually align the COBOL
structure".

-- 
        John Plattner                          Shared Financial Systems
        uunet!shared!johnp                     Dallas, Tx 
        Phone: 214-458-3839                    Fax: 214-458-3876
 --->>> My comments are my opinions and may not be shared by Shared <<<---



More information about the Comp.unix.aix mailing list