Why #pragma pack not take effect?

Discussion in 'C Programming' started by Jimmy, Jul 3, 2007.

  1. Jimmy

    Jimmy Guest

    Struct definition as following(on 32-bit Linux):

    #pragma pack(push, 8)
    struct MY_STRUCT
    {
    char a[2];
    short b;
    short c;
    short d;
    int e;
    long long x;
    long long y;
    };
    #pragma(pop)

    During the test, result of 'sizeof(struct MY_STRUCT)' is 28. Why not 32?
    As I had expected, a,b,c,d will be packed into one 8-byte, e one and x, y
    two. Ain't I right?

    If I get wrong usage of #pragma pack(), could anyone please tell me how to
    get it work correctly?

    BTW, what on earth is the difference between __attribute__ align() and
    #pragma pack()?

    Thanks in advance!
     
    Jimmy, Jul 3, 2007
    #1
    1. Advertising

  2. "Jimmy" <> writes:
    > Struct definition as following(on 32-bit Linux):
    >
    > #pragma pack(push, 8)
    > struct MY_STRUCT
    > {
    > char a[2];
    > short b;
    > short c;
    > short d;
    > int e;
    > long long x;
    > long long y;
    > };
    > #pragma(pop)
    >
    > During the test, result of 'sizeof(struct MY_STRUCT)' is 28. Why not 32?
    > As I had expected, a,b,c,d will be packed into one 8-byte, e one and x, y
    > two. Ain't I right?
    >
    > If I get wrong usage of #pragma pack(), could anyone please tell me how to
    > get it work correctly?
    >
    > BTW, what on earth is the difference between __attribute__ align() and
    > #pragma pack()?


    Neither __attribute__ align() nor #pragma pack is defined by the C
    standard. You'll need to check your compiler's documention, or ask in
    a forum where it's discussed.

    Incidentally, I would have assumed that "#pragma pack(push, 8)" would
    be paired with "#pragma pack(pop)", not "#pragma(pop)". If my guess
    is correct (and it's only a guess), then what you've posted isn't your
    real code. If you're going to post code, *always* copy-and-paste the
    exact code.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jul 3, 2007
    #2
    1. Advertising

  3. Jimmy

    Army1987 Guest

    "Jimmy" <> ha scritto nel messaggio news:f6d0gg$uje$99.com...
    > Struct definition as following(on 32-bit Linux):
    >
    > #pragma pack(push, 8)
    > struct MY_STRUCT
    > {
    > char a[2];
    > short b;
    > short c;
    > short d;
    > int e;
    > long long x;
    > long long y;
    > };
    > #pragma(pop)
    >
    > During the test, result of 'sizeof(struct MY_STRUCT)' is 28. Why not 32?
    > As I had expected, a,b,c,d will be packed into one 8-byte, e one and x, y
    > two. Ain't I right?
    >
    > If I get wrong usage of #pragma pack(), could anyone please tell me how to
    > get it work correctly?


    Not here. It's implementation defined. Check the documentation of
    your compiler.

    > BTW, what on earth is the difference between __attribute__ align() and
    > #pragma pack()?
    >
    > Thanks in advance!
    >
    >
     
    Army1987, Jul 3, 2007
    #3
  4. Jimmy

    Guest

    On Jul 3, 3:14 pm, "Jimmy" <> wrote:
    > Struct definition as following(on 32-bit Linux):
    > During the test, result of 'sizeof(struct MY_STRUCT)' is 28. Why not 32?
    > As I had expected, a,b,c,d will be packed into one 8-byte, e one and x, y
    > two. Ain't I right?


    This is OT here but anyway... assuming this is gcc then 28 bytes is
    correct, I don't know why you'd expect otherwise. Note that for x86
    gcc usually defines:
    char: 1 byte
    short: 2 bytes
    long: 4 bytes
    int: 4 bytes
    long long: 8 bytes

    Therefore, your struct is:

    struct MY_STRUCT
    {
    char a[2]; /* 2 */
    short b; /* 2 */
    short c; /* 2 */
    short d; /* 2 */
    int e; /* 4 */
    long long x; /* 8 */
    long long y; /* 8 */
    /* total: 28 */
    };

    YMMV since gcc can be modified and recompiled to have different sizes
    for variables. But for the sake of being able to correctly compile the
    Linux TCP/IP stack this is the most common configuration on most 32bit
    (and even most newer 64bit) machines.
     
    , Jul 3, 2007
    #4
  5. Jimmy

    CBFalconer Guest

    Jimmy wrote:
    >

    .... snip ...
    >
    > BTW, what on earth is the difference between __attribute__ align()
    > and #pragma pack()?


    None. Neither exist in standard C. Find a suitable newsgroup.

    --
    <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
    <http://www.securityfocus.com/columnists/423>
    <http://www.aaxnet.com/editor/edit043.html>
    cbfalconer at maineline dot net



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Jul 3, 2007
    #5
  6. Kenny McCormack, Jul 4, 2007
    #6
    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,721
    Pete Becker
    Dec 9, 2003
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,213
    Smokey Grindel
    Dec 2, 2006
  3. Boltar

    Question about using #pragma pack

    Boltar, Mar 20, 2008, in forum: C Programming
    Replies:
    7
    Views:
    417
    Keith Thompson
    Mar 20, 2008
  4. Steven Woody
    Replies:
    9
    Views:
    601
    Steven Woody
    Jul 13, 2008
  5. Fedor Rodikov
    Replies:
    6
    Views:
    693
    James Kanze
    Sep 2, 2011
Loading...

Share This Page