[comp.unix.aix] Aligned access to c structures

CCHD@lure.latrobe.edu.au (Huw Davies - La Trobe University Computer Centre) (02/14/91)

I have a slight problem with the AIX v3 c compiler (on an RS/6000).
The following code doesn't do what I want :-) Please note that this
is an example of the error extracted from debugging 6K lines of c code
(It took a while to find....).

$ more test_struct.c
#include <stdio.h>

struct test
{
    short a;
    int b;
} c;

main()
{
        printf("a: %x\nb: %x\nSize of a: %d\n", &c.a, &c.b, 
                                                  (int) &c.b - (int) &c.a);
}

$ ./test
a: 2003e460
b: 2003e464
Size of a: 4
$ 

I would have liked the difference in address to be 2, not 4. I know
that there are efficiency reasons for aligned access, but I would like
to be able to override the default. 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.

Huw Davies
Computing Services

e-mail: cchd@lucifer.latrobe.edu.au

bengsig@dk.oracle.com (Bjorn Engsig) (02/14/91)

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.
| 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@oracle.com, bengsig@oracle.nl

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

marc@marc.watson.ibm.com (Marc Auslander) (02/14/91)

The trouble with the sort of packing you propose is that it does not
provide "left to right equivalence".  In other words, adding something
to the end of a structure can change the mappings of fields that
precede it.  Consider the trouble this causes when you need to extend
a structure without recompiling the world.
--


Marc Auslander       <marc@ibm.com>

johnp@shared.uucp (John Plattner) (02/16/91)

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 <<<---

bengsig@dk.oracle.com (Bjorn Engsig) (02/18/91)

Article <1991Feb15.220246.35565@shared.uucp> by johnp@shared.uucp (John Plattner) says:
|If you REALLY want to bash IBM a little bit, get their COBOL (shudder shudder)
| [ example ]
|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.
This behaviour is not specific to IBM but to Micro Focus cobol, which
is what is behind the IBM VS Cobol.  I'm on thin ice here, but I actually think
cobol is required to behave like that (because of data file portability); please
correct me if I'm wrong.
-- 
Bjorn Engsig, ORACLE Corporation, E-mail: bengsig@oracle.com, bengsig@oracle.nl

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

bengsig@dk.oracle.com (Bjorn Engsig) (02/18/91)

Article <MARC.91Feb14091358@marc.watson.ibm.com> by marc@marc.watson.ibm.com (Marc Auslander) says:
|Consider the trouble this causes when you need to extend
|a structure without recompiling the world.
Some people in the real world are used to do that on many occasions.  Luckily
we have make.
-- 
Bjorn Engsig, ORACLE Corporation, E-mail: bengsig@oracle.com, bengsig@oracle.nl

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

andreas@easix.gun.de (Andreas Baess) (02/26/91)

bengsig@dk.oracle.com (Bjorn Engsig) writes:

>|If you REALLY want to bash IBM a little bit, get their COBOL (shudder shudder)
>| [ example ]
>|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.

>This behaviour is not specific to IBM but to Micro Focus cobol, which
>is what is behind the IBM VS Cobol.  I'm on thin ice here, but I actually think
>cobol is required to behave like that (because of data file portability); please
>correct me if I'm wrong.

This is common in all COBOL implementations I know. There once upon was a time
where main storage was so incredible expensive. Every COBOL programmer knows,
that alignment of data can be enforced be declaring the field with an 01 level
or by adding an sync behind the declaration.

-- 
-> andreas@easix.GUN.de  {tmpmbx,mcshh,smurf,flyer}!easix
-> Was man nicht begreift kann man wenigstens nicht vergessen!