473,379 Members | 1,330 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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

Jan 10 '08 #1
40 2743
Boltar <bo********@yahoo.co.ukwrote in comp.lang.c++:
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
Jan 10 '08 #2
On 10 Jan, 17:23, "Tomás Ó hÉilidhe" <t...@lavabit.comwrote:
Boltar <boltar2...@yahoo.co.ukwrote in comp.lang.c++:
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)
Makes no difference.
>
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.
>
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
Jan 10 '08 #3
Boltar <bo********@yahoo.co.ukwrote in news:c76081d7-da52-4633-a060-
ad**********@z17g2000hsg.googlegroups.com:
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
Jan 10 '08 #4
Boltar wrote:
>>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
Jan 10 '08 #5
On 10 Jan, 17:47, Joe Greer <jgr...@doubletake.comwrote:
Boltar <boltar2...@yahoo.co.ukwrote in news:c76081d7-da52-4633-a060-
add262544...@z17g2000hsg.googlegroups.com:
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
No that I know if - just using "cc t.c" and "c++ t.c" to compile

B2003
Jan 10 '08 #6
On 2008-01-10 18:27, Boltar wrote:
On 10 Jan, 17:23, "Tomás Ó hÉilidhe" <t...@lavabit.comwrote:
>Boltar <boltar2...@yahoo.co.ukwrote in comp.lang.c++:
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)

Makes no difference.
>>
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.
>>
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
Jan 10 '08 #7
On 10 Jan, 18:14, Erik Wikström <Erik-wikst...@telia.comwrote:
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.
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
Jan 10 '08 #8
On 10 Jan, 17:57, Christian Hackl <ha...@sbox.tugraz.atwrote:
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
Jan 10 '08 #9
Joe Greer wrote:
Boltar <bo********@yahoo.co.ukwrote in news:c76081d7-da52-4633-a060-
ad**********@z17g2000hsg.googlegroups.com:
>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
Jan 10 '08 #10
Boltar wrote:
On 10 Jan, 17:57, Christian Hackl <ha...@sbox.tugraz.atwrote:
>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
Jan 10 '08 #11
Boltar wrote:
On 10 Jan, 17:57, Christian Hackl <ha...@sbox.tugraz.atwrote:
>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
Jan 10 '08 #12
Boltar wrote:
On 10 Jan, 17:57, Christian Hackl <ha...@sbox.tugraz.atwrote:
>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
Jan 10 '08 #13
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?
--
Christian Hackl
Jan 10 '08 #14
On 2008-01-10 19:37, Boltar wrote:
On 10 Jan, 18:14, Erik Wikström <Erik-wikst...@telia.comwrote:
>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
Jan 10 '08 #15
On 2008-01-10 20:12, Christian Hackl wrote:
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?
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
Jan 10 '08 #16
Boltar wrote:
On 10 Jan, 18:14, Erik Wikström <Erik-wikst...@telia.comwrote:
>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.

Jan 10 '08 #17
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));
}
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
Jan 10 '08 #18
Boltar <bo********@yahoo.co.ukwrote in comp.lang.c++:
>(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.

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

Surely the whole
point of sizeof is to return the size in bytes of the members inside?

Don't forget padding.

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
Jan 10 '08 #19
Erik Wikström wrote:
On 2008-01-10 20:12, Christian Hackl wrote:
>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
Jan 10 '08 #20
Boltar wrote:
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++.
#include <stdio.h>

struct foo
{
};
main()
{
printf("%d\n",sizeof(struct foo));
}
--
Ian Collins.
Jan 10 '08 #21
Erik Wikstrom wrote:
On 2008-01-10 19:37, Boltar wrote:
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
Jan 10 '08 #22
"Victor Bazarov" <v.********@comAcast.netwrote in news:fm5p2n$9qs$1
@news.datemas.de:
>
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
Jan 10 '08 #23
On Thu, 10 Jan 2008 09:18:17 -0800 (PST), Boltar
<bo********@yahoo.co.ukwrote in comp.lang.c++:
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.

#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.
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
Jan 11 '08 #24
On Thu, 10 Jan 2008 21:46:29 +0100 (CET), Joe Greer
<jg****@doubletake.comwrote in comp.lang.c++:
"Victor Bazarov" <v.********@comAcast.netwrote in news:fm5p2n$9qs$1
@news.datemas.de:

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
Jan 11 '08 #25
On Thu, 10 Jan 2008 19:27:50 GMT, Tristan Wibberley
<ma********@maihem.orgwrote in comp.lang.c++:
>
On Thu, 2008-01-10 at 20:21 +0100, Rolf Magnus wrote:
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
Jan 11 '08 #26
sg
On Jan 10, 11:14 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2008-01-10 18:27, Boltar wrote:
On 10 Jan, 17:23, "Tomás Ó hÉilidhe" <t...@lavabit.comwrote:
Boltar <boltar2...@yahoo.co.ukwrote in comp.lang.c++:
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)
Makes no difference.
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.
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
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
Jan 11 '08 #27
sg
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

