sizeof() , different results for same thing in C and C++. Why? | | |
Hi
Why - using gcc on linux - does this return 0 in C but returns 1 in C+
+? I don't get it.
#include <stdio.h>
struct foo
{
};
main()
{
printf("%d\n",sizeof(struct foo));
}
B2003 | | | | re: sizeof() , different results for same thing in C and C++. Why?
Boltar <boltar2003@yahoo.co.ukwrote in comp.lang.c++: Quote:
Why - using gcc on linux - does this return 0 in C but returns 1 in C+
+? I don't get it.
>
#include <stdio.h>
>
struct foo
{
};
>
>
main()
{
printf("%d\n",sizeof(struct foo));
}
Change:
sizeof(struct foo)
to:
(int)sizeof(struct foo)
(You shouldn't supply printf with a size_t if you're using %d)
If you get the same output then your C compiler is faulty.
In all the standards of C and C++, the value returned by sizeof will
always be non-zero.
I'd expect the size of that struct to be 1, but of course it could be 13
or 9 or 27 or 2.
--
Tomás Ó hÉilidhe | | | | re: sizeof() , different results for same thing in C and C++. Why?
On 10 Jan, 17:23, "Tomás Ó hÉilidhe" <t...@lavabit.comwrote: Quote:
Boltar <boltar2...@yahoo.co.ukwrote in comp.lang.c++:
> Quote:
Why - using gcc on linux - does this return 0 in C but returns 1 in C+
+? I don't get it.
> Quote:
#include <stdio.h>
> > Quote:
main()
{
printf("%d\n",sizeof(struct foo));
}
>
Change:
sizeof(struct foo)
to:
(int)sizeof(struct foo)
>
(You shouldn't supply printf with a size_t if you're using %d)
Makes no difference. Quote:
>
If you get the same output then your C compiler is faulty.
Its gcc 4.1.0. Perhaps it is a bug but it seems a pretty fundamental
one. Quote:
>
In all the standards of C and C++, the value returned by sizeof will
always be non-zero.
Why would it be non zero if theres nothing in it? Surely the whole
point of sizeof is to return the size in bytes of the members inside?
If there are no members it should be zero bytes surely? 1 makes no
sense to me.
B2003 | | | | re: sizeof() , different results for same thing in C and C++. Why?
Boltar <boltar2003@yahoo.co.ukwrote in news:c76081d7-da52-4633-a060- add2625445e6@z17g2000hsg.googlegroups.com: Quote:
Hi
>
Why - using gcc on linux - does this return 0 in C but returns 1 in C+
+? I don't get it.
>
#include <stdio.h>
>
struct foo
{
};
>
>
main()
{
printf("%d\n",sizeof(struct foo));
}
>
>
>
B2003
>
>
Do you have RTTI turned on? That could account for it.
joe | | | | re: sizeof() , different results for same thing in C and C++. Why?
Boltar wrote: Quote: Quote: Quote:
>>Why - using gcc on linux - does this return 0 in C but returns 1 in C+
>>+? I don't get it.
>>#include <stdio.h>
>>struct foo
>>{
>>};
>>main()
>>{
>> printf("%d\n",sizeof(struct foo));
>>}
>[...]
>
Why would it be non zero if theres nothing in it? Surely the whole
point of sizeof is to return the size in bytes of the members inside?
If there are no members it should be zero bytes surely? 1 makes no
sense to me.
Someone in this group will surely give you a more sophisticated answer,
but if I recall correctly, it has to do with the fact that each object
must be individually addressable in memory, even if it's an
instantiation of an empty class or struct.
--
Christian Hackl | | | | re: sizeof() , different results for same thing in C and C++. Why?
On 10 Jan, 17:47, Joe Greer <jgr...@doubletake.comwrote: Quote:
Boltar <boltar2...@yahoo.co.ukwrote in news:c76081d7-da52-4633-a060-
add262544...@z17g2000hsg.googlegroups.com:
>
>
> > Quote:
Why - using gcc on linux - does this return 0 in C but returns 1 in C+
+? I don't get it.
> Quote:
#include <stdio.h>
> > Quote:
main()
{
printf("%d\n",sizeof(struct foo));
}
> >
Do you have RTTI turned on? That could account for it.
>
joe
No that I know if - just using "cc t.c" and "c++ t.c" to compile
B2003 | | | | re: sizeof() , different results for same thing in C and C++. Why?
On 2008-01-10 18:27, Boltar wrote: Quote:
On 10 Jan, 17:23, "Tomás Ó hÉilidhe" <t...@lavabit.comwrote: Quote:
>Boltar <boltar2...@yahoo.co.ukwrote in comp.lang.c++:
>> Quote:
Why - using gcc on linux - does this return 0 in C but returns 1 in C+
+? I don't get it.
>> Quote:
#include <stdio.h>
>> >> Quote:
main()
{
printf("%d\n",sizeof(struct foo));
}
>>
>Change:
> sizeof(struct foo)
>to:
> (int)sizeof(struct foo)
>>
>(You shouldn't supply printf with a size_t if you're using %d)
>
Makes no difference.
> Quote:
>>
>If you get the same output then your C compiler is faulty.
>
Its gcc 4.1.0. Perhaps it is a bug but it seems a pretty fundamental
one.
> Quote:
>>
>In all the standards of C and C++, the value returned by sizeof will
>always be non-zero.
>
Why would it be non zero if theres nothing in it? Surely the whole
point of sizeof is to return the size in bytes of the members inside?
If there are no members it should be zero bytes surely? 1 makes no
sense to me.
No, sizeof() tells you how much less free memory you will have after
creating an instance of the type. Since all objects (and ints, doubles,
etc. counts as objects in this) in both C and C++ must have an address
and no two objects are allowed to share the same address (except perhaps
when using union) an object must take at least one byte.
Another example demonstrating that the size of not the sum of the sizes
of the members is the following:
struct Test
{
char c;
int i;
};
On my machine sizeof(Test) is 8, since the integer is 4-byte aligned 3
padding bytes will be inserted between the char and the int.
--
Erik Wikström | | | | re: sizeof() , different results for same thing in C and C++. Why?
On 10 Jan, 18:14, Erik Wikström <Erik-wikst...@telia.comwrote: Quote:
No, sizeof() tells you how much less free memory you will have after
creating an instance of the type. Since all objects (and ints, doubles,
etc. counts as objects in this) in both C and C++ must have an address
and no two objects are allowed to share the same address (except perhaps
when using union) an object must take at least one byte.
Well it seems in C this doesn't apply , since sizeof() returns zero
for an empty struct. Quote:
On my machine sizeof(Test) is 8, since the integer is 4-byte aligned 3
padding bytes will be inserted between the char and the int.
yes, but byte aligning pre-existing members isn't the same as
conjuring up a byte from nowhere.
B2003 | | | | re: sizeof() , different results for same thing in C and C++. Why?
On 10 Jan, 17:57, Christian Hackl <ha...@sbox.tugraz.atwrote: Quote:
Someone in this group will surely give you a more sophisticated answer,
but if I recall correctly, it has to do with the fact that each object
must be individually addressable in memory, even if it's an
instantiation of an empty class or struct.
Shouldn't it be 4 (or 8) bytes then - the size of a pointer? Why 1
byte?
B2003 | | | | re: sizeof() , different results for same thing in C and C++. Why?
Joe Greer wrote: Quote:
Boltar <boltar2003@yahoo.co.ukwrote in news:c76081d7-da52-4633-a060- add2625445e6@z17g2000hsg.googlegroups.com:
> Quote:
>Hi
>>
>Why - using gcc on linux - does this return 0 in C but returns 1 in
>C+ +? I don't get it.
>>
>#include <stdio.h>
>>
>struct foo
>{
>};
>>
>>
>main()
>{
> printf("%d\n",sizeof(struct foo));
>}
>>
>>
>>
>B2003
>>
>>
>
Do you have RTTI turned on? That could account for it.
What does RTTI have to do with anything? In C++ an empty struct
has a size of 1 if instantiated stand-alone. It's done so that
individual elements of an array of those structs could be indexed
and addressed individually.
If instantiated as a base class of another object, an empty
struct does NOT contribute to the size:
stuct A {
int a;
};
struct foo {};
struct AA : foo {
int a;
};
#include <cassert>
int main() {
assert(sizeof(A) == sizeof(AA));
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask | | | | re: sizeof() , different results for same thing in C and C++. Why?
Boltar wrote: Quote:
On 10 Jan, 17:57, Christian Hackl <ha...@sbox.tugraz.atwrote: Quote:
>Someone in this group will surely give you a more sophisticated
>answer, but if I recall correctly, it has to do with the fact that
>each object must be individually addressable in memory, even if it's
>an instantiation of an empty class or struct.
>
Shouldn't it be 4 (or 8) bytes then - the size of a pointer? Why 1
byte?
What pointer? A byte is the smallest individually addressable unit
in memory. Why waste 3 bytes (or 7)?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask | | | | re: sizeof() , different results for same thing in C and C++. Why?
Boltar wrote: Quote:
On 10 Jan, 17:57, Christian Hackl <ha...@sbox.tugraz.atwrote: Quote:
>Someone in this group will surely give you a more sophisticated answer,
>but if I recall correctly, it has to do with the fact that each object
>must be individually addressable in memory, even if it's an
>instantiation of an empty class or struct.
>
Shouldn't it be 4 (or 8) bytes then - the size of a pointer? Why 1
byte?
Why should it be the same size as a pointer? The pointer variable itself
may occupy 4 or 8 bytes or whatever, but it can still *address* smaller
objects.
char c = '\0'; // sizeof(c) is 1
char *ptr = &c; // sizeof(ptr) may be 4
This is my understanding, at least. However, I'm by no means an expert,
so don't take this explanation for granted :)
--
Christian Hackl | | | | re: sizeof() , different results for same thing in C and C++. Why?
Boltar wrote: Quote:
On 10 Jan, 17:57, Christian Hackl <ha...@sbox.tugraz.atwrote: Quote:
>Someone in this group will surely give you a more sophisticated
>answer, but if I recall correctly, it has to do with the fact that
>each object must be individually addressable in memory, even if
>it's an instantiation of an empty class or struct.
>
Shouldn't it be 4 (or 8) bytes then - the size of a pointer? Why 1
byte?
>
The standard just says that it must be at least one byte. It could be
more, if that is better for some implementation.
Bo Persson | | | | re: sizeof() , different results for same thing in C and C++. Why?
Bo Persson wrote: Quote:
Boltar wrote: Quote:
>Shouldn't it be 4 (or 8) bytes then - the size of a pointer? Why 1
>byte?
>
The standard just says that it must be at least one byte.
Does the standard really say that sizeof of an empty class may be more
than 1 byte? If so, why doesn't it restrict the maximum size to that of
a char, which AFAIK is guaranteed to be 1 byte?
--
Christian Hackl | | | | re: sizeof() , different results for same thing in C and C++. Why?
On 2008-01-10 19:37, Boltar wrote: Quote:
On 10 Jan, 18:14, Erik Wikström <Erik-wikst...@telia.comwrote: Quote:
>No, sizeof() tells you how much less free memory you will have after
>creating an instance of the type. Since all objects (and ints, doubles,
>etc. counts as objects in this) in both C and C++ must have an address
>and no two objects are allowed to share the same address (except perhaps
>when using union) an object must take at least one byte.
>
Well it seems in C this doesn't apply , since sizeof() returns zero
for an empty struct.
I though that we agree that there was something wrong with gcc. In the C
standard section 6.2.6.1, second paragraph: "Except for bit-fields,
objects are composed of contiguous sequences of one or more bytes ...".
--
Erik Wikström | | | | re: sizeof() , different results for same thing in C and C++. Why?
On 2008-01-10 20:12, Christian Hackl wrote: Quote:
Bo Persson wrote:
> Quote:
>Boltar wrote: Quote:
>>Shouldn't it be 4 (or 8) bytes then - the size of a pointer? Why 1
>>byte?
>>
>The standard just says that it must be at least one byte.
>
Does the standard really say that sizeof of an empty class may be more
than 1 byte? If so, why doesn't it restrict the maximum size to that of
a char, which AFAIK is guaranteed to be 1 byte?
What benefit is there in restricting an empty class to 1 byte? Just
guaranteeing that it is at least one is good enough for all purposes.
--
Erik Wikström | | | | re: sizeof() , different results for same thing in C and C++. Why?
Boltar wrote: Quote:
On 10 Jan, 18:14, Erik Wikström <Erik-wikst...@telia.comwrote: Quote:
>No, sizeof() tells you how much less free memory you will have after
>creating an instance of the type. Since all objects (and ints, doubles,
>etc. counts as objects in this) in both C and C++ must have an address
>and no two objects are allowed to share the same address (except perhaps
>when using union) an object must take at least one byte.
>
Well it seems in C this doesn't apply , since sizeof() returns zero
for an empty struct.
In C, empty structs are not allowed. AFAICS, the program is ill-formed. | | | | re: sizeof() , different results for same thing in C and C++. Why?
Why - using gcc on linux - does this return 0 in C but returns 1 in C+ Quote:
+? I don't get it.
>
#include <stdio.h>
>
struct foo
{
};
>
>
main()
{
printf("%d\n",sizeof(struct foo));
}
I don't now in C, but in C++ the standard requires that every complete
object size is greater than 0. Even if it's empty. Why? To avoid
overlapping of different objects. Consider this:
struct A {};
void foo ()
{
A a1;
A a2;
A* pa = &a1;
}
Now, if we use the pointer pa to access a1.. we would also be accessing a2!
This is not desirable at all.
For a better explanation: http://code.grep.in/2005/11/c-programming-sizeof.html
--
Marc | | | | re: sizeof() , different results for same thing in C and C++. Why?
Boltar <boltar2003@yahoo.co.ukwrote in comp.lang.c++: Quote: Quote:
>(You shouldn't supply printf with a size_t if you're using %d)
>
Makes no difference.
It might not make a difference on *your* particular system, but it does
on other implementations of the C++ Standard. Quote: Quote:
>In all the standards of C and C++, the value returned by sizeof will
>always be non-zero.
>
Why would it be non zero if theres nothing in it?
Because the smallest addressable unit is the byte. There's nothing
smaller. Every object is made up of bytes, and the least amount of bytes
an object can have is 1.
I think the real answer you're looking for though is: Because that's just
the way it is. Quote:
Surely the whole
point of sizeof is to return the size in bytes of the members inside?
Don't forget padding. Quote:
If there are no members it should be zero bytes surely? 1 makes no
sense to me.
Again you just have to accept it, that's just the way things were chosen
to be. Pointer arithmetic is based on the size of the object... so things
would be a little more complicated if you could have zero-length objects.
Also, every object must have a unique address in memory (unless we're
talking about inheritence of course).
--
Tomás Ó hÉilidhe | | | | re: sizeof() , different results for same thing in C and C++. Why?
Erik Wikström wrote: Quote:
On 2008-01-10 20:12, Christian Hackl wrote: Quote:
>Does the standard really say that sizeof of an empty class may be more
>than 1 byte? If so, why doesn't it restrict the maximum size to that of
>a char, which AFAIK is guaranteed to be 1 byte?
>
What benefit is there in restricting an empty class to 1 byte?
Hmm, well, none, I guess. I just thought it would make sense like that.
However, I am perfectly happy with the standard not putting any
restrictions on the maximum size.
--
Christian Hackl | | | | re: sizeof() , different results for same thing in C and C++. Why?
Boltar wrote: Quote:
Hi
>
Why - using gcc on linux - does this return 0 in C but returns 1 in C+
+? I don't get it.
>
Did you pay attention to the compiler's warnings? Your example won't
even compile as C++. Quote:
#include <stdio.h>
>
struct foo
{
};
>
>
main()
{
printf("%d\n",sizeof(struct foo));
}
--
Ian Collins. | | | | re: sizeof() , different results for same thing in C and C++. Why?
Erik Wikstrom wrote: Quote:
On 2008-01-10 19:37, Boltar wrote:
Quote: Quote:
Well it seems in C this doesn't apply , since sizeof() returns zero
for an empty struct.
>
I though that we agree that there was something wrong with gcc. In
the C standard section 6.2.6.1, second paragraph: "Except for
bit-fields, objects are composed of contiguous sequences of one or
more bytes ...".
It also requires structs to have a nonempty set of members.
Brian | | | | re: sizeof() , different results for same thing in C and C++. Why?
"Victor Bazarov" <v.Abazarov@comAcast.netwrote in news:fm5p2n$9qs$1
@news.datemas.de: Quote:
>
What does RTTI have to do with anything? In C++ an empty struct
has a size of 1 if instantiated stand-alone. It's done so that
individual elements of an array of those structs could be indexed
and addressed individually.
>
If instantiated as a base class of another object, an empty
struct does NOT contribute to the size:
>
stuct A {
int a;
};
>
struct foo {};
>
struct AA : foo {
int a;
};
>
#include <cassert>
int main() {
assert(sizeof(A) == sizeof(AA));
}
>
V
Sadly, I just jumped into the middle of this and spouted off the first
thing I could think of given the data that the sizeof foo was 0 in C.
Sorry for the noise.
joe | | | | re: sizeof() , different results for same thing in C and C++. Why?
On Thu, 10 Jan 2008 09:18:17 -0800 (PST), Boltar
<boltar2003@yahoo.co.ukwrote in comp.lang.c++: Quote:
Hi
>
Why - using gcc on linux - does this return 0 in C but returns 1 in C+
+? I don't get it.
Because you are not using gcc to compile C, but rather GNU C, which is
a different language. Quote:
#include <stdio.h>
>
struct foo
{
};
The structure definition above is a syntax error in C. Every
structure or union definition must define at least one member. Quote:
main()
{
printf("%d\n",sizeof(struct foo));
}
>
>
>
B2003
There is no such thing as either an object with size of 0 or a struct
with no members in the C language.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html | | | | re: sizeof() , different results for same thing in C and C++. Why?
On Thu, 10 Jan 2008 21:46:29 +0100 (CET), Joe Greer
<jgreer@doubletake.comwrote in comp.lang.c++: Quote:
"Victor Bazarov" <v.Abazarov@comAcast.netwrote in news:fm5p2n$9qs$1
@news.datemas.de:
> Quote:
What does RTTI have to do with anything? In C++ an empty struct
has a size of 1 if instantiated stand-alone. It's done so that
individual elements of an array of those structs could be indexed
and addressed individually.
If instantiated as a base class of another object, an empty
struct does NOT contribute to the size:
stuct A {
int a;
};
struct foo {};
struct AA : foo {
int a;
};
#include <cassert>
int main() {
assert(sizeof(A) == sizeof(AA));
}
V
>
Sadly, I just jumped into the middle of this and spouted off the first
thing I could think of given the data that the sizeof foo was 0 in C.
Sorry for the noise.
No, it's not, there is no such thing in C. The grammar requires a
struct definition to contain at least one member definition.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html | | | | re: sizeof() , different results for same thing in C and C++. Why?
On Thu, 10 Jan 2008 19:27:50 GMT, Tristan Wibberley
<maihem-nn1@maihem.orgwrote in comp.lang.c++: Quote:
>
On Thu, 2008-01-10 at 20:21 +0100, Rolf Magnus wrote:
> Quote:
In C, empty structs are not allowed. AFAICS, the program is ill-formed.
>
Then it's undefined behaviour and there is no problem with the C program
outputting 0.
Not quite, it violates the grammar and thus requires a diagnostic. If
an executable is produced along with the diagnostic, the result is not
a C program so even the C term of "undefined behavior" does not really
apply.
No diagnostic means not compiled with a conforming C compiler, which
most compilers, not just gcc, are not by default.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html | | | | re: sizeof() , different results for same thing in C and C++. Why?
On Jan 10, 11:14 pm, Erik Wikström <Erik-wikst...@telia.comwrote: Quote:
On 2008-01-10 18:27, Boltar wrote:
>
>
> Quote:
On 10 Jan, 17:23, "Tomás Ó hÉilidhe" <t...@lavabit.comwrote: Quote:
Boltar <boltar2...@yahoo.co.ukwrote in comp.lang.c++:
> Quote: Quote:
Why - using gcc on linux - does this return 0 in C but returns 1 in C+
+? I don't get it.
> Quote: Quote:
#include <stdio.h>
> > Quote: Quote:
main()
{
printf("%d\n",sizeof(struct foo));
}
> Quote: Quote:
Change:
sizeof(struct foo)
to:
(int)sizeof(struct foo)
> Quote: Quote:
(You shouldn't supply printf with a size_t if you're using %d)
> Quote:
Makes no difference.
> Quote: Quote:
If you get the same output then your C compiler is faulty.
> Quote:
Its gcc 4.1.0. Perhaps it is a bug but it seems a pretty fundamental
one.
> Quote: Quote:
In all the standards of C and C++, the value returned by sizeof will
always be non-zero.
> Quote:
Why would it be non zero if theres nothing in it? Surely the whole
point of sizeof is to return the size in bytes of the members inside?
If there are no members it should be zero bytes surely? 1 makes no
sense to me.
>
No, sizeof() tells you how much less free memory you will have after
creating an instance of the type. Since all objects (and ints, doubles,
etc. counts as objects in this) in both C and C++ must have an address
and no two objects are allowed to share the same address (except perhaps
when using union) an object must take at least one byte.
>
Another example demonstrating that the size of not the sum of the sizes
of the members is the following:
>
struct Test
{
char c;
int i;
>
};
>
On my machine sizeof(Test) is 8, since the integer is 4-byte aligned 3
padding bytes will be inserted between the char and the int.
>
--
Erik Wikström
Well VC++ compiler has preprocessor #pragma with this you can control
the byte alignment.
#pragma pack(push, 1)
struct Test
{
char c;
int i;
};
#pragma pack(pop)
If you compile it using VC++ you will see the size 5
Regards
Sandeep | | | | re: sizeof() , different results for same thing in C and C++. Why?
When we compile the same program with c and c++, sizeof (Struct Foo)
is 0 and 1 respectively. Because in C++ it allocate some memory for
this pointer.
Regards
Sandeep | | | | re: sizeof() , different results for same thing in C and C++. Why?
sg wrote: Quote:
Well VC++ compiler has preprocessor #pragma with this you can control
the byte alignment.
>
#pragma pack(push, 1)
struct Test
{
char c;
int i;
};
#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). | | | | re: sizeof() , different results for same thing in C and C++. Why?
Juha Nieminen wrote: Quote: Quote:
>#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 :)
Best Regards,
Lars | | | | re: sizeof() , different results for same thing in C and C++. Why?
On 10 Jan, 20:02, Ian Collins <ian-n...@hotmail.comwrote: Quote:
Boltar wrote: > Quote:
Why - using gcc on linux - does this return 0 in C but returns 1 in C+
+? I don't get it.
>
Did you pay attention to the compiler's warnings? Your example won't
even compile as C++.
I think you need to check you system since it compiles and runs fine
on mine:
morgoth$ cat t.cc
#include <stdio.h>
struct foo
{
};
main()
{
printf("%d\n",sizeof(struct foo));
}
morgoth$ g++ t.cc
morgoth$ ./a.out
1
morgoth$
B2003 | | | | re: sizeof() , different results for same thing in C and C++. Why?
On Jan 11, 10:52 am, Boltar <boltar2...@yahoo.co.ukwrote: Quote:
On 10 Jan, 20:02, Ian Collins <ian-n...@hotmail.comwrote: Quote:
Boltar wrote: Quote:
Why - using gcc on linux - does this return 0 in C but returns 1 in C+
+? I don't get it.
Quote: Quote:
Did you pay attention to the compiler's warnings? Your example won't
even compile as C++.
Quote:
I think you need to check you system since it compiles and runs fine
on mine:
Quote:
morgoth$ cat t.cc
#include <stdio.h>
Quote:
main()
{
printf("%d\n",sizeof(struct foo));
}
That doesn't invoke the gcc C++ compiler. To use the C++
compiler, you need:
g++ -std=c++98 -pedantic t.cc
And if you use that, it doesn't compile (at least with g++
4.1.0---nor with Sun CC 5.8).
Similarly, if you invoke the C compiler:
gcc -std=c99 -pedantic t.c
you get error messages as well.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 | | | | re: sizeof() , different results for same thing in C and C++. Why?
On Jan 10, 8:12 pm, Christian Hackl <ha...@sbox.tugraz.atwrote: Quote:
Bo Persson wrote: Quote:
Boltar wrote: Quote:
Shouldn't it be 4 (or 8) bytes then - the size of a pointer? Why 1
byte?
Quote: Quote:
The standard just says that it must be at least one byte.
Quote:
Does the standard really say that sizeof of an empty class may
be more than 1 byte? If so, why doesn't it restrict the
maximum size to that of a char, which AFAIK is guaranteed to
be 1 byte?
Because some architectures require a pointer to a structure to
be aligned on a multiple of 4 (or 6, or whatever) bytes. And
sizeof must take alignment considerations into account.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 | | | | re: sizeof() , different results for same thing in C and C++. Why?
On 11 Jan, 10:46, James Kanze <james.ka...@gmail.comwrote: Quote:
On Jan 11, 10:52 am, Boltar <boltar2...@yahoo.co.ukwrote:
>
>
> Quote:
On 10 Jan, 20:02, Ian Collins <ian-n...@hotmail.comwrote: Quote:
Boltar wrote:
Why - using gcc on linux - does this return 0 in C but returns 1 in C+
+? I don't get it.
Did you pay attention to the compiler's warnings? Your example won't
even compile as C++.
I think you need to check you system since it compiles and runs fine
on mine:
morgoth$ cat t.cc
#include <stdio.h>
struct foo
{
};
main()
{
printf("%d\n",sizeof(struct foo));
}
morgoth$ g++ t.cc
>
That doesn't invoke the gcc C++ compiler. To use the C++
Rubbish. Of course it involkes the C++ compiler , 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.
B2003 | | | | re: sizeof() , different results for same thing in C and C++. Why?
Boltar wrote: Quote:
On 11 Jan, 10:46, James Kanze <james.ka...@gmail.comwrote: Quote:
>On Jan 11, 10:52 am, Boltar <boltar2...@yahoo.co.ukwrote:
>>
>>
>> Quote:
On 10 Jan, 20:02, Ian Collins <ian-n...@hotmail.comwrote:
Boltar wrote:
Why - using gcc on linux - does this return 0 in C but returns 1 in
C+ +? I don't get it.
Did you pay attention to the compiler's warnings? Your example won't
even compile as C++.
I think you need to check you system since it compiles and runs fine
on mine:
morgoth$ cat t.cc
#include <stdio.h>
struct foo
{
};
main()
{
printf("%d\n",sizeof(struct foo));
}
morgoth$ g++ t.cc
>>
>That doesn't invoke the gcc C++ compiler. To use the C++
>
Rubbish. Of course it involkes the C++ compiler , what else is it
invoking , a pascal compiler??
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). Quote:
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.
Irrelevant with regard to which question? The code above is invalid C++ as
per [7/7] and [3.6.1/2] regardless of whether the compiler of your choosing
happens to compile it.
Best
Kai-Uwe Bux | | | | re: sizeof() , different results for same thing in C and C++. Why?
On Jan 10, 6:44*pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote: Quote:
Joe Greer wrote: Quote:
Boltar <boltar2...@yahoo.co.ukwrote in news:c76081d7-da52-4633-a060-
add262544...@z17g2000hsg.googlegroups.com:
> > Quote: Quote:
Why - using gcc on linux - does this return 0 in C but returns 1 in
C+ +? I don't get it.
> Quote: Quote:
#include <stdio.h>
> > Quote: Quote:
main()
{
* * * * printf("%d\n",sizeof(struct foo));
}
> > Quote:
Do you have RTTI turned on? *That could account for it.
>
What does RTTI have to do with anything? *In C++ an empty struct
has a size of 1 if instantiated stand-alone. *It's done so that
individual elements of an array of those structs could be indexed
and addressed individually.
>
If instantiated as a base class of another object, an empty
struct does NOT contribute to the size:
Are you sure that shouldnt be "...MAY not contribute..", at least in
pre C++0x? IIRC this is an optimisation that may be applied at the
discretion of the compiler, hence the moniker EBO or Empty Base class
Optimisation.
I believe that it is however unlikely that this optimisation won't be
applied on most compilers in practise..
(As for C++0x, I don't know, but if this isnt mandatory in pre C++0x
then making it mandatory in C++0x would appear to be a breaking
change, as potentially objects will change in size.)
regards
Andy Little | | | | re: sizeof() , different results for same thing in C and C++. Why?
Boltar wrote: Quote:
Hi
>
Why - using gcc on linux - does this return 0 in C but returns 1 in C+
+? I don't get it.
>
#include <stdio.h>
>
struct foo
{
};
>
The above is invalid in C and has at least size 1 in C++. | | | | re: sizeof() , different results for same thing in C and C++. Why?
On 11 Jan., 11:49, James Kanze <james.ka...@gmail.comwrote: Quote:
On Jan 10, 8:12 pm, Christian Hackl <ha...@sbox.tugraz.atwrote:
> Quote:
Bo Persson wrote: Quote:
Boltar wrote:
>Shouldn't it be 4 (or 8) bytes then - the size of a pointer? Why 1
>byte?
The standard just says that it must be at least one byte.
Does the standard really say that sizeof of an empty class may
be more than 1 byte? If so, why doesn't it restrict the
maximum size to that of a char, which AFAIK is guaranteed to
be 1 byte?
>
Because some architectures require a pointer to a structure to
be aligned on a multiple of 4 (or 6, or whatever) bytes. *And
sizeof must take alignment considerations into account.
>
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.
/Peter | | | | re: sizeof() , different results for same thing in C and C++. Why?
On 2008-01-11 09:46:35 -0500, peter koch <peter.koch.larsen@gmail.comsaid: Quote:
On 11 Jan., 11:49, James Kanze <james.ka...@gmail.comwrote: Quote:
>On Jan 10, 8:12 pm, Christian Hackl <ha...@sbox.tugraz.atwrote:
>> Quote:
>>Bo Persson wrote:
>>>Boltar wrote:
>>>>Shouldn't it be 4 (or 8) bytes then - the size of a pointer? Why 1
>>>>byte?
>>>The standard just says that it must be at least one byte.
>>Does the standard really say that sizeof of an empty class may
>>be more than 1 byte? If so, why doesn't it restrict the
>>maximum size to that of a char, which AFAIK is guaranteed to
>>be 1 byte?
>>
>Because some architectures require a pointer to a structure to
>be aligned on a multiple of 4 (or 6, or whatever) bytes. *And
>sizeof must take alignment considerations into account.
>>
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.
--
Pete
Roundhouse Consulting, Ltd. ( www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
( www.petebecker.com/tr1book) | | | | re: sizeof() , different results for same thing in C and C++. Why?
On 2008-01-11 10:18, Lars Uffmann wrote: Quote:
Juha Nieminen wrote: Quote: Quote:
>>#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...
--
Erik Wikström | | | | re: sizeof() , different results for same thing in C and C++. Why?
In article <696e962a-e295-420a-b2c8-1d76726aabb9
@e6g2000prf.googlegroups.com>, boltar2003@yahoo.co.uk says...
[ ... ] Quote:
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)
--
Later,
Jerry.
The universe is a figment of its own imagination. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,295 network members.
|