473,505 Members | 13,982 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about creating a struct of flags in c++

Is there an efficient way to create a struct of flag in C++?

I need to create a struct of boolean flag, like this:
struct testStruct {
bool flag1;
bool flag2;
bool flag3;
bool flag4;
bool flag5;
bool flag6;
bool flag7;
bool flag8;
bool flag9;
bool flag10;
bool flag11;
bool flag12;
bool flag13;
};

But if I do that, i print out the sizeof(), that struct and it is 13.
So i think the compile use 1 byte for each flag.

Is it possible to create a struct so that each flag uses only 1 bit.

Thank you.
Oct 30 '08 #1
20 3382
Pl********@gmail.com wrote:
Is there an efficient way to create a struct of flag in C++?

I need to create a struct of boolean flag, like this:
struct testStruct {
bool flag1;
bool flag2;
bool flag3;
bool flag4;
bool flag5;
bool flag6;
bool flag7;
bool flag8;
bool flag9;
bool flag10;
bool flag11;
bool flag12;
bool flag13;
};

But if I do that, i print out the sizeof(), that struct and it is 13.
So i think the compile use 1 byte for each flag.

Is it possible to create a struct so that each flag uses only 1 bit.
Yes, read about bitfields. The syntax is the colon and the field width
after the name of the member, like

bool flag1:1;

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 30 '08 #2
On Oct 29, 11:10*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Plisske...@gmail.com wrote:
Is there an efficient way to create a struct of flag in C++?
I need to create a struct of boolean flag, like this:
struct testStruct {
* *bool flag1;
* *bool flag2;
* *bool flag3;
* *bool flag4;
* *bool flag5;
* *bool flag6;
* *bool flag7;
* *bool flag8;
* *bool flag9;
* *bool flag10;
* *bool flag11;
* *bool flag12;
* *bool flag13;
};
But if I do that, i print out the sizeof(), that struct and it is 13.
So i think the compile use 1 byte for each flag.
Is it possible to create a struct so that each flag uses only 1 bit.

Yes, read about bitfields. *The syntax is the colon and the field width
after the name of the member, like

* * *bool flag1:1;

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thank you Victor.

I am going to follow you suggestion. If I create a class like this:
class myMask {
public:
bool flag1:1;
bool flag2:1;
bool flag3:1;
bool flag4:1;
bool flag5:1;
bool flag6:1;
bool flag7:1;
bool flag8:1;
bool flag9:1;
bool flag10:1;
bool flag11:1;
bool flag12:1;
bool flag13:1;
};

Can I set all the flag to 0 by doing this:

myMask mask;

memset(&mask, '\0', sizeof(myMask));

And can I compare if 2 masks are the same by doing this:

myMask mask1;
myMask mask2;

memcmp(&mask1, &mask2, sizeof(myMask));

Thanks.
Oct 30 '08 #3
On 10ÔÂ30ÈÕ, ÏÂÎç1ʱ50·Ö, "Plisske...@gmail.com" <Plisske...@gmail.com>
wrote:
On Oct 29, 11:10 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:


Plisske...@gmail.com wrote:
Is there an efficient way to create a struct of flag in C++?
I need to create a struct of boolean flag, like this:
struct testStruct {
bool flag1;
bool flag2;
bool flag3;
bool flag4;
bool flag5;
bool flag6;
bool flag7;
bool flag8;
bool flag9;
bool flag10;
bool flag11;
bool flag12;
bool flag13;
};
But if I do that, i print out the sizeof(), that struct and it is 13.
So i think the compile use 1 byte for each flag.
Is it possible to create a struct so that each flag uses only 1 bit.
Yes, read about bitfields. The syntax is the colon and the field width
after the name of the member, like
bool flag1:1;
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Thank you Victor.

