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



 DEVELOP > c-Plus-Plus > sizeof() , different results for same thing in C and C++. Why?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 2

1

 

2

 
Topic: DEVELOP > c-Plus-Plus
User: "Boltar"
Date: 10 Jan 2008 11:18:17 AM
Object: 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
.

User: "Marc"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 10 Jan 2008 01:25:41 PM

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
.

User: "Tomás Ó hÉilidhe"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 10 Jan 2008 11:23:38 AM
Boltar <boltar2003@yahoo.co.uk> wrote 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
.
User: "Boltar"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 10 Jan 2008 11:27:12 AM
On 10 Jan, 17:23, "Tom=E1s =D3 h=C9ilidhe" <t...@lavabit.com> wrote:

Boltar <boltar2...@yahoo.co.uk> wrote 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
.
User: "Tomás Ó hÉilidhe"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 10 Jan 2008 01:28:30 PM
Boltar <boltar2003@yahoo.co.uk> wrote 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
.

User: "Christian Hackl"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 10 Jan 2008 11:57:33 AM
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
.
User: "Boltar"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 10 Jan 2008 12:37:56 PM
On 10 Jan, 17:57, Christian Hackl <ha...@sbox.tugraz.at> wrote:

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
.
User: "Bo Persson"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 10 Jan 2008 01:04:32 PM
Boltar wrote:

On 10 Jan, 17:57, Christian Hackl <ha...@sbox.tugraz.at> wrote:

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
.
User: "Christian Hackl"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 10 Jan 2008 01:12:04 PM
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
.
User: "=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?="

Title: Re: sizeof() , different results for same thing in C and C++. Why? 10 Jan 2008 01:21:16 PM
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
.
User: "Christian Hackl"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 10 Jan 2008 01:48:18 PM
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
.


User: "James Kanze"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 11 Jan 2008 04:49:40 AM
On Jan 10, 8:12 pm, Christian Hackl <ha...@sbox.tugraz.at> 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?

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=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
.
User: "peter koch"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 11 Jan 2008 08:46:35 AM
On 11 Jan., 11:49, James Kanze <james.ka...@gmail.com> wrote:

On Jan 10, 8:12 pm, Christian Hackl <ha...@sbox.tugraz.at> 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?


Because some architectures require a pointer to a structure to
be aligned on a multiple of 4 (or 6, or whatever) bytes. =A0And
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
.
User: "Pete Becker"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 11 Jan 2008 09:14:43 AM
On 2008-01-11 09:46:35 -0500, peter koch <peter.koch.larsen@gmail.com> said:

On 11 Jan., 11:49, James Kanze <james.ka...@gmail.com> wrote:

On Jan 10, 8:12 pm, Christian Hackl <ha...@sbox.tugraz.at> 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?


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

User: "James Kanze"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 11 Jan 2008 10:08:42 AM
On Jan 11, 3:46 pm, peter koch <peter.koch.lar...@gmail.com> wrote:

On 11 Jan., 11:49, James Kanze <james.ka...@gmail.com> wrote:

On Jan 10, 8:12 pm, Christian Hackl <ha...@sbox.tugraz.at> 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?

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.

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).
--
James Kanze (GABI Software) mailto:james.kanze@gmail.com
Conseils en informatique orient=EF=BF=BDe objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=EF=BF=BDmard, 78210 St.-Cyr-l'=EF=BF=BDcole, France, +33 (0)1 30 2=
3 00 34
.
User: "peter koch"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 11 Jan 2008 12:37:13 PM
On 11 Jan., 17:08, James Kanze <james.ka...@gmail.com> wrote:

On Jan 11, 3:46 pm, peter koch <peter.koch.lar...@gmail.com> wrote:





On 11 Jan., 11:49, James Kanze <james.ka...@gmail.com> wrote:

On Jan 10, 8:12 pm, Christian Hackl <ha...@sbox.tugraz.at> 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?

Because some architectures require a pointer to a structure to
be aligned on a multiple of 4 (or 6, or whatever) bytes. =A0And
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.


That *IS* the reason. =A0Because you can have pointers to
incomplete classes, all class pointers must have the same size
and layout. =A0On 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. =A0And 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
.






User: "Victor Bazarov"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 10 Jan 2008 12:54:59 PM
Boltar wrote:

On 10 Jan, 17:57, Christian Hackl <ha...@sbox.tugraz.at> wrote:

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
.

User: "Christian Hackl"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 10 Jan 2008 12:58:42 PM
Boltar wrote:

On 10 Jan, 17:57, Christian Hackl <ha...@sbox.tugraz.at> wrote:

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
.



User: "=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?="

Title: Re: sizeof() , different results for same thing in C and C++. Why? 10 Jan 2008 12:14:29 PM
On 2008-01-10 18:27, Boltar wrote:

On 10 Jan, 17:23, "Tomás Ó hÉilidhe" <t...@lavabit.com> wrote:

Boltar <boltar2...@yahoo.co.uk> wrote 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
.
User: "Boltar"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 10 Jan 2008 12:37:19 PM
On 10 Jan, 18:14, Erik Wikstr=F6m <Erik-wikst...@telia.com> wrote:

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
.
User: "Rolf Magnus"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 10 Jan 2008 01:21:41 PM
Boltar wrote:

On 10 Jan, 18:14, Erik Wikström <Erik-wikst...@telia.com> wrote:

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

User: "Jerry Coffin"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 11 Jan 2008 09:34:01 PM
In article <696e962a-e295-420a-b2c8-1d76726aabb9
@e6g2000prf.googlegroups.com>,
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.
.

User: "=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?="

Title: Re: sizeof() , different results for same thing in C and C++. Why? 10 Jan 2008 01:16:26 PM
On 2008-01-10 19:37, Boltar wrote:

On 10 Jan, 18:14, Erik Wikström <Erik-wikst...@telia.com> wrote:

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
.
User: "Default User"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 10 Jan 2008 02:12:21 PM
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
.



User: "sg"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 11 Jan 2008 01:19:48 AM
On Jan 10, 11:14 pm, Erik Wikstr=F6m <Erik-wikst...@telia.com> wrote:

On 2008-01-10 18:27, Boltar wrote:



On 10 Jan, 17:23, "Tom=E1s =D3 h=C9ilidhe" <t...@lavabit.com> wrote:

Boltar <boltar2...@yahoo.co.uk> wrote 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=F6m

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
.
User: "Juha Nieminen"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 11 Jan 2008 02:43:32 AM
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).
.
User: "Lars Uffmann"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 11 Jan 2008 03:18:34 AM
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
.
User: "=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?="

Title: Re: sizeof() , different results for same thing in C and C++. Why? 11 Jan 2008 11:35:34 AM
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
.







User: "Joe Greer"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 10 Jan 2008 11:47:01 AM
Boltar <boltar2003@yahoo.co.uk> wrote in news:c76081d7-da52-4633-a060-
add2625445e6@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
.
User: "Victor Bazarov"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 10 Jan 2008 12:44:41 PM
Joe Greer wrote:

Boltar <boltar2003@yahoo.co.uk> wrote in news:c76081d7-da52-4633-a060-
add2625445e6@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
.
User: "Joe Greer"

Title: Re: sizeof() , different results for same thing in C and C++. Why? 10 Jan 2008 02:46:29 PM
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote 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
.




  Page 1 of 2

1

 

2

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER