This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/51568] Enum value is not extracted properly via a union
- From: "rearnsha at gcc dot gnu.org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Thu, 15 Dec 2011 17:27:19 +0000
- Subject: [Bug c/51568] Enum value is not extracted properly via a union
- Auto-submitted: auto-generated
- References: <bug-51568-4@http.gcc.gnu.org/bugzilla/>
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51568
--- Comment #2 from Richard Earnshaw <rearnsha at gcc dot gnu.org> 2011-12-15 17:27:19 UTC ---
(In reply to comment #1)
> On linux sizeof (struct rec) is 7, so how do you expect an unsigned (size = 4)
> to hold the entire value?
>
> If you want a packed enum, you need to specify the packed on the element
> declaration, not just on the overall structure:
>
> struct rec {
> unsigned short w;
> unsigned char c;
> enum en e __attribute__((packed));
> } __attribute__((packed));
Actually that doesn't work. The only ways to pack the enum into 8 bits are to
either:
1) Use -fshort-enums (ABI change)
2) Use a bitfield expression in your struct.
struct rec {
unsigned short w;
unsigned char c;
enum en e:8;
} __attribute__((packed));
Sorry for the confusion.