size of enum

Discussions around product based on ARM Cortex M0+ core.

Moderator: nferre

pozz
Posts: 68
Joined: Fri Jun 13, 2014 2:55 pm

size of enum

Thu Jul 03, 2014 10:22 am

It seems arm-gcc of Atmel Studio uses implicitly -fshort-enums option. Indeed

Code: Select all

sizeof(enum MyEnum) == 1 
if the largest value of enum MyEnum enters in a unsigned char.

Why does this happen without explicit -fshort-enums option? From gcc user's manual:
-fshort-enums
Allocate to an enum type only as many bytes as it needs for the declared range of possible values. Specifically, the enum type is equivalent to the smallest integer type that has enough room.

Warning: the -fshort-enums switch causes GCC to generate code that is not binary compatible with code generated without that switch. Use it to conform to a non-default application binary interface.
The warning is important. How the libraries have been compiled (newlib and I don't know what else). If they have been compiled without that option, the application that I wrote will be incompatible with them.
lgarelli
Posts: 44
Joined: Mon Oct 26, 2009 4:44 pm

Re: size of enum

Mon Jul 14, 2014 10:05 pm

A practical way to have the size of an enum is to add a item named enum_last in example at the end of the enum.
May be not very practical if you are not using contiguous values to the enum members.

Return to “SAM D20 Cortex-M0+ MCU”

Who is online

Users browsing this forum: No registered users and 1 guest