Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Which is the best way for holding flags?

Reply
Thread Tools

Which is the best way for holding flags?

 
 
loquak
Guest
Posts: n/a
 
      11-30-2004
Greets.
I wonder which way would be the best for holding some state flags in terms
of memory use and efficiency.

a)

struct FLAGS
{
bool flag1 : 1;
bool flag2 : 1;
bool flag3 : 1;
....
}
FLAGS flags;

b)

enum FLAGS
{
flag1,
flag2,
flag3
....
}
std::bitset<FLAGS> flags;

c)

bool flag1;
bool flag2;
bool flag3;
....

Which way would you prefer?


 
Reply With Quote
 
 
 
 
Jonathan Turkanis
Guest
Posts: n/a
 
      11-30-2004
"loquak" <(E-Mail Removed)> wrote in message
news:cogik6$u0s$(E-Mail Removed)...
> Greets.
> I wonder which way would be the best for holding some state flags in terms
> of memory use and efficiency.
>
> a)
>
> struct FLAGS
> {
> bool flag1 : 1;
> bool flag2 : 1;
> bool flag3 : 1;
> ...
> }


I hope it will not offend you if I say: Yuck.

> FLAGS flags;
>
> b)
>
> enum FLAGS
> {
> flag1,
> flag2,
> flag3
> ...
> }
> std::bitset<FLAGS> flags;


This is wrong: std::bitset takes a (non-type) std::size_t parameter.

> c)
>
> bool flag1;
> bool flag2;
> bool flag3;
> ...


This wastes space.

>
> Which way would you prefer?
>


d)

namespace flags {
const int flag1 = 1;
const int flag2 = flag1 << 1;
const int flag3 = flag2 << 1;
....
}

int flags;

Jonathan


 
Reply With Quote
 
 
 
 
Jonathan Turkanis
Guest
Posts: n/a
 
      11-30-2004

"Jonathan Turkanis" <(E-Mail Removed)> wrote:

> namespace flags {
> const int flag1 = 1;
> const int flag2 = flag1 << 1;
> const int flag3 = flag2 << 1;
> ....
> }
>
> int flags;


Whoops! I meant

int flags_;

Jonathan


 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      11-30-2004
loquak wrote:
> Greets.
> I wonder which way would be the best for holding some state flags in terms
> of memory use and efficiency.


- here, memory use and efficiency are 2 very different things.

It is undoubtedly faster to read and write bool or perhaps char values
than it is to read an write individual bits.

You need to ask yourself, is memory use more important than performance.

Now, if you have such large numbers of bits, such that you're trashing
cache all the time, you might find that a compact format is more
interesting.

Hence, I would probably choose a) if size was an issue, or c) if
performance was more important.

BTW - vector<bool> does employ a more compact format.

>
> a)
>
> struct FLAGS
> {
> bool flag1 : 1;
> bool flag2 : 1;
> bool flag3 : 1;
> ...
> }
> FLAGS flags;
>
> b)
>
> enum FLAGS
> {
> flag1,
> flag2,
> flag3
> ...
> }
> std::bitset<FLAGS> flags;
>
> c)
>
> bool flag1;
> bool flag2;
> bool flag3;
> ...
>
> Which way would you prefer?
>
>

 
Reply With Quote
 
Matthias =?ISO-8859-1?Q?K=E4ppler?=
Guest
Posts: n/a
 
      11-30-2004
Jonathan Turkanis wrote:

>
> "Jonathan Turkanis" <(E-Mail Removed)> wrote:
>
>> namespace flags {
>> const int flag1 = 1;
>> const int flag2 = flag1 << 1;
>> const int flag3 = flag2 << 1;
>> ....
>> }
>>
>> int flags;

>
> Whoops! I meant
>
> int flags_;
>
> Jonathan


What does the trailing underscore do? Or is it just a notation thing? Just
curious.
 
Reply With Quote
 
PKH
Guest
Posts: n/a
 
      11-30-2004

"Jonathan Turkanis" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Jonathan Turkanis" <(E-Mail Removed)> wrote:
>
>> namespace flags {
>> const int flag1 = 1;
>> const int flag2 = flag1 << 1;
>> const int flag3 = flag2 << 1;
>> ....
>> }
>>
>> int flags;

>
> Whoops! I meant
>
> int flags_;
>
> Jonathan
>
>


One technique I read in the faq that is nice if you have more flags than can
fit into an integer datatype :
http://www.parashift.com/c++-faq-lite/ctors.html [10.17] What is the "Named
Parameter Idiom"?

PKH


 
Reply With Quote
 
Jonathan Turkanis
Guest
Posts: n/a
 
      11-30-2004

"Matthias Käppler" <(E-Mail Removed)> wrote in message
news:coh8hl$63f$04$(E-Mail Removed)-online.com...
> Jonathan Turkanis wrote:
>
> >
> > "Jonathan Turkanis" <(E-Mail Removed)> wrote:
> >
> >> namespace flags {
> >> const int flag1 = 1;
> >> const int flag2 = flag1 << 1;
> >> const int flag3 = flag2 << 1;
> >> ....
> >> }
> >>
> >> int flags;

> >
> > Whoops! I meant
> >
> > int flags_;
> >
> > Jonathan

>
> What does the trailing underscore do? Or is it just a notation thing? Just
> curious.


I didn't want the namespace and the variable holding the flags to have the same
name. Usually the variable consisting of a combination of flags will be a data
member of some class; I usually call it "flags_".

Jonathan


 
Reply With Quote
 
Jonathan Turkanis
Guest
Posts: n/a
 
      11-30-2004

"PKH" <(E-Mail Removed)> wrote in message
news:VMVqd.19400$(E-Mail Removed)...
>
> "Jonathan Turkanis" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >
> > "Jonathan Turkanis" <(E-Mail Removed)> wrote:
> >
> >> namespace flags {
> >> const int flag1 = 1;
> >> const int flag2 = flag1 << 1;
> >> const int flag3 = flag2 << 1;
> >> ....
> >> }
> >>
> >> int flags;

> >
> > Whoops! I meant
> >
> > int flags_;
> >
> > Jonathan
> >
> >

>
> One technique I read in the faq that is nice if you have more flags than can
> fit into an integer datatype :
> http://www.parashift.com/c++-faq-lite/ctors.html [10.17] What is the "Named
> Parameter Idiom"?


David Abrahams and Daniel Wallin have a nice library which allows name
parameters, using syntax like:

Dog* d = new Dog(age = 3, weight = 45.1);

It was just reviewed for inclusion in Boost; the result is pending.

I don't really see the connection between this and bit flags, though.

Jonathan


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Which way is the best way to require another file in .rb Eddy Xu Ruby 5 04-11-2008 06:25 PM
Opinion: Optimal way of holding large amounts of data between pages? David ASP .Net 0 06-21-2006 08:08 PM
HtmlGenericControl not holding correct value.... Ralph Krausse ASP .Net 2 09-09-2004 06:20 PM
Holding Page for when Application Domain dies? I appreciate your help.... anony ASP .Net 2 12-22-2003 02:14 PM
Session problem? not holding Grant ASP .Net 3 08-14-2003 01:22 AM



Advertisments