Memory allocation for enum data type - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Memory allocation for enum data type

amvelamvel Posts: 14Member
Hi Buddies,

Please consider the example

typedef enum



In example. i have 6 enum members. But if i find the sizeof(enumtag),
it returns only 4 bytes as total size ie., it returns size of only one member. What about remaining 5 members.

Please anyone can tell me how memory allocation is done for an enum datatype and how its organised.

Thanks in advance.


  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]enums are not allocated in memory - they exist only on compilation stage. They only exist to tell compiler what value is FEB for example. When code runs - there is no enums there anymore.

    It is exactly same as doing this:[/color]
    #define JAN 0
    #define FEB 1
    #define MAR 2
    #define APR 3
    [color=Blue]Absolutely same, but easier to support and read the code with enums then with #defines.

    Do these defines require memory allocation? Answer is no. Same for enums.[/color]
  • amvelamvel Posts: 14Member
    Hi buddy,

    Thanks for ur reply...
    U hav mentioned that the memory shall not be allocated for enums.
    But i could remember that the memory allocation and processing time(preprocessor time and compilation time) are the main difference between macro and an enum.

    For example,

    while doing debugging(runtime) v ll not able to c the value of macro,Since memory is not allocated for macro and v ll not able to access the memory location. To know the value of macro, v hav to look into the file where it is defined.

    But for enums, memory shall be allocated. so that v could able to access the memory and c the value of enum in debugging time(runtime).

    So, if memory is not allocated for an enum, then how v could able to know the value in runtime. And from where it access and gives us the value.

    Dude, I feel i dint get exact answer for my question. let me know, if my explanation is not correct and try to provide me the answer.

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]Your explanation is not correct. Macro (#define xxx)and enum are the same thing, except that macro do not have a type and enum has type int. The rest is the same.[/color]
Sign In or Register to comment.