Solved

an enumeration based on char rather than an int

Posted on 2006-07-07
11
242 Views
Last Modified: 2010-04-24
Hey, I read that it is possible to base an enumeration on other data types rather than an int to save memory.  each member of an int based enumeration takes 32 bit and according to my book if you base it on a char, everything will fit into one byte.

__value enum WeekDay : char
{
     Monday = 1, Tuesday..... , Sunday
};

can anyone explains me what exactly happens here and how does the compiler know to base it on an int? the notation seems like the notation for deriving a class from a base class

thanks
0
Comment
Question by:yattias
  • 3
  • 2
  • 2
  • +1
11 Comments
 

Author Comment

by:yattias
ID: 17062081
oh and in a regular enumeration, each member takes four bytes right? so does that mean that in the one i listed above, each member would take 1?
0
 
LVL 15

Accepted Solution

by:
lakshman_ce earned 134 total points
ID: 17063443
A __value enum is similar to an ordinary C++ enumerated type except for the following:

A __value enum can specify an underlying type.
By default pointer-to rules, a pointer-to a __value enum type is a __gc pointer.

The declaration and behavior of enums has changed from Managed Extensions for C++ to Visual C++ 2005.

The Managed Extensions enum declaration is preceded by the __value keyword. The idea here is to distinguish the native enum from the CLR enum which is derived from System::ValueType, while suggesting an analogous functionality.

Specifying the type is an additional info to call the right enum through functions. Please refer to this link to understand more about all these
http://msdn2.microsoft.com/en-us/library/ms235243.aspx
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 133 total points
ID: 17064782
You could encode it bitwise (binary flags).
and int can then store up to 32 possibilities (for the 32 bit version of an int to be precise).

You can test with the binary and (&)   operator for instance.

crudely
enum eFirst = 0x01, eSecond = 0x02, eThird = 0x04, eFourth = 0x08....

int iValue = 0x05;  // eFirst + eThird
iValue & eFirst     //TRUE
iValue & eSecond //FALSE
iValue & eThird    //TRUE
iValue & eFourth  //FALSE
....
0
 

Author Comment

by:yattias
ID: 17065393
reply to comment from lakshman_ce

>A __value enum is similar to an ordinary C++ enumerated type except for the following:
>
>A __value enum can specify an underlying type.
>By default pointer-to rules, a pointer-to a __value enum type is a __gc pointer


when you said underlying type did you mean the type after the colon?

                 __value enum WeekDay : char (in this case the underlying type would be char right?)
and if so, you could specify an underlying type only in __value types and not in regular c++ enumerations?

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:yattias
ID: 17065406
reply to comment from AndyAinscow

>enum eFirst = 0x01, eSecond = 0x02, eThird = 0x04, eFourth = 0x08....

I kinda see what you did here with the hexadecimal but not quite completely, could you explain please? Lets say I want to create an instance of eFirst, how would I access the types (eFirst, eSecond) and so on? what values would they have? thanks
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 17065852
The hex values are the values you use.  The 'result' is made up of various binary values.  There are various binary comparison operators available (and, or, exlusive or...).
What the bitwise comparison does is just pack certain values (eg. flags..open closed, paid, not paid....) into ONE variable.  re the first part of you question about saving space.
0
 
LVL 15

Expert Comment

by:lakshman_ce
ID: 17067485
>>when you said underlying type did you mean the type after the colon?

Exactly.
0
 
LVL 49

Assisted Solution

by:DanRollins
DanRollins earned 133 total points
ID: 17111525
Good "old-fashioned" enum values are always int, but they work a lot like a #define, that is, when the compiler sees it, it uses whatever datatype is in the context.

With that in mind, consider that the actual bit-length of the enum is often irrelevant.  For instance,

      enum WeekDay {  Monday = 1, Tuesday=2, Sunday=7 };

      BYTE DayOfWeek= 1;
      if ( DayOfWeek == Monday ) {
            ...
      }

The compiler will use a one-byte comparison.  The generated code will be optimal and storage space for the variable will be minimized.

In trying to understand your question... it occurs to me that you may have an array (or other memory consuming object) and you want to avoid using up four bytes each time your enum is used.  In that case, just define the value as a one-byte value in the array, and let the compiler use its default conversion logic.
 
      enum WeekDay {  Monday = 1, Tuesday=2, Sunday=7 };
      enum Month {  January= 1, February=2, December=12 };

      struct MyStruct {
            char szName[20];
            WeekDay eDay;     // four bytes
            Month   eMonth;   // four bytes
      };

      struct MyStruct2 {
            char szName[20];
            BYTE eDay;       // one byte
            BYTE eMonth;     // one byte
      };

      MyStruct r;
      r.eDay=  Monday;     // a four-byte data move

      MyStruct2 r2;
      r2.eDay=  Monday;     // a one-byte data move

The second structure stores the data more efficiently.  If you have an array of 1000 of these, the second method will save you 6000 bytes or memory (three bytes for each eDay and three more for each eMonth).  The savings is in memory space... there is often NO advantage in speed since under the covers, the CPU actually has to do *extra* work to isolate a single byte when it is on an odd (non-DWORD) memory address.

-- Dan
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now