473,387 Members | 1,545 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,387 software developers and data experts.

More memory space needed for an Enum

Hey all.

I have bit shifted my enum values all the way to 31 items. I feel that
this api that i am using continues to grow, i will be stuck with out
any more items in my enum.

I would like to promote the enum to a long if possible so that it will
be 64 bits long. Thanks.

Oct 29 '06 #1
5 3407
<th**********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Hey all.

I have bit shifted my enum values all the way to 31 items. I feel that
this api that i am using continues to grow, i will be stuck with out
any more items in my enum.

I would like to promote the enum to a long if possible so that it will
be 64 bits long. Thanks.
You have more than 4 billion entries in your enum? A 32 bit enum can take
4294967296 different entries, how in the world have you used them all up?
Oct 29 '06 #2
Well I am not the one designing the API, Facebook is.

This is what I am doing
enum UserDetails
{
none = 0,
aboutMe = 1 << 0,
affiliations = 1 << 1,
birthday = 1 << 2,
books = 1 << 3,
clubs = 1 << 4,
currentLocation = 1 << 5,
....
workHistory = 1 << 30,
all = UINT_MAX
};

Oct 29 '06 #3
Would you be willing to help convert my code with me line by line to a
bitset? I am not sure exactly how they work and how they work like
enums.

Denise Kleingeist wrote:
Hello!
th**********@gmail.com wrote:
I have bit shifted my enum values all the way to 31 items. I feel that
this api that i am using continues to grow, i will be stuck with out
any more items in my enum.

I'd guess that you are best off changing the design of your API in the
first
place! If you think that your API is the best invention since sliced
bread,
you might want to consider std::bitset<nconstants instead of enums.

Good luck, Denise.
Oct 29 '06 #4
Hello!
th**********@gmail.com wrote:
Would you be willing to help convert my code with me line by line to a
bitset?
To answer you question first: no! People tend to pay to have me do
their
work and I'm currently not available for hiring. That said...

It sounds like you use your enum like a bitset, e.g.:

enum foobar
{
foo = 1 << 0,
bar = 1 << 1
// ...
};

.... and later implement logic depending on these values, e.g.

void f(foobar fb /*...*/)
{
if (fb & foo)
do_whatever_if_flag_foo_is_set();
if (fb & bar)
do_whatever_if_flag_bar_is_set();
}

Now, std:: bitset offers rather similar functionality, e.g.:

typedef std::bitset<352foobar;
foobar const foo = foobar(1) << 0;
foobar const bar = foobar(1) << 1;

Funnily enough, function f() above doesn't need any source changes,
only recompilation with the new definition of foobar which can now
support 352 different flags instead of just 32 (or whatever your
systems
maximum integer size is). The only real drawback of using std::bitset
instead of enums for this kind of stuff is that std::bitset cannot be
used
as the argument in a switch() while and enum can be used.

Good luck, Denise.

Oct 29 '06 #5
th**********@gmail.com wrote:
Hey all.

I have bit shifted my enum values all the way to 31 items. I feel that
this api that i am using continues to grow, i will be stuck with out
any more items in my enum.

I would like to promote the enum to a long if possible so that it will
be 64 bits long. Thanks.
I try to move totally away from enums and switch statements unless there
is a very select small number of them and even then I usually stick
enums into a class equivalent thing.

Firstly, the use of switch statements inevitably becomes way too complex
as you add new enums. So I use a static const reference to a class
instance that provides all the services for that "enum". This may take
a little more code and maybe less but I never have problems when I mix
bit masks of different things or have to go scrummaging through complex
code looking for switch statements just to make them more complex.

If I need to make collections of them I put them in a std::set or
similar container. I can also teach this container about valid sets of
things that can be added, sometimes even statically so the errors show
up at compile time rather than run time.

So code like this below is what I'm talking about. I've done a bit more
using templates in other code where the template type itself performs
compile time checks for invalid combinations. The compile time check
only works for simple combinations and dynamic combinations can only be
checked at run time.

BTW, as an observation, this is not unlike the "typeid" standard
implementation.
#include <set>
// the basic class than embodies the thing you're doing.
class Things
{
friend class Thing_Impl;

private:
Things();
Things( const Things & );
Things & operator=( const Things & );

public:
bool operator<( const Things & i_rhs ) const
{
return this < & i_rhs;
}

// some thing methods
virtual const char * Name() = 0;
// put more methods here instead of
// switch statements littered through the code

// nominate all the different things that you need
static const Things & Thing1;
static const Things & Thing2;
static const Things & Thing3;
};

