vendredi 5 avril 2019

enum class promotion - printf prints wrong value

I have this enum class:

enum class myEnum : u8{
    LEVEL_ERROR = 0,
    LEVEL_WARN = 50,
    LEVEL_DEBUG = 150,
  };

and at some point I making use of it (not exactly this way, but simply this is what happens):

myEnum instance = 42;
printf("My enum is now: %u", instance);

And printed value is sometimes 298 sometimes 126657066 but never 42. So what I noticed is that all of this "random" numbers are my value but padded with 3 bytes of whatever (stack ?) - 42 = 0x2A, 298 = 0x12A and 126657066 = 0x78CA22A. I understand that my type is getting promoted to int but it should be padded with 3 bytes of "0" in case of unsigned variable type. So why is is padded with garbage ?

Aucun commentaire:

Enregistrer un commentaire