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

size of class that uses bit fields


Why is the Class size 4-bytes instead of 1 byte as I intend to ?

=================================
class BitTest {
public:
unsigned eightBits : 8;
};

int main()
{
cout << "size of BitTest: " << sizeof(BitTest) << endl;
}
==================================
-- xiaobin
Jul 22 '05 #1
3 1810
Xiaobin Yang wrote:
Why is the Class size 4-bytes instead of 1 byte as I intend to ?

=================================
class BitTest {
public:
unsigned eightBits : 8;
};

int main()
{
cout << "size of BitTest: " << sizeof(BitTest) << endl;
}
==================================


Whatever your intentions are, the compiler may (a) not know about
them and (b) have its own agenda. While you can attempt to make
the class object a byte long by saying

class MyThingThatShouldBeOneCharLong {
char data;
};

the compiler is still allowed to pad the object to make it larger
for alignment purposes.

The compiler may not be all that evil as you might now think. See
the documentation for your compiler to learn about the ways to tell
it how to align your structures.

BTW, using 'unsigned' for bit fields causes the underlying member
to have size of 'unsigned int', which is probably 4 on your system.
You might try doing

unsigned char eightBits : 8;

but again, see what alignment options you have in your compiler.

Victor
Jul 22 '05 #2
"Xiaobin Yang" <xy***@esat.kuleuven.ac.be> wrote in message
news:Pi******************************@speyburn.esa t.kuleuven.ac.be...

Why is the Class size 4-bytes instead of 1 byte as I intend to ?

=================================
class BitTest {
public:
unsigned eightBits : 8;
};

int main()
{
cout << "size of BitTest: " << sizeof(BitTest) << endl;
}
==================================


The language standard allows an implementation to
insert any amount of 'padding' after any struct/class member
(but not before the first). This is typically done in the
interest of 'efficiency' and/or to meet the host platform's
alignment requirements.

Some implementations (for platforms on which such things
are possible) provide a #pragma to control struct 'packing'.
Check your documentation.

But using such a construct renders the code nonportable.

You also seem to be exhibiting a misconception:
that 'byte' means 'eight bits'. The language requires
type 'char' (which is synonymous with 'byte') to have
a *minimum* of eight bits, but does not prohibit a
larger size (e.g. 9, 16, 32, etc.).

-Mike
Jul 22 '05 #3
Thanks! I find it out .

This class size is 3 bytes. in this case, the increment step is
sizeof(char). If both unsigned and char are used in member variables, the
increment step depends on recent history record.

class Person {
private:
char myStatus : 4;
char GradLevel : 4;
char marriageStatus: 4;
char myAge : 4;
char x1 : 4;
char x2 : 4;
};


On Mon, 7 Jun 2004, Victor Bazarov wrote:
Xiaobin Yang wrote:
Why is the Class size 4-bytes instead of 1 byte as I intend to ?

=================================
class BitTest {
public:
unsigned eightBits : 8;
};

int main()
{
cout << "size of BitTest: " << sizeof(BitTest) << endl;
}
==================================


Whatever your intentions are, the compiler may (a) not know about
them and (b) have its own agenda. While you can attempt to make
the class object a byte long by saying

class MyThingThatShouldBeOneCharLong {
char data;
};

the compiler is still allowed to pad the object to make it larger
for alignment purposes.

The compiler may not be all that evil as you might now think. See
the documentation for your compiler to learn about the ways to tell
it how to align your structures.

BTW, using 'unsigned' for bit fields causes the underlying member
to have size of 'unsigned int', which is probably 4 on your system.
You might try doing

unsigned char eightBits : 8;

but again, see what alignment options you have in your compiler.

Victor


-- xiaobin tel:(016 3) 21707.
Jul 22 '05 #4

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

Similar topics

18
by: Tarundeep | last post by:
hi, let us say it is a 32 bit processor then the size of the pointer is 32 bits. now i want to know what would be the size of the class with vtable pointer in it , that is it has few virtual...
2
by: kuhni | last post by:
Hi everybody! After searching newsgroups for a couple of hours, I now try asking directly (running the risk of asking again the same question). My problem is to predict when the size of the...
8
by: Pent | last post by:
Hi All, Why is this code valid? How can the static ctor be used? It doesn't act as class ctor at all. struct A { static A() { } }
6
by: Jon Jagger | last post by:
I was thinking about how you can only use sizeof to find the size of an unmanaged type. I started to wonder if there was a way to find the size of a managed object and came up with the following....
17
by: TheMadHatter | last post by:
yello, Quick Q: With regards to ram use, there really isnt any memory savings if I use type bool, vs int....... is there? If I remmeber correctly from a microcontroller course, the smallest...
7
by: krs | last post by:
Hi, I'm using the MS timetracker app as a basis for an application I am building. I have at present two classes, Site and SitesCollection, similar to the timeentry and timeentrycollection...
0
by: Nickneem | last post by:
I'm having a hard time trying to print a crystal report on a 1400 x 2970 (mm) custom form. I created a new form (start - settings - printers - server properties - create new form) 14 x 29,7 cm. In...
6
by: Salman | last post by:
I want to know abt the size of the class using sizeof function. i have pasted 2 programs. Both gives different sizes of the class just by re- arranging the order of the private varibles. Tell me...
11
by: mast2as | last post by:
This question has been posted to this forum before and I read the thread but found that the answers were perhaps imcomplete, so I am trying again. Whenever I am creating objects I would like to...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
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...

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.