I am going to follow you suggestion. If I create a class like this:
class myMask {
public:
bool flag1:1;
bool flag2:1;
bool flag3:1;
bool flag4:1;
bool flag5:1;
bool flag6:1;
bool flag7:1;
bool flag8:1;
bool flag9:1;
bool flag10:1;
bool flag11:1;
bool flag12:1;
bool flag13:1;
};

Can I set all the flag to 0 by doing this:

myMask mask;

memset(&mask, '\0', sizeof(myMask));

And can I compare if 2 masks are the same by doing this:

myMask mask1;
myMask mask2;

memcmp(&mask1, &mask2, sizeof(myMask));

Thanks.- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒýÓõÄÎÄ×Ö -
there are two things to say:
1, you need no class, but struct, because struct is good enough to
solve it;
2, either class or struct, you could use ctor where all the bit fields
are set to 0 or any value you want.
Oct 30 '08 #4
Hisense wrote:
>>
I am going to follow you suggestion. If I create a class like this:
class myMask {
public:
bool flag1:1;
bool flag2:1;
bool flag3:1;
bool flag4:1;
bool flag5:1;
bool flag6:1;
bool flag7:1;
bool flag8:1;
bool flag9:1;
bool flag10:1;
bool flag11:1;
bool flag12:1;
bool flag13:1;
};

Can I set all the flag to 0 by doing this:

myMask mask;

memset(&mask, '\0', sizeof(myMask));
Proper way is:

myMask mask = myMask();
>And can I compare if 2 masks are the same by doing this:

myMask mask1;
myMask mask2;

memcmp(&mask1, &mask2, sizeof(myMask));
No, because of padding, some bits can be left uninitialized, therefore
you have to compare each field.
I am not sure if you can do this (probably not)
if(mask1 == mask2)
{
....
}
2, either class or struct, you could use ctor where all the bit fields
are set to 0 or any value you want.
POD structures can not have constructors. Read here:
http://www.fnal.gov/docs/working-gro...x/doc/POD.html
Oct 30 '08 #5
On Oct 30, 6:50 am, "Plisske...@gmail.com" <Plisske...@gmail.com>
wrote:
On Oct 29, 11:10 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Plisske...@gmail.com wrote:
Is there an efficient way to create a struct of flag in C++?
I need to create a struct of boolean flag, like this:
struct testStruct {
bool flag1;
bool flag2;
bool flag3;
bool flag4;
bool flag5;
bool flag6;
bool flag7;
bool flag8;
bool flag9;
bool flag10;
bool flag11;
bool flag12;
bool flag13;
};
But if I do that, i print out the sizeof(), that struct and it is 13.
So i think the compile use 1 byte for each flag.
Is it possible to create a struct so that each flag uses only 1 bit.
Yes, read about bitfields. The syntax is the colon and the field width
after the name of the member, like
bool flag1:1;
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Thank you Victor.

I am going to follow you suggestion. If I create a class like this:
class myMask {
public:
bool flag1:1;
bool flag2:1;
bool flag3:1;
bool flag4:1;
bool flag5:1;
bool flag6:1;
bool flag7:1;
bool flag8:1;
bool flag9:1;
bool flag10:1;
bool flag11:1;
bool flag12:1;
bool flag13:1;
};

Can I set all the flag to 0 by doing this:

myMask mask;

memset(&mask, '\0', sizeof(myMask));

And can I compare if 2 masks are the same by doing this:

myMask mask1;
myMask mask2;

memcmp(&mask1, &mask2, sizeof(myMask));

Thanks.
Oct 30 '08 #6
Pl********@gmail.com wrote:
[..] If I create a class like this:
class myMask {
public:
bool flag1:1;
bool flag2:1;
bool flag3:1;
bool flag4:1;
bool flag5:1;
bool flag6:1;
bool flag7:1;
bool flag8:1;
bool flag9:1;
bool flag10:1;
bool flag11:1;
bool flag12:1;
bool flag13:1;
};

Can I set all the flag to 0 by doing this:

myMask mask;
That will leave them undefined. Consider defining the default
constructor and the comparison operator.
>
memset(&mask, '\0', sizeof(myMask));

