PDA

View Full Version : (OT), c++ question about bit fields.



LostInTheWoods
05-07-2003, 05:29 AM
Sorry for the OT question, but i have an inquisition about bit fields.

I have this.
struct BoolBit
{
bool Bit7 : 1;
bool Bit6 : 1;
bool Bit5 : 1;
bool Bit4 : 1;
bool Bit3 : 1;
bool Bit2 : 1;
bool Bit1 : 1;
bool Bit0 : 1;
};

is this legal?
struct BoolBit
{
bool Bit[8] : 1;
}

tfpsly
05-07-2003, 05:46 AM
Originally posted by LostInTheWoods:
is this legal?
struct BoolBit
{
bool Bit[8] : 1;
}

Nop, as you cannot get a pointer on each element. Table element must be accessible through pointers (as a table is just a pointer on a chunk of allocated memory).

Julien Cayzac
05-07-2003, 10:19 AM
Originally posted by LostInTheWoods:
Sorry for the OT question, but i have an inquisition about bit fields.


Even in plain C, bitfields should be avoided whenever possible, mainly for packing reasons. You don't know how your compiler will "optimize" that structure. Many compilers introduce non-standard __packed-like pragmas or attributes to deal with that, but using them is not a decent design decision.

BTW, you should ask such questions in a more adequate forum such as the ones at www.flipcode.com, (http://www.flipcode.com,) as this one is OpenGL related.

Julien.

SirKnight
05-07-2003, 12:16 PM
For C and C++ programming questions I really like the message board on http://www.cprogramming.com Their boards are split up into many different disscusion topics. Like C, C++, Windows Programming, etc etc. I have always liked that website.

-SirKnight