5

I'm trying to work with C and Assembly (intelx8086) language.

I'm also using one class that was implemented by a friend of mine. It has a

typedef enum data_10 {a=0,b=7,c=10,} data_10_type;

I want to work with this class bitwise (AKA construct it/destroy it on Assembly). My question is, how much memory does "enum" take?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
NacOverflow
  • 97
  • 1
  • 2
  • 8
  • 1
    In C, same as an `int`. In C++0x, it can be more or less based on the base type. – DCoder Sep 04 '12 at 15:47
  • possible duplicate of [What is the size of an enum in C?](http://stackoverflow.com/questions/366017/what-is-the-size-of-an-enum-in-c) – DCoder Sep 04 '12 at 15:47
  • [Wikipedia](http://en.wikipedia.org/wiki/Data_structure_alignment#Typical_alignment_of_C_structs_on_x86) has some information relevant to your task. – NovaDenizen Sep 05 '12 at 00:20
  • C doesn't have classes, at least not as a first-class language feature. Maybe you mean a struct with a member of that `data_10_type` type? – Peter Cordes Feb 07 '21 at 06:43

3 Answers3

6

Although it may vary from compiler to compiler, enum typically takes the same size as an int. To be sure, though, you can always use sizeof( data_10_type );

Greyson
  • 3,598
  • 1
  • 21
  • 22
  • This will tell you the size on one particular version of one compiler on one architecture. It's not future-proof. – NovaDenizen Sep 05 '12 at 00:16
  • 1
    @NovaDenizen Only if you print it out once and use that forever. Better to store it in a variable and use that to do the allocation. – Greyson Sep 05 '12 at 17:11
  • @Greyson So the OP is writing in assembly, but in every single reference to these structures he does address arithmetic based on this variable? It seems more probable to me that OP wants to know definitively the byte layout of the fields in the structure, but this information doesn't exist outside the context of a single version of a compiler. – NovaDenizen Sep 07 '12 at 17:04
2

Why don't you print it?

/* C99 */
#include <stdio.h>

typedef enum { a = 0, b = 7, c = 10 } data_10_type;
printf("%zu\n", sizeof(data_10_type));

The identifiers in an enumerator list are declared as constants that have type int (C11 §6.7.2.2 Enumeration specifiers), so sizeof(data_10_type) is often equal to sizeof(int), but it isn't necessary!

BTW, if you want to have a size in bits, just use the CHAR_BIT constant (defined in <limits.h>), which indicates how many bits there are in a single byte).

/* C99 */
#include <limits.h>
#include <stdio.h>

typedef enum { a = 0, b = 7, c = 10 } data_10_type;
printf("%zu\n", sizeof(data_10_type) * CHAR_BIT);
md5
  • 23,373
  • 3
  • 44
  • 93
  • in which cases does CHAR_BIT differ from 8? – Dirk Sep 04 '12 at 15:51
  • 3
    The standard says `CHAR_BIT` is greater than 8. For example, on DSPs, `CHAR_BIT` is often equal to 16. – md5 Sep 04 '12 at 15:53
  • 2
    @Dirk: there's no guarantee that CHAR_BIT will be 8. Historically, bytes have been 6, 7, and 8 bits. Modern embedded systems may have different sizes of char (16-bits, for example). We also don't know what architectures will look like 10+ years down the road, so it's best not to assume (or to at least use static assertions to fail safely). – sfstewman Sep 04 '12 at 15:54
  • `CHAR_BIT` must be *at least* 8; it may be larger, but not smaller (5.2.4.2.1, "implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign"). – John Bode Sep 04 '12 at 16:11
1

An enum does not really take any memory at all; it's understood by the compiler and the right numbers get used during compilation. It's an int, whose size is dependent on your system.

mah
  • 39,056
  • 9
  • 76
  • 93