0
#pragma pack(push, 1)
typedef enum __attribute__ ((__packed__)) {
    ETypeZero = 0,
    ETypeOne,
    ETypeTwo,
    ETypeUndefined
} ESomeType;
#pragma pack(pop)

In my code above, I had accidentally forgot #pragma pack(pop), and I saw some other data down the line getting corrupted. Why would that have occurred? Also, is there a GCC flag that can detect the missing pop?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Nishikant
  • 67
  • 5
  • 1
    "*I saw some other data down the line getting corrupted. Why would that have occurred?*" - Why do you think it *wouldn't* have occurred? The `pragma` takes effect on all subsequent code until you turn it off, or the file ends. So, you altered the alignment of not just your `enum`, but of all other subsequent data as well. And that other data was likely used in places that expected it to have a different alignment than you specified. – Remy Lebeau Apr 28 '22 at 20:34
  • In any case, why are you using `#pragma pack(push, 1)` together with `__attribute__ ((__packed__))` at all? That is redundant. Use one or the other, not both. Use `#pragma pack` for whole blocks of code. Use `__attribute__ ((packed))` on specific variables/types. – Remy Lebeau Apr 28 '22 at 20:38

0 Answers0