Union Misalignment?

Discussion in 'C++' started by Bryan Parkoff, Sep 15, 2005.

  1. byte and word and double word are always in alignment. Do C++ Compiler
    fill the zero as padding inside byte's field and word's field to avoid
    misalignment? It looks like that union does not use shift which it loads
    data from memory. It is like "(H << 8) | L".
    Intel Pentium 4 can degrade the performance if shift is used heavily,
    but union may be recommended to avoid using shift. It may be slow to store
    two bytes into memory before load one word from memory containing two bytes.
    union example is below.
    Please advise.

    union var
    {
    struct
    {
    unsigned __int8 L;
    unsigned __int8 H;
    };
    struct
    {
    unsigned __int16 W;
    };
    };

    Bryan Parkoff
     
    Bryan Parkoff, Sep 15, 2005
    #1
    1. Advertising

  2. Bryan Parkoff

    Jack Klein Guest

    On Thu, 15 Sep 2005 16:29:57 GMT, "Bryan Parkoff" <>
    wrote in comp.lang.c++:

    > byte and word and double word are always in alignment. Do C++ Compiler
    > fill the zero as padding inside byte's field and word's field to avoid
    > misalignment? It looks like that union does not use shift which it loads
    > data from memory. It is like "(H << 8) | L".


    That is not something specified by the language. It depends on the
    compiler, and quite possibly on options that you use when you run the
    compiler.

    > Intel Pentium 4 can degrade the performance if shift is used heavily,
    > but union may be recommended to avoid using shift. It may be slow to store
    > two bytes into memory before load one word from memory containing two bytes.
    > union example is below.
    > Please advise.


    The C++ language knows nothing at all about the Intel Pentium 4 or any
    other particular processor. How well a particular compiler optimizes
    the object code for any particular processor is a QOI (Quality Of
    Optimization) issue. The language standard only specifies what the
    observable output of a strictly conforming program must be. It does
    not specify how fast the output must be produced or how small the
    executable image must be.

    > union var
    > {
    > struct
    > {
    > unsigned __int8 L;
    > unsigned __int8 H;
    > };
    > struct
    > {
    > unsigned __int16 W;
    > };
    > };
    >
    > Bryan Parkoff


    I have heard anecdotally that Intel's compilers, for example, generate
    more optimized code for Intel processors than Microsoft's compilers
    do. I have no knowledge whether this is correct.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Sep 16, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marco Ippolito
    Replies:
    2
    Views:
    3,239
    Marco Ippolito
    May 19, 2004
  2. goli

    SQL UNION query in Java

    goli, Apr 4, 2005, in forum: Java
    Replies:
    4
    Views:
    2,692
  3. =?ISO-8859-1?Q?Thomas_G=E4rtner?=

    JNI working with jvalue (union)

    =?ISO-8859-1?Q?Thomas_G=E4rtner?=, Sep 19, 2005, in forum: Java
    Replies:
    3
    Views:
    3,475
    Roedy Green
    Sep 20, 2005
  4. Matt Garman
    Replies:
    1
    Views:
    691
    Matt Garman
    Apr 25, 2004
  5. Peter Dunker

    union in struct without union name

    Peter Dunker, Apr 26, 2004, in forum: C Programming
    Replies:
    2
    Views:
    924
    Chris Torek
    Apr 26, 2004
Loading...

Share This Page