5
\$\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?
asked Dec 29, 2011 at 10:45
\$\endgroup\$
2
  • \$\begingroup\$ That's an 8-bit int, not a 1-byte int. \$\endgroup\$ Commented Jan 1, 2012 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\$ Commented Jan 2, 2012 at 6:00

2 Answers 2

5
\$\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.

answered Dec 29, 2011 at 23:57
\$\endgroup\$
1
  • \$\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\$ Commented Dec 30, 2011 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

answered Dec 29, 2011 at 23:55
\$\endgroup\$
2
  • \$\begingroup\$ I got a kick out of pushing you over the 5K border ^^ \$\endgroup\$ Commented Dec 30, 2011 at 2:04
  • \$\begingroup\$ every time one reminds me of good habit (here not ditching enum) i +1 :) \$\endgroup\$ Commented Dec 30, 2011 at 6:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.