Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is an enum stored?

I was surprised when doing the following that no assembly is produced for the Enums:

enter image description here

I thought perhaps it would do something like:

Hello:
    .byte 0
Goodbye:
    .byte 1

It seems it only adds in the values when assigned to a var:

enter image description here

Why is this so? Why don't the 'enum values' get stored when declared (even if I set Hello=1)? Example link: https://godbolt.org/z/xxnMvq.

like image 524
samuelbrody1249 Avatar asked Mar 02 '23 19:03

samuelbrody1249


1 Answers

An enum is not a variable, it is a type. The different enum labels are in fact constants. Being a type it requires no space in your final binary.

The compiler internally keeps a list of them and replaces every use of them with the constant they represent.

These values do however end up in the debugging info, if it is generated, so you can use them in your debugger.

like image 139
koder Avatar answered Mar 11 '23 17:03

koder