And can I compare if 2 masks are the same by doing this:

myMask mask1;
myMask mask2;

memcmp(&mask1, &mask2, sizeof(myMask));
It is better to have the overloaded operator== in your class.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 30 '08 #7
On Oct 30, 9:22*am, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Plisske...@gmail.com wrote:
[..] *If I create a class like this:
class myMask {
public:
* * *bool flag1:1;
* * *bool flag2:1;
* * *bool flag3:1;
* * *bool flag4:1;
* * *bool flag5:1;
* * *bool flag6:1;
* * *bool flag7:1;
* * *bool flag8:1;
* * *bool flag9:1;
* * *bool flag10:1;
* * *bool flag11:1;
* * *bool flag12:1;
* * *bool flag13:1;
* };
Can I set all the flag to 0 by doing this:
* * myMask mask;

That will leave them undefined. *Consider defining the default
constructor and the comparison operator.
* * memset(&mask, '\0', sizeof(myMask));
And can I compare if 2 masks are the same by doing this:
myMask mask1;
myMask mask2;
memcmp(&mask1, &mask2, sizeof(myMask));

It is better to have the overloaded operator== in your class.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thank you for all the help.

In my case, I have this class which only have
~200 boolean flags. I am thinking if there is a faster way to
* initialize all of them to false
* compare if 2 classes have the same values (the same set of flags are
set)

Thank you again.
Oct 30 '08 #8
On 30 Okt., 06:50, "Plisske...@gmail.com" <Plisske...@gmail.com>
wrote:
On Oct 29, 11:10*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Plisske...@gmail.com wrote:
Is there an efficient way to create a struct of flag in C++?
I need to create a struct of boolean flag, like this:
struct testStruct {
* *bool flag1;
* *bool flag2;
* *bool flag3;
* *bool flag4;
* *bool flag5;
* *bool flag6;
* *bool flag7;
* *bool flag8;
* *bool flag9;
* *bool flag10;
* *bool flag11;
* *bool flag12;
* *bool flag13;
};
But if I do that, i print out the sizeof(), that struct and it is 13.
So i think the compile use 1 byte for each flag.
Is it possible to create a struct so that each flag uses only 1 bit.
Yes, read about bitfields. *The syntax is the colon and the field width
after the name of the member, like
* * *bool flag1:1;
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Thank you Victor.

I am going to follow you suggestion. If I create a class like this:
class myMask {
public:
* * *bool flag1:1;
* * *bool flag2:1;
* * *bool flag3:1;
* * *bool flag4:1;
* * *bool flag5:1;
* * *bool flag6:1;
* * *bool flag7:1;
* * *bool flag8:1;
* * *bool flag9:1;
* * *bool flag10:1;
* * *bool flag11:1;
* * *bool flag12:1;
* * *bool flag13:1;
* };

Can I set all the flag to 0 by doing this:

* * myMask mask;

* * memset(&mask, '\0', sizeof(myMask));

And can I compare if 2 masks are the same by doing this:

myMask mask1;
myMask mask2;

memcmp(&mask1, &mask2, sizeof(myMask));
try this:

struct myMask {
union {
unsigned int nMask;
struct {
bool flag1:1;
bool flag2:1;
bool flag3:1;
bool flag4:1;
bool flag5:1;
bool flag6:1;
bool flag7:1;
bool flag8:1;
bool flag9:1;
bool flag10:1;
bool flag11:1;
bool flag12:1;
bool flag13:1;
};
};
};

inline bool operator==(myMask const& rm, myMask const& lm)
{return rm.nMask == lm.nMask;};

the union will force your compiler to put all the bits into one int.
you can the set the int to 0 to clear all bits and compare the ints to
compare the masks.

Just a hint: you'll never need '/0'. just write 0. '/0' is just used
to inform the readers of your code: "hey folks, this is explicitely
meant as a char in a string". so if you want to do something weired:

