This is the mail archive of the ecos-discuss@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: data alignment issues


What I use unions for is for readability mostly.  Take for instance my
structure and union below.  As you can see, both members of the union
are the same size anyhow (for my purposes) so it wouldn't matter in this
case.  I use it for clarity though.  If I was just to put the
commandParameters field in as an "unsigned long commandParameter",
someone looking at my code might not know what it means.  So, using a
union is perfectly clear as long as the other guy understands unions.

// Rig command structures for rig commands.
typedef union
{
    /* delay in seconds for the PAUSE ocmmand*/
    unsigned sDelay;
    /* What to set the RTC to for the INITIALIZE command*/
    unsigned long rtc;
} TCommandParameters;

typedef struct
{
    /* version minor */
    unsigned short versionLS,
    /* version major */
        versionMS;
    /* command to perform */
    unsigned command;
    /* parameters for the command */
    TCommandParameters commandParameters;
} TRigCommand;

-----Original Message-----
From: Andrew Lunn [mailto:andrew.lunn@ascom.ch] 
Sent: Thursday, August 09, 2001 10:05 AM
To: Trenton D. Adams
Cc: 'Andrew Lunn'; ecos-discuss@sources.redhat.com
Subject: Re: [ECOS] data alignment issues


On Thu, Aug 09, 2001 at 09:48:22AM -0600, Trenton D. Adams wrote:
> What about with unions?  When I know damn well both platforms have the
> same size for their primitive data types is it ok to copy an entire
> union to the buffer?   After all, the union will only be the size of
the
> largest data type, and no alignment issues should happen right?

Im not sure i follow what you mean.

If you have a union like

union {
        char    c;
        short   s;
        long    l;
} u;

Then the structure will normaly be 4 bytes long. But what use is such
a union? You normaly have a union of structures

union {
        a_t     a;
        b_t     b;
        c_t     c:
} u;

You then have the issue of how is the compiler aligning the data of
types a_t, b_t, c_t? 

Also, the C standard says "A union type describes an overlapping
nonempty set of member objects, each of which has an optinally
specified name and possible distinct type". I cannot see anywhere
which specifies how they overlap. One compiler may overlap them at the
beginning of the memory and another could overlap them at the end
of the memory!

        Andrew


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]