// Then I might implement an operator| like so.
// first I need somthing for my std::set to hold
// a reference to a Things object reference.

class ThingsRef
{
public:

ThingsRef(
const Things & i_foo
)
: m_fooref( i_foo )
{
}

bool operator<( const ThingsRef & i_rhs ) const
{
return this->m_fooref < i_rhs.m_fooref;
}

const Things & m_fooref;
};
// my set of things type
class ThingSet
{
std::set<ThingsRef l_set;

public:
ThingSet(
const Things & i_thing
) {
l_set.insert( i_thing );
}

ThingSet(
const Things & i_lhs,
const Things & i_rhs
) {
l_set.insert( i_lhs );
l_set.insert( i_rhs );
}

ThingSet(
const ThingSet & i_lhs,
const Things & i_rhs
)
: l_set( i_lhs.l_set )
{
l_set.insert( i_rhs );
}

ThingSet(
const Things & i_lhs,
const ThingSet & i_rhs
)
: l_set( i_rhs.l_set )
{
l_set.insert( i_lhs );
}

ThingSet(
const ThingSet & i_lhs,
const ThingSet & i_rhs
)
: l_set( i_lhs.l_set )
{
l_set.insert( i_rhs.l_set.begin(), i_rhs.l_set.end() );
}

};
ThingSet operator|( const Things & i_lhs, const Things & i_rhs )
{
return ThingSet( i_lhs, i_rhs );
}

ThingSet operator|( const ThingSet & i_lhs, const Things & i_rhs )
{
return ThingSet( i_lhs, i_rhs );
}

ThingSet operator|( const Things & i_lhs, const ThingSet & i_rhs )
{
return ThingSet( i_lhs, i_rhs );
}

ThingSet operator|( const ThingSet & i_lhs, const ThingSet & i_rhs )
{
return ThingSet( i_lhs, i_rhs );
}
int main()
{
// Create a thingset from a single thing
ThingSet l_test1( Things::Thing1 );
ThingSet l_test2(
Things::Thing1 | Things::Thing2 | Things::Thing3
);
ThingSet l_test3( Things::Thing1 | l_test1);
ThingSet l_test4( l_test2 | l_test1 );

}
// in a totally separate file hidden from the rest of the world
//
//

Things::Things()
{
}

class Thing_Impl
: public Things
{
const char * name;

public:
Thing_Impl( const char * name )
: name( name )
{
}

const char * Name()
{
return name;
}

};

// implement all the different "Things"
//
const Thing_Impl Thing_Impl1( "Thingy 1" );
const Things & Things::Thing1 = Thing_Impl1;
const Thing_Impl Thing_Impl2( "Thingy 1" );
const Things & Things::Thing2 = Thing_Impl2;
const Thing_Impl Thing_Impl3( "Thingy 1" );
const Things & Things::Thing3 = Thing_Impl3;

Oct 29 '06 #6

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

Similar topics

4
by: Brad Tilley | last post by:
When memory usage is a concern, is it better to do: from X import Y or import X Also, is there a way to load and unload modules as they are needed. I have some scripts that sleep for...
8
by: Tron Thomas | last post by:
As part of applying for a programming position at a company, I recently I had submitted some code samples to one of the developers for review. This is the feedback I received: One of his...
18
by: Tron Thomas | last post by:
Given the following information about memory management in C++: ----- The c-runtime dynamic memory manager (and most other commercial memory managers) has issues with fragmentation similar to a...
1
by: picard | last post by:
I have seen in various posts that there are tricks to increasing the largest continuous memory block available to an application on a windows machine. I want to prove this is possible using a...
5
by: mikegw | last post by:
Hello all. I am currently using an implementation of sysV shared memory. The entire shared memory is allocated is one continuous block of which I get the pointer to the head, everything should...
11
by: Rodrigo Dominguez | last post by:
there are sometimes that I use third party libraries, I use some functions that returns char * or structs, etc. sometimes the memory that is returned by those libraries, when I try to free this...
10
by: Bonj | last post by:
I almost understand TSTs, to the point where I just need to know the answer to this: When making a TST (in C++) that will have as its leaf nodes words that make up SQL language and an categorising...
3
by: yxq | last post by:
Using the function below, i can get icon from a file to imagelist, but when exit my program, system will pop up a error box of "Memory can not Read", why? My system is Windows XP & sp2 ...
17
by: dtschoepe | last post by:
Hi, I have a homework project I am working on, so be forwarned, I'm new to C programming. But anyway, having some trouble with a memory allocation issue related to a char * that is a variable...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.