Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!shared!johnp From: johnp@shared.uucp (John Plattner) Newsgroups: comp.unix.aix Subject: Re: Aligned access to c structures (how to avoid) Message-ID: <1991Feb15.220246.35565@shared.uucp> Date: 15 Feb 91 22:02:46 GMT References: <5053@lure.latrobe.edu.au> <1254@dkunix9.dk.oracle.com> Organization: Shared Financial Systems, Dallas, TX Lines: 44 In article <1254@dkunix9.dk.oracle.com> bengsig@dk.oracle.com (Bjorn Engsig) writes: >Article <5053@lure.latrobe.edu.au> by CCHD@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 <<<---