Topic: Sizeof in C++ versus sizeof in C (was: Re:


Author: "Ronald F. Guilmette" <rfg@rahul.net>
Date: 24 Dec 1994 09:42:51 GMT
Raw View
In article <3dcn0a$k69@engnews2.Eng.Sun.COM>,
Steve Clamage <clamage@Eng.Sun.COM> wrote:
>In article 8dr@hustle.rahul.net, "Ronald F. Guilmette" <rfg@rahul.net> writes:
>>In article <3d2237$7k1@engnews2.Eng.Sun.COM>,
>>Steve Clamage <clamage@Eng.Sun.COM> wrote:
>>>... The "sizeof" operator returns the amount of space an
>>>object requires when used as an array element, including any
>>>hidden data and padding for alignment...
>>
>>Steve, I take it that you are saying that for your average, run-of-the
>>mill, 32-bit UNIX workstation, if I have:
>>
>> struct S { double d; int i; };
>>
>>that `sizeof(struct S)' will yield 16 rather than 12, yes?
>
>It depends on whether doubles need to be 8-byte aligned. On some systems
>they do, so sizeof(S) will be 16...

I was talking about systems where double need to be 8-byte aligned.

>>I just have one question... is this yet another small incompatability
>>with C?
>
>The rule is exactly the same in Standard C. I'm surprised you don't know that.

Well, I confess that I _was_ a bit ignorant in this case, but that still
doesn't mean that the assertion you have just made is obviously true.

As quoted above, you said that in C++ the sizeof a given struct type and/or
object will include the size of any trailing padding which the given
type/object will be padded with when it is an element of an array.

But the ANSI/ISO C standard doesn't require that, or anything like that.

Rather, it just says that `sizeof' for a struct type/object includes the
space used by any ``internal or trailing padding''... and that last bit
(about the trailing padding) is not nearly so precise (for standard C)
as you have said the rules are in the case of C++.

In other words, I think that I may indeed have been right, and that there
may indeed be a small incompatability between the definitions of C and C++
in this regard, but if _that_ is ture, then the Right Solution is to get
X3J11/WG14 to further clarify the semantics of `sizeof' in C so that
they exactly match the (very sensible and intutive) rule you have said
applies in the case of C++.

--

-- Ron Guilmette, Sunnyvale, CA ---------- RG Consulting -------------------
---- E-mail: rfg@segfault.us.com ----------- Purveyors of Compiler Test ----
-------------------------------------------- Suites and Bullet-Proof Shoes -




Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 22 Dec 1994 20:19:22 GMT
Raw View
In article 8dr@hustle.rahul.net, "Ronald F. Guilmette" <rfg@rahul.net> writes:
>In article <3d2237$7k1@engnews2.Eng.Sun.COM>,
>Steve Clamage <clamage@Eng.Sun.COM> wrote:
>>... The "sizeof" operator returns the amount of space an
>>object requires when used as an array element, including any
>>hidden data and padding for alignment...
>
>Steve, I take it that you are saying that for your average, run-of-the
>mill, 32-bit UNIX workstation, if I have:
>
> struct S { double d; int i; };
>
>that `sizeof(struct S)' will yield 16 rather than 12, yes?

It depends on whether doubles need to be 8-byte aligned. On some systems
they do, so sizeof(S) will be 16. On some systems, 4-byte alignment
is sufficient, so the size will be 12.

>I just have one question... is this yet another small incompatability
>with C?

The rule is exactly the same in Standard C. I'm surprised you don't know that.

---
Steve Clamage, stephen.clamage@eng.sun.com