Re: Struct Alignment

Discussion in 'C Programming' started by Eric G., Aug 3, 2003.

  1. Eric G.

    Eric G. Guest

    Andre <> wrote:

    > Why does gcc (or any C compiler for that matter) align structs on double
    > word boundaries? And why should we align variables inside structs? One
    > clear advantage could be cache utilization but isn't that possible even
    > without alignment? Thanks


    As I understand it, it is for efficiency purposes. When things are
    aligned perfectly, the computer executing the code does far less work
    when moving things around.

    This used to be far more important, but computers a dreadfully fast
    these days and such a technique would likely only truly be useful in
    extreme computationally intensive applications...like 3D rendering,
    etc..


    --
    == Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
    "Therefore the considerations of the intelligent always include both
    benefit and harm." - Sun Tzu
    == Insults, like violence, are the last refuge of the incompetent... ===
    Eric G., Aug 3, 2003
    #1
    1. Advertising

  2. Eric G.

    Malcolm Guest

    "Eric G." <> wrote in message
    >
    > This used to be far more important, but computers a dreadfully fast
    > these days and such a technique would likely only truly be useful in
    > extreme computationally intensive applications...like 3D rendering,
    > etc.
    >

    That's only partly true. There are many programs which execute a trivial bit
    of arithmetic and speed doesn't matter. However if you have constant
    attrition due to no floating-point values being aligned, then you are
    effectively reducing (halving maybe) the speed of the whole computer. This
    is serious.
    And, of course, the compiler has no way of knowing whether the code it is
    compiling is speed critical, like a 3d video game, or something where speed
    is unimportant, like calculating interest on mortgage repayments.
    Malcolm, Aug 3, 2003
    #2
    1. Advertising

  3. Malcolm wrote:

    <snip>

    > And, of course, the compiler has no way of knowing whether the code it is
    > compiling is speed critical, like a 3d video game, or something where
    > speed is unimportant, like calculating interest on mortgage repayments.


    Having worked on several systems in which mortgage interest repayments are
    calculated, I can assure you that speed is quite important in that area.
    The process is simple at its core, of course, but the fiddly bits that
    arise from legislation and indeed from "competitive edge" can be quite
    time-consuming.

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
    Richard Heathfield, Aug 4, 2003
    #3
    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. Bryan Parkoff
    Replies:
    0
    Views:
    415
    Bryan Parkoff
    Apr 2, 2005
  2. Jack Klein

    Re: Struct Alignment

    Jack Klein, Aug 4, 2003, in forum: C Programming
    Replies:
    0
    Views:
    393
    Jack Klein
    Aug 4, 2003
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,354
    Chris Fogelklou
    Apr 20, 2004
  4. S.Tobias

    struct and union alignment

    S.Tobias, Sep 23, 2004, in forum: C Programming
    Replies:
    67
    Views:
    1,342
    Dave Thompson
    Oct 4, 2004
  5. struct alignment

    , May 5, 2005, in forum: C Programming
    Replies:
    2
    Views:
    309
    Tim Prince
    May 5, 2005
Loading...

Share This Page