Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Are struct members are also padded?

Reply
Thread Tools

Are struct members are also padded?

 
 
Tech Id
Guest
Posts: n/a
 
      07-23-2010
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).
 
Reply With Quote
 
 
 
 
ImpalerCore
Guest
Posts: n/a
 
      07-23-2010
On Jul 23, 9:02*am, Kenneth Brody <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Denis McMahon
Guest
Posts: n/a
 
      07-23-2010
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
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      07-24-2010
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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      07-25-2010
On Sat, 24 Jul 2010 10:28:18 -0700 (PDT), "christian.bau"
<(E-Mail Removed)> wrote:

>On Jul 23, 2:02*pm, Kenneth Brody <(E-Mail Removed)> 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
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      07-25-2010
Barry Schwarz wrote:
) On Sat, 24 Jul 2010 10:28:18 -0700 (PDT), "christian.bau"
)<(E-Mail Removed)> wrote:
)
)>On Jul 23, 2:02?pm, Kenneth Brody <(E-Mail Removed)> 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
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      07-26-2010
On Sun, 25 Jul 2010 08:04:04 +0000 (UTC), Willem
<(E-Mail Removed)> wrote:

>Barry Schwarz wrote:
>) On Sat, 24 Jul 2010 10:28:18 -0700 (PDT), "christian.bau"
>)<(E-Mail Removed)> wrote:
>)
>)>On Jul 23, 2:02?pm, Kenneth Brody <(E-Mail Removed)> 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
 
Reply With Quote
 
Stephen Sprunk
Guest
Posts: n/a
 
      07-26-2010
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
>> <(E-Mail Removed)> wrote:
>>
>>> Barry Schwarz wrote:
>>> ) On Sat, 24 Jul 2010 10:28:18 -0700 (PDT), "christian.bau"
>>> )<(E-Mail Removed)> wrote:
>>> )
>>> )>On Jul 23, 2:02?pm, Kenneth Brody<(E-Mail Removed)> 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
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      07-26-2010
Barry Schwarz wrote:
) On Sun, 25 Jul 2010 08:04:04 +0000 (UTC), Willem
)<(E-Mail Removed)> wrote:
)
)>Barry Schwarz wrote:
)>) On Sat, 24 Jul 2010 10:28:18 -0700 (PDT), "christian.bau"
)>)<(E-Mail Removed)> wrote:
)>)
)>)>On Jul 23, 2:02?pm, Kenneth Brody <(E-Mail Removed)> 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
 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      07-26-2010
Kenneth Brody <(E-Mail Removed)> writes:

> On 7/25/2010 10:29 PM, Barry Schwarz wrote:
>> On Sun, 25 Jul 2010 08:04:04 +0000 (UTC), Willem
>> <(E-Mail Removed)> wrote:
>>
>>> Barry Schwarz wrote:
>>> ) On Sat, 24 Jul 2010 10:28:18 -0700 (PDT), "christian.bau"
>>> )<(E-Mail Removed)> wrote:
>>> )
>>> )>On Jul 23, 2:02?pm, Kenneth Brody<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Are struct members are also padded? Tech ID C Programming 5 06-12-2011 09:39 PM
Re: UPDATE: They didn't only pledge allegiance to Mex flag: anchor babies in TX *also* shouted "RAZA! RAZA!" in US school also tedorn44@hotmail.com Digital Photography 9 10-21-2006 11:12 AM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments