473,396 Members | 2,004 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,396 software developers and data experts.

why class A and E's sizes are 4 instead of 1?

Hi,

I have #include <iostream>

using namespace std;

class A{
public:
unsigned x : 5;
unsigned y : 3;
};

class B
{ };

class C
{
public:
char test;
};

class D
{
public:
int x;
};

class E
{
public:
bool f1;
};

int main()
{
cout<<"sizeof(bool)="<<sizeof(bool)<<endl;
cout<<"sizeof(A) = "<< sizeof(A) << endl;
cout<<"sizeof(B) = "<<sizeof(B) <<endl;
cout<<"sizeof(C) = "<<sizeof(C) <<endl;
cout<<"sizeof(D) = "<<sizeof(D) <<endl;
cout<<"sizeof(E) = "<< sizeof(E) <<endl;
return;
}
}

Aug 24 '06 #1
3 1185

yu******@gmail.com wrote:
Hi,

I have #include <iostream>

using namespace std;

class A{
public:
unsigned x : 5;
unsigned y : 3;
};

...

class E
{
public:
bool f1;
};

int main()
{
cout<<"sizeof(bool)="<<sizeof(bool)<<endl;
cout<<"sizeof(A) = "<< sizeof(A) << endl;
cout<<"sizeof(B) = "<<sizeof(B) <<endl;
cout<<"sizeof(C) = "<<sizeof(C) <<endl;
cout<<"sizeof(D) = "<<sizeof(D) <<endl;
cout<<"sizeof(E) = "<< sizeof(E) <<endl;
return;
}
}
How do you figure class A and class E could ever be a size of 1?
Haven't you noticed that even an empty class is not really empty?
If it was, how would the program know which instance is which?

A programmer knows that an integer or a pointer, for example, is not
neccessarily 4 bytes.
He also knows that a class will have its members padded by the compiler
appropriately in order to save clock cycles. Are you not using a 32 bit
or 64 bit machine?
Isn't such a machine set up to support quick, efficient indexing of
their respective memory architecture schemes? Doesn't it make sense
that such an architecture would require extra steps to extract a subset
of the contents of a particular indexed address (ie: a single byte at
bits 8 to 15 of a 32 bit location)? That would slow your computer to a
crawl, wouldn't it?

A programmer's goal is to write code that is transparent to the
hardware/platform its running on. The programmer doesn't care how the
padding is implemented on one compiler/platform or another. If he does
care, he'll likely write buggy code.

Aug 25 '06 #2
On 24 Aug 2006 14:09:38 -0700 in comp.lang.c++, yu******@gmail.com
wrote,
>class A{
public:
unsigned x : 5;
unsigned y : 3;
};
unsigned = unsigned int.
compare:

class A{
public:
unsigned char x : 5;
unsigned char y : 3;
};

Aug 25 '06 #3

yu******@gmail.com wrote:
Hi,

I have #include <iostream>

using namespace std;

class A{
public:
unsigned x : 5;
unsigned y : 3;
};

class B
{ };

class C
{
public:
char test;
};

class D
{
public:
int x;
};

class E
{
public:
bool f1;
};
And the size of bool depends on the compliar.
In Visual C++4.2, the Standard C++ header files contained a typedef
that equated bool with int. In Visual C++ 5.0 and later, bool is
implemented as a built-in type with a size of 1 byte.
Also, In our old version of gcc, the size of the bool type was
apparently 4 bytes, but in gcc-3.2.3, it is 1 byte.

According to section 5.3.3 of C++ standard, "the result of sizeof
applied to any other fundamental type is implementation
defined."[ISO98].

I don't have a standard on my hand, these information is from
Internet or MSDN.
int main()
{
cout<<"sizeof(bool)="<<sizeof(bool)<<endl;
cout<<"sizeof(A) = "<< sizeof(A) << endl;
cout<<"sizeof(B) = "<<sizeof(B) <<endl;
cout<<"sizeof(C) = "<<sizeof(C) <<endl;
cout<<"sizeof(D) = "<<sizeof(D) <<endl;
cout<<"sizeof(E) = "<< sizeof(E) <<endl;
return;
}
}
Aug 25 '06 #4

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

Similar topics

31
by: John Roth | last post by:
I'm adding a thread for comments on Gerrit Holl's pre-pep, which can be found here: http://tinyurl.com/2578q Frankly, I like the idea. It's about time that all of the file and directory stuff...
38
by: Kai Jaeger | last post by:
I am playing with setting font sizes in CSS using em as unit of measurement. All seems to be fine. Even Netscape Navigator shows the characters very similar to IE, what is not the kind if px is...
9
by: JustSomeGuy | last post by:
I need a class that is essentially a matrix (template) class. I need to be able add rows and columns as needed. any one know of such a class?
7
by: Göran Tänzer | last post by:
Hi, i've written a class which does some calculations for my web application. These informatinos are different for each page request - the current user is not important. i have about 10 aspx...
2
by: Saber | last post by:
Public Class User Private _id, _P, _L, _R As Integer Private _balance As Int64 Private _name, _family, _tel1, _tel2, _mobile, _city, _accountNumber As String Private _password, _email,...
60
by: deko | last post by:
As I understand it, most browser manufacturers have agreed on 16px for their default font size. So, this should be an accurate conversion for percentages: px % 16 = 100 14 = 87.5 13 =...
1
by: Edward | last post by:
I created a simple CSS layout (code and example below) for bloggin/writing but ran into five issues that I need help with: 1. How do I get rid of the right-margin red line on the last three...
16
by: Frank Steinmetzger | last post by:
Hello Group On my website I used to have Tahoma 8pt defined in my CSS styles. That gives me the "normal" font Windows uses everywhere in its dialogues. However, on Linux things seem to be...
28
Nepomuk
by: Nepomuk | last post by:
Hi! I've read, that in C++ there's no predefined method, to calculate the size of an array. However, it's supposed to work withsizeof(array)/sizeof(array)Now, this does work in some situations, but...
1
by: Scott Stark | last post by:
What's the best way to represent data that exists in a grid structure? Let's say product #12345 is availabile in 3 colors, my grid might look like this: COLOR S M L ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
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,...

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.