Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

an enumeration based on char rather than an int

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

  • 3
  • 2
  • 2
  • +1
3 Solutions
yattiasAuthor Commented:
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?
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
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.

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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

yattiasAuthor Commented:
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?

yattiasAuthor Commented:
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
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.
>>when you said underlying type did you mean the type after the colon?

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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now