473,513 Members | 4,753 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Aug 31 '05 #1
3 1296
Radde wrote:
#include<iostream.h>
#include <iostream>

class A
{
//char i;
};

class B
{
//char i;
};

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

void main()
int main()
{
C c;
cout<<"size is "<<sizeof(c)<<endl;
std::cout<<"size is "<<sizeof(c)<<std::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??
Probably because your default compiler options make alignment at 8-byte
boundary. No way to tell. Alignment and padding are compiler-specific.
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?


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
Aug 31 '05 #2

Victor Bazarov wrote:
Radde wrote:
#include<iostream.h>


#include <iostream>

class A
{
//char i;
};

class B
{
//char i;
};

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

void main()


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


std::cout<<"size is "<<sizeof(c)<<std::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??


Probably because your default compiler options make alignment at 8-byte
boundary. No way to tell. Alignment and padding are compiler-specific.


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


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


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

Aug 31 '05 #3

Greg wrote:

[]
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??
Probably because your default compiler options make alignment at 8-byte
boundary. No way to tell. Alignment and padding are compiler-specific.


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,


Indeed, there is no such reason. The reason is sizeof(C) is always a
multiple of alignof(C), with the latter being compilers specific.
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.
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>
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.
This is wrong.
The answer has nothing to
do with padding or alignment, but rather conformity with the Standards.
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
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.


Sorry, this is bullshit.

Aug 31 '05 #4

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

Similar topics

2
3413
by: jaysonsch | last post by:
Hello! I am having some problems with a database query that I am trying to do. I am trying to develop a way to search a database for an entry and then edit the existing values. Upon submit, the...
29
2477
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
9
3109
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
3
5372
by: Harvey | last post by:
Hi, I try to write an asp query form that lets client search any text-string and display all pages in my web server that contain the text. I have IIS 6.0 on a server 2003. The MSDN site says...
4
2133
by: Diamondback | last post by:
I have two tables, WIDGETS and VERSIONS. The WIDGETS table has descriptive information about the widgets while the VERSIONS table contains IDs relating to different iterations of those widgets...
14
3853
by: Dave Thomas | last post by:
If I have a table set up like this: Name | VARCHAR Email | VARCHAR Age | TINYINT | NULL (Default: NULL) And I want the user to enter his or her name, email, and age - but AGE is optional. ...
0
3485
by: starace | last post by:
I have designed a form that has 5 different list boxes where the selections within each are used as criteria in building a dynamic query. Some boxes are set for multiple selections but these list...
6
4816
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
4
3113
by: Stan | last post by:
I am using MS Office Access 2003 (11.5614). My basic question is can I run a query of a query datasheet. I want to use more that one criteria and can not get that query to work. I thought I...
6
4373
by: jsacrey | last post by:
Hey everybody, got a secnario for ya that I need a bit of help with. Access 97 using linked tables from an SQL Server 2000 machine. I've created a simple query using two tables joined by one...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7519
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.