Are struct members are also padded?

Discussion in 'C Programming' started by Tech ID, Jun 12, 2011.

  1. Tech ID

    Tech ID Guest

    struct pad1 {
    char c1;

    };

    struct pad2 {
    char c1;
    int x;

    };

    struct pad3 {
    char c1;
    char c2;
    int x;

    };

    struct pad4 {
    char c1;
    char c2;
    int x;
    struct pad1 {
    char c1;
    };

    };

    What will be the sizeof for the structs above? (for 32-bit and 64-bit
    machines).
    Tech ID, Jun 12, 2011
    #1
    1. Advertising

  2. Tech ID

    Ian Collins Guest

    On 06/13/11 09:09 AM, Tech ID wrote:
    > struct pad1 {
    > char c1;
    >
    > };
    >
    > struct pad2 {
    > char c1;
    > int x;
    >
    > };
    >
    > struct pad3 {
    > char c1;
    > char c2;
    > int x;
    >
    > };
    >
    > struct pad4 {
    > char c1;
    > char c2;
    > int x;
    > struct pad1 {
    > char c1;
    > };
    >
    > };
    >
    > What will be the sizeof for the structs above? (for 32-bit and 64-bit
    > machines).


    That depends on the alignment rules and sizeof(int) for that machine.
    There isn't a universal 32 or 64 bit machine.

    --
    Ian Collins
    Ian Collins, Jun 12, 2011
    #2
    1. Advertising

  3. Tech ID

    Shao Miller Guest

    On 6/12/2011 4:09 PM, Tech ID wrote:
    > struct pad1 {
    > char c1;
    >
    > };
    >
    > struct pad2 {
    > char c1;
    > int x;
    >
    > };
    >
    > struct pad3 {
    > char c1;
    > char c2;
    > int x;
    >
    > };
    >
    > struct pad4 {
    > char c1;
    > char c2;
    > int x;
    > struct pad1 {
    > char c1;
    > };
    >
    > };
    >
    > What will be the sizeof for the structs above? (for 32-bit and 64-bit
    > machines).


    The sizes are implementation-defined. The answer to the subject-line's
    question is implementation-defined.
    Shao Miller, Jun 12, 2011
    #3
  4. Tech ID

    Angel Guest

    On 2011-06-12, Tech ID <> wrote:
    > struct pad1 {
    > char c1;
    >
    > };
    >
    > struct pad2 {
    > char c1;
    > int x;
    >
    > };
    >
    > struct pad3 {
    > char c1;
    > char c2;
    > int x;
    >
    > };
    >
    > struct pad4 {
    > char c1;
    > char c2;
    > int x;
    > struct pad1 {
    > char c1;
    > };
    >
    > };
    >
    > What will be the sizeof for the structs above? (for 32-bit and 64-bit
    > machines).


    As given, the definition of struct pad4 is incorrect, as it redefines
    struct pad1. With that corrected, the sizes are as follows on the
    systems I tested on:

    x86_64, 64-bit Linux:
    1, 8, 8, 12

    sparc64, 64-bit Linux with 32-bit user land:
    1, 8, 8, 12


    As others pointed out though, there is no standard for this and the
    sizes will likely vary on different hardware and operating systems.


    --
    "C provides a programmer with more than enough rope to hang himself.
    C++ provides a firing squad, blindfold and last cigarette."
    - seen in comp.lang.c
    Angel, Jun 12, 2011
    #4
  5. On 12-Jun-11 16:09, Tech ID wrote:
    > struct pad1 {
    > char c1;
    > };
    >
    > struct pad2 {
    > char c1;
    > int x;
    > };
    >
    > struct pad3 {
    > char c1;
    > char c2;
    > int x;
    > };
    >
    > struct pad4 {
    > char c1;
    > char c2;
    > int x;
    > struct pad1 {
    > char c1;
    > };
    > };
    >
    > What will be the sizeof for the structs above? (for 32-bit and 64-bit
    > machines).


    The sizes will vary from one implementation to another, sometimes even
    on the same machine, so there are no universal answers.

    If you need to worry about this, it would be more effective to address
    the portability problems with your code so that the size is irrelevant,
    rather than try to predict what machines it will run on and figure out
    how to handle each case.

    S

    --
    Stephen Sprunk "God does not play dice." --Albert Einstein
    CCIE #3723 "God is an inveterate gambler, and He throws the
    K5SSS dice at every possible opportunity." --Stephen Hawking
    Stephen Sprunk, Jun 12, 2011
    #5
  6. Tech ID <> writes:
    > struct pad1 {
    > char c1;
    >
    > };
    >
    > struct pad2 {
    > char c1;
    > int x;
    >
    > };
    >
    > struct pad3 {
    > char c1;
    > char c2;
    > int x;
    >
    > };
    >
    > struct pad4 {
    > char c1;
    > char c2;
    > int x;
    > struct pad1 {
    > char c1;
    > };


    This should be something like:

    struct pad4 {
    char c1;
    char c2;
    int x;
    struct pad1 p1;
    };

    > What will be the sizeof for the structs above? (for 32-bit and 64-bit
    > machines).


    It's not clear to me how the question in your subject header and
    the question in the body of your article are related.

    The best (in some sense) answer to your second question (once
    you correct the error in the declaration of struct pad4) is:
    sizeof (struct pad1), sizeof (struct pad2), sizeof (struct pad3),
    and sizeof (struct pad4), respectively. I know that's not the answer
    you were looking for, but in most contexts it's all you need to know.

    As for the first question, "Are struct members ... also padded?",
    what exactly do you mean by that?

    A struct has a size and layout determined by the compiler.
    The compiler may add padding after any member, including the last
    one. This is normally done to satisfy alignment requirements,
    but in theory a compiler could add padding for any reason.

    Your struct pad4 has a member of type struct pad1, which I've
    named p1. p1 has whatever size struct pad1 has, which will include
    any padding after c1. There may or may not be additional padding
    after p1; this padding would be part of struct pad4.

    Does that answer your questions?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 12, 2011
    #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. John Dalberg
    Replies:
    0
    Views:
    460
    John Dalberg
    Jan 21, 2005
  2. Replies:
    0
    Views:
    22,503
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,355
    Chris Fogelklou
    Apr 20, 2004
  4. Tech Id

    Are struct members are also padded?

    Tech Id, Jul 23, 2010, in forum: C Programming
    Replies:
    10
    Views:
    474
    Nick Keighley
    Jul 27, 2010
  5. John Reye
    Replies:
    28
    Views:
    1,341
    Tim Rentsch
    May 8, 2012
Loading...

Share This Page