How big is an enum?

P

Pep

Okay I am being slightly lazy here but ...

I wrote a quick program that I have compiled and run under FreeBSD and Linux
using GCC and on Windows using Visual C version 6

===============================================================================
#include <stdio.h>

enum { A, B, C} aa;
long bb;
int cc;
char dd;

int main(int argc, char** argv)
{
printf("enum %d long %d, int %d char %d\n", sizeof(aa), sizeof(bb),
sizeof(cc), sizeof(dd));
}
===============================================================================


which gives this output

===============================================================================
enum 4 long 4, int 4 char 1
===============================================================================

on all systems.

There still remains the question however as to what the C++ standard claims
should be the case?

I have waded through the standard and have got myself lost in the usual
gobble dee gook in there.

Help, anyone know the definitive answer to this?

Having made a earlier claim I must now admit that I do not remember
sufficiently from my days on the Intel compiler, the VAX compiler and Sun
compilers if this is really the case?

Cheers,
Pep.
 
N

Neelesh Bodas

Pep said:
There still remains the question however as to what the C++ standard claims
should be the case?

7.2 - 5 : "The underlying type of an enumeration is an integral type
that can represent all the enumerator values
defined in the enumeration. It is implementation defined which integral
type is used as the underlying type for an enumeration except that the
underlying type shall not be larger than int unless the value of an
enumerator cannot fit in an int or unsigned int."
 
P

Pep

Neelesh said:
7.2 - 5 : "The underlying type of an enumeration is an integral type
that can represent all the enumerator values
defined in the enumeration. It is implementation defined which integral
type is used as the underlying type for an enumeration except that the
underlying type shall not be larger than int unless the value of an
enumerator cannot fit in an int or unsigned int."

Which leads to the next question. Is a int the size of a long, which it
certainly appears to be in my tests?

Cheers,
Pep.
 
K

Kristo

Pep said:
Which leads to the next question. Is a int the size of a long, which it
certainly appears to be in my tests?

It might be for your implementation. The standard guarantees that
it'll be no smaller than 16 bits, but it is permitted to be larger.

Kristo
 
I

Ivan Vecerina

: Neelesh Bodas wrote:
:
: >
: > Pep wrote:
: >> There still remains the question however as to what the C++ standard
: >> claims should be the case?
: >>
: >
: > 7.2 - 5 : "The underlying type of an enumeration is an integral type
: > that can represent all the enumerator values
: > defined in the enumeration. It is implementation defined which
integral
: > type is used as the underlying type for an enumeration except that the
: > underlying type shall not be larger than int unless the value of an
: > enumerator cannot fit in an int or unsigned int."
:
: Which leads to the next question. Is a int the size of a long, which it
: certainly appears to be in my tests?

This does appear to be the case on your platform.

Yet, the compiler in your example could have elected to use a char
rather than an int to represent the enumeration. Some compilers
allow you to configure this behavior.

Ivan
 
K

Krishanu Debnath

Pep said:
Neelesh Bodas wrote:
[snip]

Which leads to the next question. Is a int the size of a long, which it
certainly appears to be in my tests?

may be, standard only guarantees that size of long is not less than int.

Krishanu
 
M

mlimber

Krishanu said:
Pep said:
Neelesh Bodas wrote:
[snip]

Which leads to the next question. Is a int the size of a long, which it
certainly appears to be in my tests?

may be, standard only guarantees that size of long is not less than int.

Krishanu

On my platform, long = 40 bits and int = 32 bits.

Cheers! --M
 
M

Marcus Kwok

Pep said:
Which leads to the next question. Is a int the size of a long, which it
certainly appears to be in my tests?

In TC++PL:SE, p.75 (section 4.6):

(In the below, I use '===' to mean 'defined as')

1 === sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)

1 <= sizeof(bool) <= sizeof(long)

sizeof(char) <= sizeof(wchar_t) <= sizeof(long)

sizeof(float) <= sizeof(double) <= sizeof(long double)

sizeof(N) === sizeof(signed N) === sizeof(unsigned N)

where N can be char, short int, int, or long int. In addition, it is
guaranteed that a char has at least 8 bits, a short and an int at
least 16 bits, and a long at least 32 bits.


So the way I interpret it, a conforming implementation is free to make

sizeof(char) == sizeof(long) == 1

for a big enough 'char'.
 
N

Neelesh Bodas

Pep said:
Which leads to the next question. Is a int the size of a long, which it
certainly appears to be in my tests?

3.9.1 - 2: "There are four signed integer types: "signed char",
"short int", "int", and "long int." In this list, each type
provides at least as much storage as those preceding it in the list.
Plain ints have the natural size suggested by the architecture of the
execution environment) ; the other signed integer types are provided to
meet special needs."
 
K

Krishanu Debnath

mlimber said:
Krishanu said:
Pep said:
Neelesh Bodas wrote: [snip]

Which leads to the next question. Is a int the size of a long, which it
certainly appears to be in my tests?
may be, standard only guarantees that size of long is not less than int.

Krishanu

On my platform, long = 40 bits and int = 32 bits.

Cheers! --M

And how does this violates my assertion?

Krishanu
 
M

mlimber

Krishanu said:
mlimber said:
Krishanu said:
Pep wrote:
Neelesh Bodas wrote:
[snip]

Which leads to the next question. Is a int the size of a long, which it
certainly appears to be in my tests?

may be, standard only guarantees that size of long is not less than int.

Krishanu

On my platform, long = 40 bits and int = 32 bits.

Cheers! --M

And how does this violates my assertion?

Krishanu

It doesn't. I was providing a concrete example confirming it.

Cheers! --M
 
P

Pep

mlimber said:
Krishanu said:
mlimber said:
Krishanu Debnath wrote:
Pep wrote:
Neelesh Bodas wrote:
[snip]

Which leads to the next question. Is a int the size of a long, which
it certainly appears to be in my tests?

may be, standard only guarantees that size of long is not less than
int.

Krishanu

On my platform, long = 40 bits and int = 32 bits.

Cheers! --M

And how does this violates my assertion?

Krishanu

It doesn't. I was providing a concrete example confirming it.

Cheers! --M

To everyone, especially Neelesh, thanks very much for the information :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top