int i = 20;
while (i-->'\0')
{
if (i == '/2')
{
CallSomeMessageFunc("I've reached 2!");
}
}
Oct 30 '08 #9
On 30 Okt., 16:03, ".rhavin grobert" <cl...@yahoo.dewrote:
On 30 Okt., 06:50, "Plisske...@gmail.com" <Plisske...@gmail.com>
wrote:
On Oct 29, 11:10*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Plisske...@gmail.com wrote:
Is there an efficient way to create a struct of flag in C++?
I need to create a struct of boolean flag, like this:
struct testStruct {
* *bool flag1;
* *bool flag2;
* *bool flag3;
* *bool flag4;
* *bool flag5;
* *bool flag6;
* *bool flag7;
* *bool flag8;
* *bool flag9;
* *bool flag10;
* *bool flag11;
* *bool flag12;
* *bool flag13;
};
But if I do that, i print out the sizeof(), that struct and it is 13.
So i think the compile use 1 byte for each flag.
Is it possible to create a struct so that each flag uses only 1 bit..
Yes, read about bitfields. *The syntax is the colon and the field width
after the name of the member, like
* * *bool flag1:1;
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thank you Victor.
I am going to follow you suggestion. If I create a class like this:
class myMask {
public:
* * *bool flag1:1;
* * *bool flag2:1;
* * *bool flag3:1;
* * *bool flag4:1;
* * *bool flag5:1;
* * *bool flag6:1;
* * *bool flag7:1;
* * *bool flag8:1;
* * *bool flag9:1;
* * *bool flag10:1;
* * *bool flag11:1;
* * *bool flag12:1;
* * *bool flag13:1;
* };
Can I set all the flag to 0 by doing this:
* * myMask mask;
* * memset(&mask, '\0', sizeof(myMask));
And can I compare if 2 masks are the same by doing this:
myMask mask1;
myMask mask2;
memcmp(&mask1, &mask2, sizeof(myMask));

try this:

struct myMask {
* union {
* * unsigned int nMask;
* * struct {
* * * bool flag1:1;
* * * bool flag2:1;
* * * bool flag3:1;
* * * bool flag4:1;
* * * bool flag5:1;
* * * bool flag6:1;
* * * bool flag7:1;
* * * bool flag8:1;
* * * bool flag9:1;
* * * bool flag10:1;
* * * bool flag11:1;
* * * bool flag12:1;
* * * bool flag13:1;
* *};
* };

};

inline bool operator==(myMask const& rm, myMask const& lm)
{return rm.nMask == lm.nMask;};

the union will force your compiler to put all the bits into one int.
you can the set the int to 0 to clear all bits and compare the ints to
compare the masks.

Just a hint: you'll never need '/0'. just write 0. '/0' is just used
to inform the readers of your code: "hey folks, this is explicitely
meant as a char in a string". so if you want to do something weired:

int i = 20;
while (i-->'\0')
{
* if (i == '/2')
* {
* * CallSomeMessageFunc("I've reached 2!");
* }

}

typo: " if (i == '/2') " should be " if (i == '\2') "
Oct 30 '08 #10

It's not exactly on point, but you might be
interested in this article about doing
efficient, typesafe bit-twiddling
with C++: http://accu.org/index.php/journals/281

Sean
Oct 30 '08 #11
Pl********@gmail.com wrote:
On Oct 30, 9:22 am, Victor Bazarov <v.Abaza...@comAcast.netwrote:
>Plisske...@gmail.com wrote:
>>[..] If I create a class like this:
class myMask {
public:
bool flag1:1;
bool flag2:1;
bool flag3:1;
bool flag4:1;
bool flag5:1;
bool flag6:1;
bool flag7:1;
bool flag8:1;
bool flag9:1;
bool flag10:1;
bool flag11:1;
bool flag12:1;
bool flag13:1;
};
Can I set all the flag to 0 by doing this:
myMask mask;
That will leave them undefined. Consider defining the default
constructor and the comparison operator.
>> memset(&mask, '\0', sizeof(myMask));
And can I compare if 2 masks are the same by doing this:
myMask mask1;
myMask mask2;
memcmp(&mask1, &mask2, sizeof(myMask));
It is better to have the overloaded operator== in your class.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Thank you for all the help.

