Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 31st, 2005, 05:25 AM
Radde
Guest
 
Posts: n/a
Default query?

HI all,

#include<iostream.h>

class A
{
//char i;
};

class B
{
//char i;
};

class C: public A, public B
{
private:
int i;
};

void main()
{
C c;
cout<<"size is "<<sizeof(c)<<endl;
}

When i run above code, iam getting size as 8, actually there is only
one int variable, it should be 4, why is it 8??
Suppose if i move int variable from class C to clas A, then size is 4.
why is it 8 if we add variable to class C?

Cheers...

  #2  
Old August 31st, 2005, 06:05 AM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: query?

Radde wrote:[color=blue]
> #include<iostream.h>[/color]

#include <iostream>
[color=blue]
>
> class A
> {
> //char i;
> };
>
> class B
> {
> //char i;
> };
>
> class C: public A, public B
> {
> private:
> int i;
> };
>
> void main()[/color]

int main()
[color=blue]
> {
> C c;
> cout<<"size is "<<sizeof(c)<<endl;[/color]

std::cout<<"size is "<<sizeof(c)<<std::endl;
[color=blue]
> }
>
> When i run above code, iam getting size as 8, actually there is only
> one int variable, it should be 4, why is it 8??[/color]

Probably because your default compiler options make alignment at 8-byte
boundary. No way to tell. Alignment and padding are compiler-specific.
[color=blue]
> Suppose if i move int variable from class C to clas A, then size is 4.
> why is it 8 if we add variable to class C?[/color]

You need to ask in the newsgroup dedicated to your compiler. Those issues
are compiler-specific and are not defined in the language Standard.

V


  #3  
Old August 31st, 2005, 07:15 AM
Greg
Guest
 
Posts: n/a
Default Re: query?


Victor Bazarov wrote:[color=blue]
> Radde wrote:[color=green]
> > #include<iostream.h>[/color]
>
> #include <iostream>
>[color=green]
> >
> > class A
> > {
> > //char i;
> > };
> >
> > class B
> > {
> > //char i;
> > };
> >
> > class C: public A, public B
> > {
> > private:
> > int i;
> > };
> >
> > void main()[/color]
>
> int main()
>[color=green]
> > {
> > C c;
> > cout<<"size is "<<sizeof(c)<<endl;[/color]
>
> std::cout<<"size is "<<sizeof(c)<<std::endl;
>[color=green]
> > }
> >
> > When i run above code, iam getting size as 8, actually there is only
> > one int variable, it should be 4, why is it 8??[/color]
>
> Probably because your default compiler options make alignment at 8-byte
> boundary. No way to tell. Alignment and padding are compiler-specific.[/color]

The question being asked is why an instance of class C is larger than
an instance of class A or class B, not why it is four bytes larger. The
question is a good one. There would seem to be no apparent reason why a
compiler would increase the size of C when it inherits from A and B,
even when the amount of data that class C stores remains unchanged. And
in fact the only reason that the C++ compiler makes C larger in this
situation is because the C++ Standard requires it.

The C++ Standard says that neither class A or class B in this situation
can be of size zero bytes. Therefore an object of class C will
necessarily have to be larger than either of its two base classes,
because C's size cannot be zero bytes either. The answer has nothing to
do with padding or alignment, but rather conformity with the Standards.
Granted, the Standard doesn't say how much larger C has to be, but it
does explain why C is larger than A or B. And that really is the
question being asked.

Note that the "empty base class optimization" would make classes A and
B add zero incremental bytes to objects of type C. To take advantage of
this optimization (if the compiler supports it) classes A and B must
each derive from an empty base class themselves.
[color=blue][color=green]
> > Suppose if i move int variable from class C to clas A, then size is 4.
> > why is it 8 if we add variable to class C?[/color]
>
> You need to ask in the newsgroup dedicated to your compiler. Those issues
> are compiler-specific and are not defined in the language Standard.[/color]

The Standard does tell us that moving the declaration from a derived to
base class around will not change the relative sizes of the classes
involved. And knowing that fact is sufficient to answer this question.

Greg

  #4  
Old August 31st, 2005, 08:55 AM
Maxim Yegorushkin
Guest
 
Posts: n/a
Default Re: query?


Greg wrote:

[]
[color=blue][color=green][color=darkred]
> > > When i run above code, iam getting size as 8, actually there is only
> > > one int variable, it should be 4, why is it 8??[/color]
> >
> > Probably because your default compiler options make alignment at 8-byte
> > boundary. No way to tell. Alignment and padding are compiler-specific.[/color]
>
> The question being asked is why an instance of class C is larger than
> an instance of class A or class B, not why it is four bytes larger. The
> question is a good one. There would seem to be no apparent reason why a
> compiler would increase the size of C when it inherits from A and B,[/color]

Indeed, there is no such reason. The reason is sizeof(C) is always a
multiple of alignof(C), with the latter being compilers specific.
[color=blue]
> even when the amount of data that class C stores remains unchanged. And
> in fact the only reason that the C++ compiler makes C larger in this
> situation is because the C++ Standard requires it.[/color]

It does not. Refer to §10/5:

<q>
.... A base class subobject may be of zero size (clause 9); however, two
subobjects that have the same class type and that belong to the same
most derived object must not be allocated at the same address (5.10).
</q>
[color=blue]
> The C++ Standard says that neither class A or class B in this situation
> can be of size zero bytes. Therefore an object of class C will
> necessarily have to be larger than either of its two base classes,
> because C's size cannot be zero bytes either.[/color]

This is wrong.
[color=blue]
> The answer has nothing to
> do with padding or alignment, but rather conformity with the Standards.[/color]

This is also wrong. Alignment is the reason. Consider:

#include <stdio.h>

struct A {};
struct B {};
struct C {};
struct D {};
struct E {};
struct F {};
struct G {};

struct H : A, B, C, D, E, F, G
{
};

struct I : A, B, C, D, E, F, G
{
} __attribute__((aligned(8)));

int main()
{
printf("alignof(H) == %u\n", __alignof__(H));
printf("sizeof(H) == %u\n", sizeof(H));
printf("alignof(I) == %u\n", __alignof__(I));
printf("sizeof(I) == %u\n", sizeof(I));
}

[max@localhost exp]$ g++ -Wall exp.cpp -o exp

[max@localhost exp]$ ./exp
alignof(H) == 1
sizeof(H) == 1
alignof(I) == 8
sizeof(I) == 8
[color=blue]
> Granted, the Standard doesn't say how much larger C has to be, but it
> does explain why C is larger than A or B. And that really is the
> question being asked.
>
> Note that the "empty base class optimization" would make classes A and
> B add zero incremental bytes to objects of type C. To take advantage of
> this optimization (if the compiler supports it) classes A and B must
> each derive from an empty base class themselves.[/color]

Sorry, this is bullshit.

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

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 network members.
Post your question now . . .
It's fast and it's free

Popular Articles