public inbox for ecos-discuss@sourceware.org
 help / color / mirror / Atom feed
From: "Trenton D. Adams" <tadams@theone.dnsalias.com>
To: "'Andrew Lunn'" <andrew.lunn@ascom.ch>
Cc: <ecos-discuss@sources.redhat.com>
Subject: RE: [ECOS] data alignment issues
Date: Thu, 09 Aug 2001 09:11:00 -0000	[thread overview]
Message-ID: <003001c120ed$d36871e0$090110ac@TRENT> (raw)
In-Reply-To: <20010809180507.H3846@biferten.ma.tech.ascom.ch>

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

  reply	other threads:[~2001-08-09  9:11 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-08-09  0:56 rob.wj.jansen
2001-08-09  7:44 ` Trenton D. Adams
2001-08-09  7:55   ` Andrew Lunn
2001-08-09  7:59     ` Trenton D. Adams
2001-08-09  8:49     ` Trenton D. Adams
2001-08-09  9:05       ` Andrew Lunn
2001-08-09  9:11         ` Trenton D. Adams [this message]
2001-08-09 11:42   ` Grant Edwards
  -- strict thread matches above, loose matches on Subject: below --
2001-08-09  8:19 rob.wj.jansen
2001-08-08 10:00 Trenton D. Adams

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='003001c120ed$d36871e0$090110ac@TRENT' \
    --to=tadams@theone.dnsalias.com \
    --cc=andrew.lunn@ascom.ch \
    --cc=ecos-discuss@sources.redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).