473,769 Members | 6,597 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(b ool)="<<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 1201

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(b ool)="<<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(b ool)="<<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
3928
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 in the os module got objectified properly (or at least with some semblance of OO propriety!) In the issues section:
38
4596
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 used! But! when selecting the "Larger" or "Smaller" command from the menubar in IE, font sizes increases from normal (1em) to, say, 6em or so _in the first step_!!! In the next step it seems to be 20em or say. Choosing "Smaller" makes the text...
9
1445
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
1682
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 pages and 20 ascx user controls. In most of these pages/user controls i need the informations of this class. First i created an instance of this class in every page/user control i
2
1305
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, _ficheNumber, _postalAddress As String Private _province, _bank As String Private _isSpecial, _photo As Boolean Public stk As New Stack
60
4787
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 = 81.25
1
2675
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 right-boxes in Internet Explorer? 2. How can I put a 4px margin BETWEEN the right-boxes, "border-top: 4px" didn't do it and if I put in a "margin-top: 4px;" it shows the red underneath instead of the brown background graphic.
16
5797
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 different. If I want a font equal in size to Windows' Tahoma 8pt, I need to set 11 as font size. This has the result that text on my website is too small for Linux systems at the moment. Could you tell me what I need to do in order to get the same...
28
4578
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 not in others. Here's what I mean:#include <iostream> int length(int * array){return sizeof(array)/sizeof(array);} int main() { int array1 = {3,2,1,0}; std::cout << "Length of array1: " << sizeof(array1)/sizeof(array1) << "\n" <<...
1
2023
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 XL Red x x x x Blue x x x Green x x I have a Product class and I basically want to create a property that...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10214
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10048
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3963
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.