Closed GoogleCodeExporter closed 8 years ago
I solved the problem, it was a compiler issue but it'll be nice for protoc-c to
consider such situation in later releases or at least warn the user in docs,
notes, etc. Here's the cause of the problem:
ANSI C defines no fixed type size for enums but limits the maximum size, e.g.
int. Many compilers, specially in case device programming optimize enum
storage. In my case, the arm compiler saves the enum value as a char, short or
int (maximum) depending on the value. For example a vlue of 9 will be saved as
a one byte char, while 20,000 will be saved as short. However since the maximum
value is int (4 bytes in my case), then the struct representing the message in
.proto file assigns 4 bytes to the enum field. Now say that my enum value is 1
which takes one byte in my machine. Now suppose that "offsetof(msg_struct,
my_enum)" return 16. In my big-endian machine the pointer returned for the
offset points to a 4 bytes block of "0x01 00 00 00" while the correct value
should have been "0x00 00 00 01".
I used the -fy switch to force the compiler to store enums as integers.
Original comment by Farrokh....@gmail.com
on 27 Sep 2011 at 10:52
I have been aware of this problem. I'm surprised not many compilers support
small enums nowadays.
I am torn between supporting a member of the enum descriptor that is the sizeof
the enum, or adding into the code a macro like:
__PROTOBUF_C_ENSURE_ENUM_IS_32BITS(MACRO_NAME).
A standard implementation of this would be:
#define __PROTOBUF_C_ENSURE_ENUM_IS_32BITS(MACRO_NAME) \
PROTOBUF_C_ENSURE_ENUM_IS_32_BITS__##MACRO_NAME = 0x10000
Original comment by lahike...@gmail.com
on 2 Nov 2011 at 4:41
Fix committed to subversion to ensure enums are the size of ints.
Original comment by lahike...@gmail.com
on 12 Nov 2011 at 2:32
Original comment by lahike...@gmail.com
on 12 Nov 2011 at 2:33
Original issue reported on code.google.com by
Farrokh....@gmail.com
on 1 Sep 2011 at 12:02Attachments: