Are struct members are also padded?

Discussion in 'C Programming' started by Tech Id, Jul 23, 2010.

  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, Jul 23, 2010
    #1
    1. Advertising

  2. Tech Id

    ImpalerCore Guest

    On Jul 23, 9:02 am, Kenneth Brody <> wrote:

    <snip>

    > On my DS-6000, with the default compiler flags, I got 37, 16, 42, and 5,
    > respectively.  But, with a different set of flags, I can produce 1, 2, 3,
    > and 4, respectively.


    You need to upgrade your hardware. I believe the latest version is
    9000.

    > You can find out what your particular implementation, with a particular set
    > of compiler flags, produces, by simply printf'ing the sizeof the structs.
    >
    > --
    > Kenneth Brody
     
    ImpalerCore, Jul 23, 2010
    #2
    1. Advertising

  3. On 23/07/10 13:31, Tech Id wrote:

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


    depends on the compiler, and whether, if the compiler supports it, the
    struct is packed.

    Rgds

    Denis McMahon
     
    Denis McMahon, Jul 23, 2010
    #3
  4. Tech Id

    Eric Sosman Guest

    On 7/23/2010 8:31 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).


    sizeof(struct pad1), sizeof(struct pad2), sizeof(struct pad3),
    and nothing (doesn't compile), respectively. We can also infer that

    sizeof(struct pad1) >= 1
    sizeof(struct pad2) >= 2
    sizeof(struct pad3) >= 3

    .... but that's all the C language itself can give us.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jul 24, 2010
    #4
  5. On Sat, 24 Jul 2010 10:28:18 -0700 (PDT), "christian.bau"
    <> wrote:

    >On Jul 23, 2:02 pm, Kenneth Brody <> wrote:
    >
    >> On my DS-6000, with the default compiler flags, I got 37, 16, 42, and 5,
    >> respectively.  But, with a different set of flags, I can produce 1, 2, 3,
    >> and 4, respectively.

    >
    >Your compiler with default compiler flags is not a conforming C
    >implementation. Answer four must be at least 40.


    What pray tell are you smoking? There is no requirement for any of
    the members of the struct in question to have a size greater than 1.
    With only four members in the struct, four is a perfectly good size.

    --
    Remove del for email
     
    Barry Schwarz, Jul 25, 2010
    #5
  6. Tech Id

    Willem Guest

    Barry Schwarz wrote:
    ) On Sat, 24 Jul 2010 10:28:18 -0700 (PDT), "christian.bau"
    )<> wrote:
    )
    )>On Jul 23, 2:02?pm, Kenneth Brody <> wrote:
    )>
    )>> On my DS-6000, with the default compiler flags, I got 37, 16, 42, and 5,
    )>> respectively. ?But, with a different set of flags, I can produce 1, 2, 3,
    )>> and 4, respectively.
    )>
    )>Your compiler with default compiler flags is not a conforming C
    )>implementation. Answer four must be at least 40.
    )
    ) What pray tell are you smoking? There is no requirement for any of
    ) the members of the struct in question to have a size greater than 1.
    ) With only four members in the struct, four is a perfectly good size.

    Struct 4 contains struct 1. Struct 1 is of size 37. 37+3=40.
    I think that's what he was getting at.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Jul 25, 2010
    #6
  7. On Sun, 25 Jul 2010 08:04:04 +0000 (UTC), Willem
    <> wrote:

    >Barry Schwarz wrote:
    >) On Sat, 24 Jul 2010 10:28:18 -0700 (PDT), "christian.bau"
    >)<> wrote:
    >)
    >)>On Jul 23, 2:02?pm, Kenneth Brody <> wrote:
    >)>
    >)>> On my DS-6000, with the default compiler flags, I got 37, 16, 42, and 5,
    >)>> respectively. ?But, with a different set of flags, I can produce 1, 2, 3,
    >)>> and 4, respectively.
    >)>
    >)>Your compiler with default compiler flags is not a conforming C
    >)>implementation. Answer four must be at least 40.
    >)
    >) What pray tell are you smoking? There is no requirement for any of
    >) the members of the struct in question to have a size greater than 1.
    >) With only four members in the struct, four is a perfectly good size.
    >
    >Struct 4 contains struct 1. Struct 1 is of size 37. 37+3=40.
    >I think that's what he was getting at.


    Under the specified conditions ("with a different set of flags"),
    struct 1 has a size of 1 as indicated four words after the text I just
    quoted.

    --
    Remove del for email
     
    Barry Schwarz, Jul 26, 2010
    #7
  8. On 26 Jul 2010 08:37, Kenneth Brody wrote:
    > On 7/25/2010 10:29 PM, Barry Schwarz wrote:
    >> On Sun, 25 Jul 2010 08:04:04 +0000 (UTC), Willem
    >> <> wrote:
    >>
    >>> Barry Schwarz wrote:
    >>> ) On Sat, 24 Jul 2010 10:28:18 -0700 (PDT), "christian.bau"
    >>> )<> wrote:
    >>> )
    >>> )>On Jul 23, 2:02?pm, Kenneth Brody<> wrote:
    >>> )>
    >>> )>> On my DS-6000, with the default compiler flags, I got 37, 16,
    >>> 42, and 5,
    >>> )>> respectively. ?But, with a different set of flags, I can produce
    >>> 1, 2, 3,
    >>> )>> and 4, respectively.
    >>> )>
    >>> )>Your compiler with default compiler flags is not a conforming C
    >>> )>implementation. Answer four must be at least 40.
    >>> )
    >>> ) What pray tell are you smoking? There is no requirement for any of
    >>> ) the members of the struct in question to have a size greater than 1.
    >>> ) With only four members in the struct, four is a perfectly good size.
    >>>
    >>> Struct 4 contains struct 1. Struct 1 is of size 37. 37+3=40.
    >>> I think that's what he was getting at.

    >>
    >> Under the specified conditions ("with a different set of flags"),
    >> struct 1 has a size of 1 as indicated four words after the text I just
    >> quoted.

    >
    > But, he was talking about "with default compiler flags", where the
    > numbers are 37 and 5, respectively.
    >
    > I guess I'll have to submit a bug report, though I understand the
    > DS-6000 is no longer supported.
    >
    >> struct pad1 {
    >> char c1;
    >> };

    > [...]
    >> struct pad4 {
    >> char c1;
    >> char c2;
    >> int x;
    >> struct pad1 {
    >> char c1;
    >> };
    >> };

    >
    >
    > Or, can the struct within pad4 be distinct from the "other" struct pad1,
    > and therefore have different padding?


    I thought all structs with the same members must have the same size and
    padding (for a given implementation, of course). If so, this is
    definitely a bug in the DS6k compiler.

    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, Jul 26, 2010
    #8
  9. Tech Id

    Willem Guest

    Barry Schwarz wrote:
    ) On Sun, 25 Jul 2010 08:04:04 +0000 (UTC), Willem
    )<> wrote:
    )
    )>Barry Schwarz wrote:
    )>) On Sat, 24 Jul 2010 10:28:18 -0700 (PDT), "christian.bau"
    )>)<> wrote:
    )>)
    )>)>On Jul 23, 2:02?pm, Kenneth Brody <> wrote:
    )>)>
    )>)>> On my DS-6000, with the default compiler flags, I got 37, 16, 42, and 5,
    )>)>> respectively. ?But, with a different set of flags, I can produce 1, 2, 3,
    )>)>> and 4, respectively.
    )>)>
    )>)>Your compiler with default compiler flags is not a conforming C
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )>)>implementation. Answer four must be at least 40.
    )>)
    )>) What pray tell are you smoking? There is no requirement for any of
    )>) the members of the struct in question to have a size greater than 1.
    )>) With only four members in the struct, four is a perfectly good size.
    )>
    )>Struct 4 contains struct 1. Struct 1 is of size 37. 37+3=40.
    )>I think that's what he was getting at.
    )
    ) Under the specified conditions ("with a different set of flags"),
    ) struct 1 has a size of 1 as indicated four words after the text I just
    ) quoted.

    The specified conditions, which I underlined for your convenience,
    are "with default compiler flags".


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Jul 26, 2010
    #9
  10. Tech Id

    Tim Rentsch Guest

    Kenneth Brody <> writes:

    > On 7/25/2010 10:29 PM, Barry Schwarz wrote:
    >> On Sun, 25 Jul 2010 08:04:04 +0000 (UTC), Willem
    >> <> wrote:
    >>
    >>> Barry Schwarz wrote:
    >>> ) On Sat, 24 Jul 2010 10:28:18 -0700 (PDT), "christian.bau"
    >>> )<> wrote:
    >>> )
    >>> )>On Jul 23, 2:02?pm, Kenneth Brody<> wrote:
    >>> )>
    >>> )>> On my DS-6000, with the default compiler flags, I got 37, 16, 42, and 5,
    >>> )>> respectively. ?But, with a different set of flags, I can produce 1, 2, 3,
    >>> )>> and 4, respectively.
    >>> )>
    >>> )>Your compiler with default compiler flags is not a conforming C
    >>> )>implementation. Answer four must be at least 40.
    >>> )
    >>> ) What pray tell are you smoking? There is no requirement for any of
    >>> ) the members of the struct in question to have a size greater than 1.
    >>> ) With only four members in the struct, four is a perfectly good size.
    >>>
    >>> Struct 4 contains struct 1. Struct 1 is of size 37. 37+3=40.
    >>> I think that's what he was getting at.

    >>
    >> Under the specified conditions ("with a different set of flags"),
    >> struct 1 has a size of 1 as indicated four words after the text I just
    >> quoted.

    >
    > But, he was talking about "with default compiler flags", where the
    > numbers are 37 and 5, respectively.
    >
    > I guess I'll have to submit a bug report, though I understand the
    > DS-6000 is no longer supported.
    >
    >> struct pad1 {
    >> char c1;
    >> };

    > [...]
    >> struct pad4 {
    >> char c1;
    >> char c2;
    >> int x;
    >> struct pad1 {
    >> char c1;
    >> };
    >> };

    >
    >
    > Or, can the struct within pad4 be distinct from the "other" struct
    > pad1, and therefore have different padding?


    As far as I know there is no explicit requirement that they be
    the same. Type compatibility rules effectively rule out the
    possibility that they could be different (since both must be
    compatible with a 'struct pad1' type declared in another
    translation unit), so at the very least it would be extremely
    surprising to see an actual implementation where they were
    different. It seems worth a bug report, even if through some
    twisted logic the Standard allows (and I'm not sure it does)
    the possibility that different amounts of padding is conforming.
     
    Tim Rentsch, Jul 26, 2010
    #10
  11. On 23 July, 13:31, Tech Id <> wrote:

    no
     
    Nick Keighley, Jul 27, 2010
    #11
    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:
    472
    John Dalberg
    Jan 21, 2005
  2. Replies:
    0
    Views:
    22,541
  3. Chris Fogelklou
    Replies:
    36
    Views:
    1,393
    Chris Fogelklou
    Apr 20, 2004
  4. Tech ID

    Are struct members are also padded?

    Tech ID, Jun 12, 2011, in forum: C Programming
    Replies:
    5
    Views:
    280
    Keith Thompson
    Jun 12, 2011
  5. John Reye
    Replies:
    28
    Views:
    1,377
    Tim Rentsch
    May 8, 2012
Loading...

Share This Page