Question about using #pragma pack

Discussion in 'C Programming' started by Boltar, Mar 20, 2008.

  1. Boltar

    Boltar Guest

    If I use #pragma pack to byte align structures to non word size , will
    this cause the program to run slightly slower if it is accessing and
    setting non word aligned integers etc in the struct? Is there any
    realignment going on silently underneath when the var is read and
    written to memory?

    B2003
    Boltar, Mar 20, 2008
    #1
    1. Advertising

  2. Boltar

    jacob navia Guest

    Boltar wrote:
    > If I use #pragma pack to byte align structures to non word size , will
    > this cause the program to run slightly slower if it is accessing and
    > setting non word aligned integers etc in the struct?


    yes

    Is there any
    > realignment going on silently underneath when the var is read and
    > written to memory?
    >


    no


    > B2003



    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
    jacob navia, Mar 20, 2008
    #2
    1. Advertising

  3. In article <>,
    Boltar <> wrote:

    >If I use #pragma pack to byte align structures to non word size , will
    >this cause the program to run slightly slower if it is accessing and
    >setting non word aligned integers etc in the struct?


    Yes probably. Otherwise why would they bother to align them in the
    usual case?

    -- Richard
    --
    :wq
    Richard Tobin, Mar 20, 2008
    #3
  4. Boltar

    Boltar Guest

    On Mar 20, 11:39 am, jacob navia <> wrote:
    > Boltar wrote:
    > > If I use #pragma pack to byte align structures to non word size , will
    > > this cause the program to run slightly slower if it is accessing and
    > > setting non word aligned integers etc in the struct?

    >
    > yes
    >
    > Is there any
    >
    > > realignment going on silently underneath when the var is read and
    > > written to memory?

    >
    > no


    I presume it must use 2 seperate get/put operations for a variable
    crossing a word boundary then?

    B2003
    Boltar, Mar 20, 2008
    #4
  5. Boltar

    jacob navia Guest

    Boltar wrote:
    > On Mar 20, 11:39 am, jacob navia <> wrote:
    >> Boltar wrote:
    >>> If I use #pragma pack to byte align structures to non word size , will
    >>> this cause the program to run slightly slower if it is accessing and
    >>> setting non word aligned integers etc in the struct?

    >> yes
    >>
    >> Is there any
    >>
    >>> realignment going on silently underneath when the var is read and
    >>> written to memory?

    >> no

    >
    > I presume it must use 2 seperate get/put operations for a variable
    > crossing a word boundary then?
    >
    > B2003
    >


    This depends on the processor
    In some cases the alignment fault will be immediately trapped by the
    processor that then issues two reads, or it could be that it is trapped
    by the operating system, what would be MUCH MORE costly.

    At each access you would pay the cost of an interupt servicing, and then
    OS support.

    Conclusion:

    Do not use anything and leave the stuff to the compiler.


    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
    jacob navia, Mar 20, 2008
    #5
  6. In article <>,
    Boltar <> wrote:
    >I presume it must use 2 seperate get/put operations for a variable
    >crossing a word boundary then?


    It may be that the hardware or microcode does this for you (it does on
    x86 for example), but it's still likely to be slower.

    It might be that within a cache line it would be possible to do
    unaligned reads and writes with no overhead, but I don't think that's
    the case. Try comp.arch which has people who really understand this.

    -- Richard
    --
    :wq
    Richard Tobin, Mar 20, 2008
    #6
  7. Boltar wrote:
    >
    > On Mar 20, 11:39 am, jacob navia <> wrote:
    > > Boltar wrote:
    > > > If I use #pragma pack to byte align structures to non word size , will
    > > > this cause the program to run slightly slower if it is accessing and
    > > > setting non word aligned integers etc in the struct?

    > >
    > > yes
    > >
    > > Is there any
    > >
    > > > realignment going on silently underneath when the var is read and
    > > > written to memory?

    > >
    > > no

    >
    > I presume it must use 2 seperate get/put operations for a variable
    > crossing a word boundary then?


    "Best case scenario", probably.

    However, there are other possibilities. Some hardware simply doesn't
    allow such access, and causes an interrupt. On some platforms, this
    will cause the program to crash. On others, the O/S will emulate the
    unaligned access ability in the interrupt handler. For example, on
    an unaligned read, it will read the two aligned chunks, pull out the
    bits that you wanted, put then in the destination, and return from
    the interrupt handler. (Hardly a simple "2 bus reads" degradation
    in efficiency.)

    You may want to look back at why you feel you need to pack the
    structs.

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Mar 20, 2008
    #7
  8. Boltar <> writes:
    > If I use #pragma pack to byte align structures to non word size , will
    > this cause the program to run slightly slower if it is accessing and
    > setting non word aligned integers etc in the struct? Is there any
    > realignment going on silently underneath when the var is read and
    > written to memory?


    "#pragma pack" is a non-standard extension. If you want to know how
    it works, you'll need to consult the documentation for your
    implementation; different compilers might implement it differently, or
    not at all.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 20, 2008
    #8
    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. JustSomeGuy

    how does pragma pack work?

    JustSomeGuy, Dec 9, 2003, in forum: C++
    Replies:
    1
    Views:
    3,658
    Pete Becker
    Dec 9, 2003
  2. Tim Jones
    Replies:
    0
    Views:
    366
    Tim Jones
    Jan 31, 2004
  3. Jimmy

    Why #pragma pack not take effect?

    Jimmy, Jul 3, 2007, in forum: C Programming
    Replies:
    5
    Views:
    759
    Kenny McCormack
    Jul 4, 2007
  4. Steven Woody
    Replies:
    9
    Views:
    578
    Steven Woody
    Jul 13, 2008
  5. Fedor Rodikov
    Replies:
    6
    Views:
    665
    James Kanze
    Sep 2, 2011
Loading...

Share This Page