Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #10778 > unrolled thread
| Started by | asit <lipun4u@gmail.com> |
|---|---|
| First post | 2011-10-10 11:22 -0700 |
| Last post | 2011-10-10 22:48 +0200 |
| Articles | 7 — 6 participants |
Back to article view | Back to comp.lang.c++
sizeof class asit <lipun4u@gmail.com> - 2011-10-10 11:22 -0700
Re: sizeof class Gerald Breuer <Gerald.Breuer@googlemail.com> - 2011-10-10 20:30 +0200
Re: sizeof class asit <lipun4u@gmail.com> - 2011-10-10 12:13 -0700
Re: sizeof class Ian Collins <ian-news@hotmail.com> - 2011-10-11 08:23 +1300
Re: sizeof class Victor Bazarov <v.bazarov@comcast.invalid> - 2011-10-10 15:28 -0400
Re: sizeof class Gerald Breuer <Gerald.Breuer@Use-Author-Supplied-Address.invalid> - 2011-10-10 22:25 +0200
Re: sizeof class Markus Wichmann <nullplan@gmx.net> - 2011-10-10 22:48 +0200
| From | asit <lipun4u@gmail.com> |
|---|---|
| Date | 2011-10-10 11:22 -0700 |
| Subject | sizeof class |
| Message-ID | <21470027.1607.1318270963966.JavaMail.geo-discussion-forums@prdw1> |
Please go through the following code...
#include <iostream>
using namespace std;
class Empty {};
class Derived1 : public Empty {};
class Derived2 : virtual public Empty {};
class Derived3 : public Empty
{
char c;
};
class Derived4 : virtual public Empty
{
char c;
};
class Dummy
{
char c;
};
int main()
{
cout<<"Sizeof(Empty) : "<<sizeof(Empty)<<endl;
cout<<"Sizeof(Derived1) : "<<sizeof(Derived1)<<endl;
cout<<"Sizeof(Derived2) : "<<sizeof(Derived2)<<endl;
cout<<"Sizeof(Derived3) : "<<sizeof(Derived3)<<endl;
cout<<"Sizeof(Derived4) : "<<sizeof(Derived4)<<endl;
cout<<"Sizeof(Dummy) : "<<sizeof(Dummy)<<endl;
return 0;
}
Why sizeof Derived4 is 8 bytes ?
[toc] | [next] | [standalone]
| From | Gerald Breuer <Gerald.Breuer@googlemail.com> |
|---|---|
| Date | 2011-10-10 20:30 +0200 |
| Message-ID | <j6vdje$hqj$1@speranza.aioe.org> |
| In reply to | #10778 |
Am 10.10.2011 20:22, schrieb asit: > Why sizeof Derived4 is 8 bytes ? In this case the compiler inserted a four bytes pointer (you're on a 32-bit-environment) to the virtual base-class. That's usual with virtual base-classes - but implementation defined.
[toc] | [prev] | [next] | [standalone]
| From | asit <lipun4u@gmail.com> |
|---|---|
| Date | 2011-10-10 12:13 -0700 |
| Message-ID | <30470378.1681.1318274028862.JavaMail.geo-discussion-forums@prmr25> |
| In reply to | #10779 |
Hence the size should be 5 bytes(4 for v-pointer + 1 for char). Then why its is of 8 bytes ?
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-10-11 08:23 +1300 |
| Message-ID | <9fgv0rFda1U1@mid.individual.net> |
| In reply to | #10780 |
On 10/11/11 08:13 AM, asit wrote: What are you replying to? Please keep context. > Hence the size should be 5 bytes(4 for v-pointer + 1 for char). Then why its is of 8 bytes ? The size of a class with a virtual base is implementation defined. -- Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Date | 2011-10-10 15:28 -0400 |
| Message-ID | <j6vh1h$535$1@dont-email.me> |
| In reply to | #10781 |
On 10/10/2011 3:23 PM, Ian Collins wrote: > On 10/11/11 08:13 AM, asit wrote: > > What are you replying to? Please keep context. > >> Hence the size should be 5 bytes(4 for v-pointer + 1 for char). Then >> why its is of 8 bytes ? > > The size of a class with a virtual base is implementation defined. I am not even sure that implementation-definedness is limited to classes with virtual bases. To OP: have you tried printing out the value sizeof yields for a class with a single 'char' data member? V -- I do not respond to top-posted replies, please don't ask
[toc] | [prev] | [next] | [standalone]
| From | Gerald Breuer <Gerald.Breuer@Use-Author-Supplied-Address.invalid> |
|---|---|
| Date | 2011-10-10 22:25 +0200 |
| Message-ID | <201110102025.UTC.j6vkca$upo$1@tioat.net> |
| In reply to | #10780 |
Am 10.10.2011 21:13, schrieb asit: > Hence the size should be 5 bytes(4 for v-pointer + 1 for char). Then why its is of 8 bytes ? That's also implementation-defined. But usually your compiler adds some padding so that each elment in an array of objects is properly aligned. I'm pretty sure the implementation of your compiler has a #pragma pack(xyz) directive.
[toc] | [prev] | [next] | [standalone]
| From | Markus Wichmann <nullplan@gmx.net> |
|---|---|
| Date | 2011-10-10 22:48 +0200 |
| Message-ID | <0a2cm8-3t9.ln1@voyager.wichi.de.vu> |
| In reply to | #10780 |
On 10.10.2011 21:13, asit wrote: > Hence the size should be 5 bytes(4 for v-pointer + 1 for char). Then why its is of 8 bytes ? Can you say padding? The alignment of the structure resulting from the class definition would be four bytes, but it has a size of five bytes. To enforce alignment in aggregate types, the size is adjusted to be divisible by four. HTH, Markus
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web