views:

2443

answers:

14

For example if I have an Enum with two cases, does it make take more memory than a boolean? Languages: Java, C++

+1  A: 

No, an enum is generally the same size as an int, same as boolean.

Cody Brocious
It could, hence 'generally'. If someone is asking such a question without further details, though, edge cases like that are rarely important.
Cody Brocious
The Java and C++ tags indicate that this comment contains the correct answer in this context.
Jeffrey L Whitledge
OK, on further research, it appears the answer for Java is not correct. (OT: It is the same as an int in C#, though.)
Jeffrey L Whitledge
In C++ enum and int same. bool and char same.
Martin York
Note: Generally.
Martin York
+6  A: 

In Java, an enum is a full-blown class:

Java programming language enum types are much more powerful than their counterparts in other languages. The enum declaration defines a class (called an enum type). The enum class body can include methods and other fields.

So the size of an enum in Java will be a little bit more heavier than a plain integer.

coobird
On 32-bit platforms, it'll be exactly the same size as an int - it's just a (32 bit) pointer to the enum instance. Only one instance of each enum value exists in memory.
Lamah
+4  A: 

In Java, it would take more memory. In C++, it would take no memory than required for a constant of the same type (it's evaluated at compile-time and has no residual significance at runtime). In C++, this means that the default type for an enum will occupy the same space as an int.

Jeff Hubbard
+2  A: 
printf("%d", sizeof(enum));
Patrick
A: 

If your enum will ever have only two cases, indeed using a boolean instead might be a better idea (memory size, performance, usage/logic), even more in Java.
If you are wondering about memory cost, it might imply you plan to use lot of them. In Java you can use BitSet class, or on a smaller scale, in both languages you can manipulate bits with bitwise operations.

PhiLho
+5  A: 

bool might be implemented as a single byte, but typically in a structure it would be surrounded by other elements that have alignment requirements that would mean that the boolean would effectively be occupying at least as much space as an int.

Modern processors load data from main memory as a whole cache line, 64 bytes. The difference between loading one byte from L1 cache and loading four bytes is negligible.

If you're trying to optimise for cache lines in a very high-performance application, then you might worry about how big your enum is, but generally I'd say it's clearer to define an enum than to use a boolean.

Mike Dimmick
+9  A: 

In Java, there should only be one instance of each of the values of your enum in memory. A reference to the enum then requires only the storage for that reference. Checking the value of an enum is as efficient as any other reference comparison.

Lamah
+5  A: 

You would only worry about this when storing large quantities of enums. For Java, you may be able to use an EnumSet in some cases. It uses a bit vector internally which is very space efficient and fast.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/EnumSet.html

Tom
A: 
anjanb
+1  A: 

In C++ an enum is typically the same size as an int. That said it is not uncommon for compilers to provide a command line switch to allow the size of the enum to be set to the smallest size that fits the range of values defined.

Andrew Edgecombe
A: 

In C/C++ an enum will be the same size as an int.

With gcc you can add attribute((packed)) to the enum definition to make it take the minimum footprint. If the largest value in the enum is < 256 this will be one byte, two bytes if the largest value is < 65536, etc.

typedef enum {
    MY_ENUM0,
    MY_ENUM1,
    MY_ENUM2,
    MY_ENUM3,
    MY_ENUM4,
    MY_ENUM5
} __attribute__((packed)) myEnum_e;
DGentry
I am fairly sure __attribute__((packed)) is a GCC-only extension. If I remember correctly, C99 does not specify a standard pragma for "packing", so every compiler does it differently.
crosstalk
You're probably right. It was my recollection that packed was a C99 feature, but my memory is often cantankerous and uncooperative. I removed the C99 reference.
DGentry
+3  A: 

In ISO C++ there is no obligation for an enum to be larger than its largest enumerator requires. In particular, enum {TRUE, FALSE} may have sizeof(1) even when sizeof(bool)==sizeof(int). There is simply no requirement. Some compilers make the enums the same size as an int. That is a compiler feature, which is allowed because the standard only imposes a minimum. Other compilers use extensions to control the size of an enum.

MSalters
I think most use the native size for the CPU architecture. 32-bit aligned data is fast to retrieve for most 32-bit CPUs, so most types end up as multiples of 32 bits.
gbjbaanb
A: 

Regardless of which one takes less memory, you should always be using a boolean for situations like this.

Greg Rogers
A: 

Why does the size of an enum matter? Do you have some application which is trying to pack memory efficiently? If so, use a bit field. Otherwise, stop doing early optimization.

David Medinets