sizeof() , different results for same thing in C and C++. Why?

P

Pete Becker

That is not the reason: just as with char, there is no reason to
require the alignment of an empty class to be a specific multiple of
anything but one.
My guess is that the requirement is there to allow compilers to always
have objects of class-type have a "convenient" size. I must admit,
that I can't see the reason for that but then I'm not a compiler
writer. All compilers I know have size of 1 for empty classes and
structs.

It's not a compiler convenience. If a class has 0 size, then elements
of an array of that type all have the same address. That breaks the
fundamental rule that distinct objects of the same type have different
addresses.
 
J

James Kanze

That is not the reason: just as with char, there is no reason
to require the alignment of an empty class to be a specific
multiple of anything but one.

That *IS* the reason. Because you can have pointers to
incomplete classes, all class pointers must have the same size
and layout. On word addressed architectures, it is usual for
this to be a word pointer---byte pointers are typically more
expensive, and most of the time, not necessary. And in order to
address something with a word pointer, it must be word aligned.
My guess is that the requirement is there to allow compilers
to always have objects of class-type have a "convenient" size.
I must admit, that I can't see the reason for that but then
I'm not a compiler writer. All compilers I know have size of 1
for empty classes and structs.

Most modern architectures are byte addressed, and 1 is the most
reasonable size for a byte addressed architecture. On a PDP-10
or a Unisys 2200, however, I would expect 4, and on the older
Burroughs/Unisys series A, 6 (48 bit words).
 
J

James Kanze

Rubbish. Of course it involkes the C++ compiler,

That's not what the GCC documentation says. The GCC
documentation says that if you want code conformant to the
standard, you have to specify which standard, and if you want to
be sure that non-conformant code is flaged, you need to add
-pedantic. Otherwise, you're dealing with g++ specific
extensions, which are not C++.
what else is it invoking , a pascal compiler?? If I replace
struct with class it gives the same result so its not calling
the C compiler either. The fact that it won't compile if you
force it into pedantic mode is irrelevant. With a standard
invocation it will compile.

The "standard" invocation is with -std=c++98 -pedantic. At
least according to the GCC documentation.
 
J

James Kanze

It invokes a compiler that implements extensions to the C++
language as permitted by the standard [1.4/8]. A compiler is
allowed to compile invalid code as per extensions although it
has to issue a diagnostic (which could be a warning).

Strictly speaking, it invokes a compiler which implements a
language close to, but not quite the same as, C++. Call it
C++/GCC, since there's a precedent for naming a new language
based on the old by appending a / and some characters. The GCC
documentation is quite clear: if you want conformant code to
compile as per the standard, you need either -ansi, or better
yet, you need to specify which standard (-std=c++98). If you
want all of the diagnostics required by the standard, you need
-pedantic as well.
 
E

Erik Wikström

Juha said:
#pragma pack(push, 1)
/* [...] */
#pragma pack(pop)

If you compile it using VC++ you will see the size 5

If you do that, expect a decrease in performance (because in most x86
architectures accessing an unaligned int will cause clock cycle penalties).

I think that was clear to Sandeep and it was just to demonstrate that
the sizeof(Test) only yields 8 due to the integer alignment :)

Which I pointed out in together with the example code...
 
P

peter koch

That *IS* the reason.  Because you can have pointers to
incomplete classes, all class pointers must have the same size
and layout.  On word addressed architectures, it is usual for
this to be a word pointer---byte pointers are typically more
expensive, and most of the time, not necessary.  And in order to
address something with a word pointer, it must be word aligned.
[snip]

Of course! Actually, when rereading your original answer, I don't
understand why I did not understand it the first time. Requiring the
size to be one would require that class* be same size of char* (which
on these weird architectures likely would double the size).
This is a heavy prize to pay in order to optimize empty classes!

/Peter
 
J

Jerry Coffin

[ ... ]
Well it seems in C this doesn't apply , since sizeof() returns zero
for an empty struct.

In C: "If the struct-declaration-list contains no named members, the
behavior is undefined." ($6.7.2.1/7)
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top