6
\$\begingroup\$

For digital electronic circuit simulation, i wanted to implement Three states. i.e High, Low and Undefined in C++.

I saw Boost::tribool, and they implement it using enum

Now for conserving some memory i have implemented it using 1 Byte Int

#ifdef _WINDOWS
typedef __int8 State;
#elif
typedef int8_t State;
#endif

const State kLow = 0;
const State kHigh = 1;
const State kUndefined = 2;
  1. Do you see any potential problem with it?
  2. And why would one waste 4 bytes instead of a single byte for 3 values only
  3. Do you have a still better way?
\$\endgroup\$
  • \$\begingroup\$ That's an 8-bit int, not a 1-byte int. \$\endgroup\$ – rightfold Jan 1 '12 at 16:13
  • \$\begingroup\$ Of course, but I was more concerned about memory usage, that's why byte. Ha, I would have loved to do this in 2 bits instead! \$\endgroup\$ – Vinayak Garg Jan 2 '12 at 6:00
6
\$\begingroup\$

I would definitely recommend using enumerations. As this is December 2011, I would imagine your compiler supports an explicit underlying type on enumerations (GCC supported this since 4.4 and Visual C++ now supports it in 11.0). As a side note, <cstdint> is supported by visual c++ now (as of 10.0).

As an addenum to traditional c-style enumerations:

enum state : std::int8_t {
    S_UNKNWON,
    S_HIGH,
    S_LOW
};

Or adding scope to the enumeration's memebers (avoid polluting global scope):

enum class state : std::int8_t {
    unknown,
    high,
    low
};

state status = state::unknown;

Of course, after looking at boost::tribool, I would recommend that over anything -- unless you really need to ensure that state is exactly 1 byte.

\$\endgroup\$
  • \$\begingroup\$ +1 for informing about cstdint. no more #ifdef required. And yeah i wanted some savings, so enum state : int8_t solved the problem, with type safety! \$\endgroup\$ – Vinayak Garg Dec 30 '11 at 6:25
5
\$\begingroup\$

The main reason there is no accidental conversion (type safety is one of the keys to using C++ correctly).

enum Tri { Yes, No, Maybe };

int main()
{
    Tri     y   = Yes;
    y= 1;   // Fails to compile.
}

Secondly you are using three bytes to hold the different states here

const State kLow = 0;
const State kHigh = 1;
const State kUndefined = 2;

With an enum there is no space taken up (though potentially the above may be optimized out).

C++11 also allows you to specify the size of an enum:

enum class Tri : char { Yes, No, Maybe };
//               ^^^^  Uses a char sized object

Do you see any potential problem with it?

Yes. Not type safe

And why would one waste 4 bytes instead of a single byte for 3 values only

Why not. Does it really matter in any modern PC.
OK for embedded systems maybe (but you obviously are using WINDOWS)

Do you have a still better way?

Yes. Use enum in C++11

\$\endgroup\$
  • \$\begingroup\$ I got a kick out of pushing you over the 5K border ^^ \$\endgroup\$ – Adam Dec 30 '11 at 2:04
  • \$\begingroup\$ every time one reminds me of good habit (here not ditching enum) i +1 :) \$\endgroup\$ – Vinayak Garg Dec 30 '11 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.