Home > front end >  Qt Creator with C/C warns about padding for enums
Qt Creator with C/C warns about padding for enums

Time:02-22

So I have a data structure that entirely consists of :

  • unsigned char i.e. byte-sized attributes
  • bool_T which I have typedefd to unsigned char (if C) or to bool (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 enums 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.

  • Related