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!

Categories

Memory allocation for enum data type

amvelamvel Posts: 14Member
Hi Buddies,

Please consider the example

typedef enum
{

JAN,
FEB,
MAR,
APR,
MAY,
JUN

}enumtag;

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.

Comments

  • 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]
    [code]
    #define JAN 0
    #define FEB 1
    #define MAR 2
    #define APR 3
    [/code]
    [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.