Advertisement

sizeof (enumInputType)

Started by December 27, 2000 05:23 PM
2 comments, last by gimp 24 years ago
I need to know at compile time if an enum is too large to fit in a particular data type. For example I have an enum that holds all my event types like KEY_Q_PRESSED, KEY_Q_RELEASED etc for ever possible action. I need to know if all these will fit in a unsigned char as I cast later in my code. The enum always seems to take upn 4 bytes no matter what''s in it. Can I somehow count the entries? Shoul I place a dummy entry at the end of the enum and check to ensure it''s less than 256 when cast as a number? Thanks Chris
Chris Brodie
An enum is basically a list of ints, and being that those constants you pass are ints, 1 of them, they should return 4 bytes.

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."

The Micro$haft BSOD T-Shirt
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
Advertisement
I believe there is a compiler option for the default enum size. In default settings for most compilers I''ve seen, there''s an option "always allocate enums as ints" that is always checked. I assume if you disable this then the compiler will choose a size based on how many enumerated constants there are.

So, in theory a simple sizeof(enum) should work, no?
--------------------------I guess this is where most people put a famous quote..."Everything is funnier with monkey''s" - Unknown
So is my suggestion for tailing the enum with a dummy value then converting to an int the only way?
Chris Brodie

This topic is closed to new replies.

Advertisement