Are struct members are also padded?

T

Tech Id

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).
 
I

ImpalerCore

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.
 
D

Denis McMahon

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
 
E

Eric Sosman

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.
 
B

Barry Schwarz

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.
 
W

Willem

Barry Schwarz wrote:
) On Sat, 24 Jul 2010 10:28:18 -0700 (PDT), "christian.bau"
)
)>
)>> 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
 
B

Barry Schwarz

Barry Schwarz wrote:
) On Sat, 24 Jul 2010 10:28:18 -0700 (PDT), "christian.bau"
)
)>
)>> 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.
 
S

Stephen Sprunk

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
 
W

Willem

Barry Schwarz wrote:
) On Sun, 25 Jul 2010 08:04:04 +0000 (UTC), Willem
)
)>Barry Schwarz wrote:
)>) On Sat, 24 Jul 2010 10:28:18 -0700 (PDT), "christian.bau"
)>)
)>)>
)>)>> 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
 
T

Tim Rentsch

Kenneth Brody said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top