Electronics > Microcontrollers

Forcing enum to be of a specific size

(1/2) > >>

ricko_uk:
Hi,
in C, is there a way to force an enum to be of a specific size (i.e. uint32_t or uint16_t)?

If not in C, What about C++?

Reason for asking is to make it the size of the micro's architecture to ensure that any change to it is an atomic operation (without having to disable interrupts or using critical sections).

Thank you :)

ajb:
You can used the "packed" attribute on compilers that support it, for example gcc: https://gcc.gnu.org/onlinedocs/gcc-3.3/gcc/Type-Attributes.html

However this doesn't let you specify a particular size, just that it should use the minimum size that will contain the enumerated values.

Alternatively, since enums in C are not really a storage type unto themselves and are basically just defined constants with slightly nicer syntax, you can store them in whatever integer type you want provided it's large enough for the largest value. Enum values are guaranteed to start at 0 and increment by one unless another value is specified, so your max value will be equal to the number values less one unless you specify differently.

SiliconWizard:

--- Quote from: ajb on June 12, 2021, 12:21:39 am ---Alternatively, since enums in C are not really a storage type unto themselves and are basically just defined constants with slightly nicer syntax, you can store them in whatever integer type you want provided it's large enough for the largest value. Enum values are guaranteed to start at 0 and increment by one unless another value is specified, so your max value will be equal to the number values less one unless you specify differently.

--- End quote ---

Yes, if you're using C, as was discussed in another thread, enums are not type-checked anyway, so using them as a type for declaring variables/parameters is useless (except for code documentation reasons). So you can use enums to define your constants, and then use another type for actually storing the value.

ajb:
Also, there's unlikely to be a significant downside to using a type that is smaller than native, so if you can use an 8 bit int to hold the values you need then you can just use that on all architectures and be done with it.  There may be a *small* performance penalty for smaller sizes on more complex architectures (like x86), but it's unlikely to be enough to worry about, and there should be no difference on Cortex M. 

One thing you may want to do is typedef the storage type you want to use separately from the enum.  You can still typedef the enum if you want.  Typedefing the storage type won't give you any additional type safety (neither will the enum itself!), but it will make it clear that the size is chosen deliberately, and if you need to change that size at any point (because you've added a bunch of enum values, or you have some corner case where the size does matter) you're only changing it in one place.  You'll see this in libraries for these exact reasons.

Tagli:
In C++, you can choose the underlying type for both classical enums and enum classes.


--- Code: ---enum class Number : uint8_t {Zero = 0, One = 1, Two = 2};
--- End code ---

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version