Jan 11 '08 #28
sg wrote:
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).
Jan 11 '08 #29
Juha Nieminen wrote:
>#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
Jan 11 '08 #30
On 10 Jan, 20:02, Ian Collins <ian-n...@hotmail.comwrote:
Boltar wrote:
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++.
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
Jan 11 '08 #31
On Jan 11, 10:52 am, Boltar <boltar2...@yahoo.co.ukwrote:
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++
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:ja*********@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

Jan 11 '08 #32
On Jan 10, 8:12 pm, Christian Hackl <ha...@sbox.tugraz.atwrote:
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.

--
James Kanze (GABI Software) email:ja*********@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
Jan 11 '08 #33
On 11 Jan, 10:46, James Kanze <james.ka...@gmail.comwrote:
On Jan 11, 10:52 am, Boltar <boltar2...@yahoo.co.ukwrote:
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?? 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
Jan 11 '08 #34
Boltar wrote:
On 11 Jan, 10:46, James Kanze <james.ka...@gmail.comwrote:
>On Jan 11, 10:52 am, Boltar <boltar2...@yahoo.co.ukwrote:
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).

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
Jan 11 '08 #35
On Jan 10, 6:44*pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Joe Greer wrote:
Boltar <boltar2...@yahoo.co.ukwrote in news:c76081d7-da52-4633-a060-
add262544...@z17g2000hsg.googlegroups.com:
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:
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


Jan 11 '08 #36
Boltar wrote:
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++.
Jan 11 '08 #37
On 11 Jan., 11:49, James Kanze <james.ka...@gmail.comwrote:
On Jan 10, 8:12 pm, Christian Hackl <ha...@sbox.tugraz.atwrote:
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.

/Peter
Jan 11 '08 #38
On 2008-01-11 09:46:35 -0500, peter koch <pe***************@gmail.comsaid:
On 11 Jan., 11:49, James Kanze <james.ka...@gmail.comwrote:
>On Jan 10, 8:12 pm, Christian Hackl <ha...@sbox.tugraz.atwrote:
>>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)

Jan 11 '08 #39
On 2008-01-11 10:18, Lars Uffmann wrote:
Juha Nieminen wrote:
>>#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
Jan 11 '08 #40
In article <696e962a-e295-420a-b2c8-1d76726aabb9
@e6g2000prf.googlegroups.com>, bo********@yahoo.co.uk says...

[ ... ]
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.
Jan 12 '08 #41

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

70
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this...
12
by: jl_post | last post by:
Dear C++ community, I have a question regarding the size of C++ std::strings. Basically, I compiled the following code under two different compilers: std::string someString = "Hello, world!";...
15
by: fdunne2 | last post by:
The following C-code implements a simple FIR filter: //realtime filter demo #include <stdio.h> #include <stdlib.h> //function defination float rtFilter1(float *num, float *den, float...
37
by: thushianthan15 | last post by:
Greetings!! Currently i am learning C from K&R.I have DOS & Linux in my system.When i used sizeof() keyword to compute the size of integer , it shows different results in DOS and Linux.In DOS it...
12
by: ozbear | last post by:
If one were writing a C interpreter, is there anything in the standard standard that requires the sizeof operator to yield the same value for two different variables of the same type? Let's...
43
by: Richard | last post by:
Could someone point me to why "sizeof x" is/isnt preferable to "sizeof(x)",
28
by: Howard Bryce | last post by:
I have come across code containing things like sizeof int How come that one can invoke sizeof without any parentheses surrounding its argument? Is this admissible within the standard? Can it...
8
by: Chameleon | last post by:
I have a TGA image header struct. TGA has 18 bytes header, so the C struct too. why this return 20? sizeof(TGAHeader) I saw this in many structs. I believe compiler round up the size to 4...
38
by: James Brown | last post by:
All, I have a quick question regarding the size of pointer-types: I believe that the sizeof(char *) may not necessarily be the same as sizeof(int *) ? But how about multiple levels of pointers...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.