So I have a data structure that entirely consists of :
unsigned char
i.e. byte-sized attributesbool_T
which I havetypedef
d tounsigned char
(if C) or tobool
(if C ) so again byte-sized- enums which contain no more than 5 entries and should therefore be byte-sized as well.
However, next to each enum
, Qt Creator puts one of those pre-analysis hypothetical warnings (the symbol is a yellow triangle that's empty - only the contour of it) about padding the data structure with some bytes with the aim, I gather, to align the next element in the structure.
How does that make any sense, since this is (or should be) all bytes ? Are my enum
s not bytes ? Can I force the compiler to make them bytes ? Can I force the compiler to not align in some cases, or does the C standard prohibit this ?
More info : I am writing C89, using Qt 5.12.11, Qt Creator 4.15.0 (based on Qt 5.15.2), CMake 3.19.4 with Ninja 1.10.2 and compiling with minGW/gcc 7.3.0.
Thanks !
CodePudding user response:
C nowadays has the possibility to specify the underlying type of an enum
, but that's quite new (C 11, not C99 let alone C89). So no, you can't assume it's a byte, nor can you directly force it. But you can use bitfields (:3
is enough for up to 8 values)
Also, bool
in C is not byte-sized by definition; it can be bigger.