In my case, I have this class which only have
~200 boolean flags. I am thinking if there is a faster way to
* initialize all of them to false
* compare if 2 classes have the same values (the same set of flags are
set)

Thank you again.
What you could do is have a static object of the same class that you'll
use as the prototype for initialising, and have the proper padding to
ensure that all data members are initialised:

struct myLargeClass {
bool blah : 1; // 1st bit
bool clah : 1; // 2nd bit
...
bool zlah : 1; // 25th bit
unsigned padding : 7; // to bring it to 32 bits total

static myLargeClass prototype;
myLargeClass() {
if (this != &prototype)
memset(this, &prototype, sizeof *this);
}
};

Statics are always zero-initialised, so 'prototype' is going to contain
all zeros. Then you can always compare objects using 'memcmp' since
there will be no uninitialised bits.

Don't forget to define 'prototype' somewhere in your implementation file.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 30 '08 #12
On Oct 30, 12:26*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Plisske...@gmail.com wrote:
On Oct 30, 9:22 am, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Plisske...@gmail.com wrote:
[..] *If I create a class like this:
class myMask {
public:
* * *bool flag1:1;
* * *bool flag2:1;
* * *bool flag3:1;
* * *bool flag4:1;
* * *bool flag5:1;
* * *bool flag6:1;
* * *bool flag7:1;
* * *bool flag8:1;
* * *bool flag9:1;
* * *bool flag10:1;
* * *bool flag11:1;
* * *bool flag12:1;
* * *bool flag13:1;
* };
Can I set all the flag to 0 by doing this:
* * myMask mask;
That will leave them undefined. *Consider defining the default
constructor and the comparison operator.
>* * memset(&mask, '\0', sizeof(myMask));
And can I compare if 2 masks are the same by doing this:
myMask mask1;
myMask mask2;
memcmp(&mask1, &mask2, sizeof(myMask));
It is better to have the overloaded operator== in your class.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thank you for all the help.
In my case, I have this class which only have
~200 boolean flags. I am thinking if there is a faster way to
* initialize all of them to false
* compare if 2 classes have the same values (the same set of flags are
set)
Thank you again.

What you could do is have a static object of the same class that you'll
use as the prototype for initialising, and have the proper padding to
ensure that all data members are initialised:

* * struct myLargeClass {
* * * *bool blah : 1; * * * *// 1st bit
* * * *bool clah : 1; * * * *// 2nd bit
* * * *...
* * * *bool zlah : 1; * * * *// 25th bit
* * * *unsigned padding : 7; // to bring it to 32 bits total

* * * *static myLargeClass prototype;
* * * *myLargeClass() {
* * * * * if (this != &prototype)
* * * * * * *memset(this, &prototype, sizeof *this);
* * * *}
* * };

Statics are always zero-initialised, so 'prototype' is going to contain
all zeros. *Then you can always compare objects using 'memcmp' since
there will be no uninitialised bits.

Don't forget to define 'prototype' somewhere in your implementation file.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thank you for this great idea.
I have 2 questions:
1. Do I have to pad it to a multiple of 32?
If I have 153 bool flags, do I pad it with 25 bits? 153 - (32 * 4) =
25

2. I don't understand "what is define 'prototype' somewhere in your
implementation file"?
Isn't it already defined here:
struct myLargeClass {
//...
static myLargeClass prototype;
//...
}

Thank you.
Oct 30 '08 #13
Victor Bazarov wrote:
static myLargeClass prototype;
myLargeClass() {
if (this != &prototype)
memset(this, &prototype, sizeof *this);
What's wrong with "*this = prototype;"?
Oct 30 '08 #14
Juha Nieminen wrote:
Victor Bazarov wrote:
> static myLargeClass prototype;
myLargeClass() {
if (this != &prototype)
memset(this, &prototype, sizeof *this);

What's wrong with "*this = prototype;"?
*Much* better actually, as the language rules forbid using memset on a
class or struct that has a constructor.
Bo Persson
Oct 30 '08 #15
Pl********@gmail.com wrote:
[..]
I have 2 questions:
1. Do I have to pad it to a multiple of 32?
No. It's up to your platform. You need to find out what the sizeof
yields for your struct with only the used flags there, and then add as
many bitfields as needed.

Essentially, you need to do

unsigned padding : ( sizeof(myLargeClass) * CHAR_BITS - Nflags );
If I have 153 bool flags, do I pad it with 25 bits? 153 - (32 * 4) =
25
If your system wants to create an object with size divisible by 16, say,
you will only need to add 9...
>
2. I don't understand "what is define 'prototype' somewhere in your
implementation file"?
Isn't it already defined here:
struct myLargeClass {
//...
static myLargeClass prototype;
No, here it's only *declared*. Read up on static data members.
//...
}

Thank you.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 30 '08 #16
On Oct 30, 4:03 pm, "Plisske...@gmail.com" <Plisske...@gmail.com>
wrote:
On Oct 30, 9:22 am, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Plisske...@gmail.com wrote:
[..] If I create a class like this:
class myMask {
public:
bool flag1:1;
bool flag2:1;
bool flag3:1;
bool flag4:1;
bool flag5:1;
bool flag6:1;
bool flag7:1;
bool flag8:1;
bool flag9:1;
bool flag10:1;
bool flag11:1;
bool flag12:1;
bool flag13:1;
};
Can I set all the flag to 0 by doing this:
myMask mask;
That will leave them undefined. Consider defining the
default constructor and the comparison operator.
memset(&mask, '\0', sizeof(myMask));
And can I compare if 2 masks are the same by doing this:
myMask mask1;
myMask mask2;
memcmp(&mask1, &mask2, sizeof(myMask));
It is better to have the overloaded operator== in your class.
In my case, I have this class which only have
~200 boolean flags. I am thinking if there is a faster way to
* initialize all of them to false
Faster than what? I'd expect the compiler to use the fastest
means available on the target hardware for myMask().
* compare if 2 classes have the same values (the same set of flags are
set)
Regretfully, there's not real way to compare the values other
than one by one. The compiler is free to introduce padding
where ever it wants. In practice, if you arrange for the total
number of bits to be a multiple of your machine's word size
(with an unsigned pad:n at the end, where n is whatever is
needed), and ensure that the added field is always 0, memcmp
will almost certainly work, even if the standard doesn't
guarantee it.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 31 '08 #17
On Oct 30, 4:03 pm, ".rhavin grobert" <cl...@yahoo.dewrote:
On 30 Okt., 06:50, "Plisske...@gmail.com" <Plisske...@gmail.com>
wrote:
[...]
And can I compare if 2 masks are the same by doing this:
myMask mask1;
myMask mask2;
memcmp(&mask1, &mask2, sizeof(myMask));
try this:
struct myMask {
union {
unsigned int nMask;
struct {
bool flag1:1;
bool flag2:1;
bool flag3:1;
bool flag4:1;
bool flag5:1;
bool flag6:1;
bool flag7:1;
bool flag8:1;
bool flag9:1;
bool flag10:1;
bool flag11:1;
bool flag12:1;
bool flag13:1;
};
};
};
inline bool operator==(myMask const& rm, myMask const& lm)
{return rm.nMask == lm.nMask;};
Lot's of problems with that.

-- It's not legal C++. Only unions can be anonymous, not
structs.

-- You'll almost certainly end up with undefined behavior using
it; you can't access the nMask element unless it was the
last one written to.

-- If you've written to the flags structure (which you haven't
named), the padding bits may take on any arbitrary values.
Which mean that two equal myMask may have unequald nMask.

-- For up to 16 flags, you're OK, but for more, the unsigned
may be smaller than the struct.
the union will force your compiler to put all the bits into
one int.
Not really.
you can the set the int to 0 to clear all bits and compare the
ints to compare the masks.
If the int is large enough, this will probably work *IF* he
makes sure that any unused bits in the struct are always 0.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 31 '08 #18
Victor Bazarov wrote:
Pl********@gmail.com wrote:
>On Oct 30, 9:22 am, Victor Bazarov <v.Abaza...@comAcast.netwrote:
>>Plisske...@gmail.com wrote:
[..] If I create a class like this:
class myMask {
public:
bool flag1:1;
bool flag2:1;
bool flag3:1;
bool flag4:1;
bool flag5:1;
bool flag6:1;
bool flag7:1;
bool flag8:1;
bool flag9:1;
bool flag10:1;
bool flag11:1;
bool flag12:1;
bool flag13:1;
};
Can I set all the flag to 0 by doing this:
myMask mask;
That will leave them undefined. Consider defining the default
constructor and the comparison operator.

memset(&mask, '\0', sizeof(myMask));
And can I compare if 2 masks are the same by doing this:
myMask mask1;
myMask mask2;
memcmp(&mask1, &mask2, sizeof(myMask));
It is better to have the overloaded operator== in your class.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thank you for all the help.

In my case, I have this class which only have
~200 boolean flags. I am thinking if there is a faster way to
* initialize all of them to false
* compare if 2 classes have the same values (the same set of flags are
set)

Thank you again.

What you could do is have a static object of the same class that you'll
use as the prototype for initialising, and have the proper padding to
ensure that all data members are initialised:

struct myLargeClass {
bool blah : 1; // 1st bit
bool clah : 1; // 2nd bit
...
bool zlah : 1; // 25th bit
unsigned padding : 7; // to bring it to 32 bits total

static myLargeClass prototype;
myLargeClass() {
if (this != &prototype)
memset(this, &prototype, sizeof *this);
}
};

Statics are always zero-initialised, so 'prototype' is going to contain
all zeros. Then you can always compare objects using 'memcmp' since
there will be no uninitialised bits.

Don't forget to define 'prototype' somewhere in your implementation file.
While this works, I wonder what's wrong with 'std::bitset'
that you prefer to do it manually? (I have never used it,
so I don't know what its disadvantages are.)
V
Schobi
Oct 31 '08 #19
On Oct 30, 12:26*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Plisske...@gmail.com wrote:
On Oct 30, 9:22 am, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Plisske...@gmail.com wrote:
[..] *If I create a class like this:
class myMask {
public:
* * *bool flag1:1;
* * *bool flag2:1;
* * *bool flag3:1;
* * *bool flag4:1;
* * *bool flag5:1;
* * *bool flag6:1;
* * *bool flag7:1;
* * *bool flag8:1;
* * *bool flag9:1;
* * *bool flag10:1;
* * *bool flag11:1;
* * *bool flag12:1;
* * *bool flag13:1;
* };
Can I set all the flag to 0 by doing this:
* * myMask mask;
That will leave them undefined. *Consider defining the default
constructor and the comparison operator.
>* * memset(&mask, '\0', sizeof(myMask));
And can I compare if 2 masks are the same by doing this:
myMask mask1;
myMask mask2;
memcmp(&mask1, &mask2, sizeof(myMask));
It is better to have the overloaded operator== in your class.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thank you for all the help.
In my case, I have this class which only have
~200 boolean flags. I am thinking if there is a faster way to
* initialize all of them to false
* compare if 2 classes have the same values (the same set of flags are
set)
Thank you again.

What you could do is have a static object of the same class that you'll
use as the prototype for initialising, and have the proper padding to
ensure that all data members are initialised:

* * struct myLargeClass {
* * * *bool blah : 1; * * * *// 1st bit
* * * *bool clah : 1; * * * *// 2nd bit
* * * *...
* * * *bool zlah : 1; * * * *// 25th bit
* * * *unsigned padding : 7; // to bring it to 32 bits total

* * * *static myLargeClass prototype;
* * * *myLargeClass() {
* * * * * if (this != &prototype)
* * * * * * *memset(this, &prototype, sizeof *this);
* * * *}
* * };

Statics are always zero-initialised, so 'prototype' is going to contain
all zeros. *Then you can always compare objects using 'memcmp' since
there will be no uninitialised bits.

Don't forget to define 'prototype' somewhere in your implementation file.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thank you for all the response on this thread.

I have 1 more question.
With this idea, I can use 'memset' to clear all the flags in the
structs.
And I can use 'memcmp' to compare flags in 2 different structs.

What can I do to 'OR' the 2 structs of flags?
I know i can do this for each flag of struct:
s1.flag1 |= s2.flag1;
s1.flag2 |= s2.flag2;
s1.flag3 |= s2.flag3;

But is there a better way?

Thank you.
Nov 1 '08 #20
Pl********@gmail.com wrote:
[..]
I have 1 more question.
With this idea, I can use 'memset' to clear all the flags in the
structs.
You're better off just assigning from a prototype. 'memset' is
not the best solution.
And I can use 'memcmp' to compare flags in 2 different structs.

What can I do to 'OR' the 2 structs of flags?
I know i can do this for each flag of struct:
s1.flag1 |= s2.flag1;
s1.flag2 |= s2.flag2;
s1.flag3 |= s2.flag3;

But is there a better way?
I don't know of any. With bitfields like you want, you will
probably have to change them manually like this. Don't yield
to the temptation to use a union. There was a suggestion to
use a bitset instead, take it into cosideration.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 2 '08 #21

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

Similar topics

3
1402
by: ma740988 | last post by:
Consider the 'C' source. void myDoorBellISR(starLinkDevice *slDevice, U32 doorBellVal) { doorBellDetected = doorBellVal; } void slRcv() { starLinkOpenStruct myOpenStruct;
36
7716
by: Bhalchandra Thatte | last post by:
I am allocating a block of memory using malloc. I want to use it to store a "header" structure followed by structs in my application. How to calculate the alignment without making any assumption...
6
2179
by: Arthur J. O'Dwyer | last post by:
As far as I know, C89/C90 did not contain the now-standard offsetof() macro. Did C89 mandate that structs had to have a consistent layout? For example, consider the typical layout of the...
7
7834
by: mike | last post by:
Hi, I am having difficulty in creating a thread using pthread_create. It seems that pthread_create does not execute 'program', and returns -1; I have checked the API but I am not sure why this...
11
1652
by: nishant | last post by:
I've a structure: struct msghdr { //some goes here struct iovec *msg_iov; //some more --- }
18
2423
by: Peter Smithson | last post by:
Hi, I've read this page - http://devrsrc1.external.hp.com/STK/impacts/i634.html but don't understand it. Here's the text - "Non-standard usage of setjmp() and longjmp() could result in...
7
4490
by: Daniel Rudy | last post by:
Is the following code legal? typedef stuct data_type_tag { int var1; int var2; int var3; struct { void *p; size_t size; unsigned int flags;
10
2251
by: Giovanni Bajo | last post by:
Hello, given the ongoing work on struct (which I thought was a dead module), I was wondering if it would be possible to add an API to register custom parsing codes for struct. Whenever I use it...
12
4611
by: djhong | last post by:
Following is a snippet of a header file. Here, #define are inside struct evConn{} Any advantage of putting them inside struct block? Looks like there is no diff in scoping of each...
0
7098
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
7303
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
7367
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...
1
7018
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
7471
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...
0
4699
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...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1528
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 ...
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.