size of class that uses bit fields

X

Xiaobin Yang

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
 
V

Victor Bazarov

Xiaobin said:
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
 
M

Mike Wahler

Xiaobin Yang said:
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
 
X

Xiaobin Yang

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;
};




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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top