How big is an enum?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pep

    How big is an enum?

    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.

  • Neelesh Bodas

    #2
    Re: How big is an enum?


    Pep wrote:[color=blue]
    > There still remains the question however as to what the C++ standard claims
    > should be the case?
    >[/color]

    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."

    Comment

    • Pep

      #3
      Re: How big is an enum?

      Neelesh Bodas wrote:
      [color=blue]
      >
      > Pep wrote:[color=green]
      >> There still remains the question however as to what the C++ standard
      >> claims should be the case?
      >>[/color]
      >
      > 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."[/color]

      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.

      Comment

      • Kristo

        #4
        Re: How big is an enum?

        Pep wrote:[color=blue]
        > Which leads to the next question. Is a int the size of a long, which it
        > certainly appears to be in my tests?[/color]

        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

        Comment

        • Ivan Vecerina

          #5
          Re: How big is an enum?

          "Pep" <pep@nowhere.co m> wrote in message
          news:dli29c$e2n $1@pop-news.nl.colt.ne t...
          : 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
          --
          http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form


          Comment

          • Krishanu Debnath

            #6
            Re: How big is an enum?

            Pep wrote:[color=blue]
            > Neelesh Bodas wrote:[/color]

            [snip]
            [color=blue]
            > Which leads to the next question. Is a int the size of a long, which it
            > certainly appears to be in my tests?
            >[/color]

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

            Krishanu

            Comment

            • mlimber

              #7
              Re: How big is an enum?

              Krishanu Debnath wrote:[color=blue]
              > Pep wrote:[color=green]
              > > Neelesh Bodas wrote:[/color]
              >
              > [snip]
              >[color=green]
              > > Which leads to the next question. Is a int the size of a long, which it
              > > certainly appears to be in my tests?
              > >[/color]
              >
              > may be, standard only guarantees that size of long is not less than int.
              >
              > Krishanu[/color]

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

              Cheers! --M

              Comment

              • Marcus Kwok

                #8
                Re: How big is an enum?

                Pep <pep@nowhere.co m> wrote:[color=blue]
                > Which leads to the next question. Is a int the size of a long, which it
                > certainly appears to be in my tests?[/color]

                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'.

                --
                Marcus Kwok

                Comment

                • Neelesh Bodas

                  #9
                  Re: How big is an enum?


                  Pep wrote:[color=blue]
                  > Which leads to the next question. Is a int the size of a long, which it
                  > certainly appears to be in my tests?[/color]

                  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."

                  Comment

                  • Krishanu Debnath

                    #10
                    Re: How big is an enum?

                    mlimber wrote:[color=blue]
                    > Krishanu Debnath wrote:[color=green]
                    >> Pep wrote:[color=darkred]
                    >>> Neelesh Bodas wrote:[/color]
                    >> [snip]
                    >>[color=darkred]
                    >>> Which leads to the next question. Is a int the size of a long, which it
                    >>> certainly appears to be in my tests?
                    >>>[/color]
                    >> may be, standard only guarantees that size of long is not less than int.
                    >>
                    >> Krishanu[/color]
                    >
                    > On my platform, long = 40 bits and int = 32 bits.
                    >
                    > Cheers! --M
                    >[/color]

                    And how does this violates my assertion?

                    Krishanu

                    Comment

                    • mlimber

                      #11
                      Re: How big is an enum?

                      Krishanu Debnath wrote:[color=blue]
                      > mlimber wrote:[color=green]
                      > > Krishanu Debnath wrote:[color=darkred]
                      > >> 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[/color]
                      > >
                      > > On my platform, long = 40 bits and int = 32 bits.
                      > >
                      > > Cheers! --M
                      > >[/color]
                      >
                      > And how does this violates my assertion?
                      >
                      > Krishanu[/color]

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

                      Cheers! --M

                      Comment

                      • Pep

                        #12
                        Re: How big is an enum?

                        mlimber wrote:
                        [color=blue]
                        > Krishanu Debnath wrote:[color=green]
                        >> mlimber wrote:[color=darkred]
                        >> > 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
                        >> >[/color]
                        >>
                        >> And how does this violates my assertion?
                        >>
                        >> Krishanu[/color]
                        >
                        > It doesn't. I was providing a concrete example confirming it.
                        >
                        > Cheers! --M[/color]

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

                        Comment

                        Working...