enums ain't no good

Discussion in 'C Programming' started by copx, Nov 27, 2007.

  1. copx

    copx Guest

    C's enum type disappoints me a lot.

    You cannot define which type of integer variable is used. This contradicts
    C's low level spirit. "I want a number variable. I do not care about size or
    execution speed." feels like typical ultra high level scripting language
    design. The compiler could not optimize an enum if it wanted to, because you
    cannot even specify whether memory requirements or speed are your primary
    concern. Enums were meant to replace these endless lists of defines you find
    all over older C source:
    #define FOO 0
    #define BAR 1
    ...
    It is a technique commonly used to give index numbers meaningful names.
    However, the variables which hold such numbers often do not need to be wider
    than one byte and are used a lot. In one program I had many enum-type
    variables as parts of my data structures and the amount of wasted RAM was
    excessive when I used C enums, because the compiler (GCC) always used
    (unsigned) ints, even if the entire range of valid values fitted into a
    single byte. IMHO the definition of an enum should allow you to specify the
    integer type. For example:

    typedef int8_t enum {
    FOO,
    BAR
    } MY_ENUM;

    Not giving the programmer this type of control makes me feel like I am using
    another language. What was the reasoning behind that decision? Did the guy
    who designed the enum type also suggest to replace all of C's integer types
    with "number"?

    copx
     
    copx, Nov 27, 2007
    #1
    1. Advertising

  2. > C's enum type disappoints me a lot.
    > You cannot define which type of integer variable is used.


    Yes, a common annoyance. However, most compilers for embedded software has a
    "treat enums like ints" option, that can be disabled, in order to achieve
    auto-sizing enum's.

    Leo Havmøller.
     
    Leo Havmøller, Nov 27, 2007
    #2
    1. Advertising

  3. copx

    Ian Collins Guest

    copx wrote:

    Ain't no good is a double negative....

    > C's enum type disappoints me a lot.
    >

    As they should, they are more than a little broken.

    <snip>

    >
    > Not giving the programmer this type of control makes me feel like I am using
    > another language. What was the reasoning behind that decision? Did the guy
    > who designed the enum type also suggest to replace all of C's integer types
    > with "number"?
    >

    Where size matters (typically but not exclusively on embedded systems),
    many compilers provide extensions to size enums.

    --
    Ian Collins.
     
    Ian Collins, Nov 27, 2007
    #3
  4. copx

    Ben Pfaff Guest

    "copx" <> writes:

    > C's enum type disappoints me a lot.
    >
    > You cannot define which type of integer variable is used. This contradicts
    > C's low level spirit.


    There's not much difference between, on one hand, defining an
    enum type without a tag and then typedef'ing an integer type to
    what name you like and, on the other hand, defining an enum type
    with a tag and using the enum type directly. If the latter falls
    short, you can just use the former.
    --
    Go not to Usenet for counsel, for they will say both no and yes.
     
    Ben Pfaff, Nov 27, 2007
    #4
  5. copx

    copx Guest

    "Leo Havmøller" <> schrieb im Newsbeitrag
    news:474b9f52$0$2100$...
    >> C's enum type disappoints me a lot.
    >> You cannot define which type of integer variable is used.

    >
    > Yes, a common annoyance. However, most compilers for embedded software has
    > a "treat enums like ints" option, that can be disabled, in order to
    > achieve auto-sizing enum's.


    Yes, IIRC GCC has such an option, too. It chooses the narrowest type
    possible. However, that is not the same as having full control over which
    type is used, and as I have said, I think this should be doable at syntax
    level.
     
    copx, Nov 27, 2007
    #5
  6. copx

    copx Guest

    "Ben Pfaff" <> schrieb im Newsbeitrag
    news:...
    > "copx" <> writes:
    >
    >> C's enum type disappoints me a lot.
    >>
    >> You cannot define which type of integer variable is used. This
    >> contradicts
    >> C's low level spirit.

    >
    > There's not much difference between, on one hand, defining an
    > enum type without a tag and then typedef'ing an integer type to
    > what name you like and, on the other hand, defining an enum type
    > with a tag and using the enum type directly. If the latter falls
    > short, you can just use the former.


    I have read about that technique in some embedded programming journal
    recently. Yes, it works, but it is an ugly workaround for what is a language
    design flaw IMO. Enums are supposed to define the type which is used to
    store the enum values after all.
     
    copx, Nov 27, 2007
    #6
  7. copx

    Eric Sosman Guest

    copx wrote:
    > C's enum type disappoints me a lot.
    > [...]
    > It is a technique commonly used to give index numbers meaningful names.
    > However, the variables which hold such numbers often do not need to be wider
    > than one byte and are used a lot. In one program I had many enum-type
    > variables as parts of my data structures and the amount of wasted RAM was
    > excessive when I used C enums, because the compiler (GCC) always used
    > (unsigned) ints, even if the entire range of valid values fitted into a
    > single byte.


    You may have missed the fact that you can use the enum
    declaration just to define the constants, and then store their
    values in any type you are fond of:

    enum { FOO, BAR, BAZ };
    char foo = FOO;
    unsigned long long bar = BAR;
    long double baz = BAZ;

    --
    Eric Sosman
    lid
     
    Eric Sosman, Nov 27, 2007
    #7
  8. "copx" <> writes:
    > Enums are supposed to define the type which is used to
    > store the enum values after all.


    Chapter and Verse?

    mlp
     
    Mark L Pappin, Nov 30, 2007
    #8
  9. copx

    CBFalconer Guest

    Mark L Pappin wrote:
    > "copx" <> writes:
    >
    >> Enums are supposed to define the type which is used to
    >> store the enum values after all.

    >
    > Chapter and Verse?


    In Pascal. However, this is C.

    --
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Nov 30, 2007
    #9
  10. copx

    Guest

    On Nov 26, 8:24 pm, "copx" <> wrote:
    > C's enum type disappoints me a lot.
    >
    > You cannot define which type of integer variable is used. This contradicts
    > C's low level spirit. "I want a number variable. I do not care about size or
    > execution speed." feels like typical ultra high level scripting language
    > design. The compiler could not optimize an enum if it wanted to, because you
    > cannot even specify whether memory requirements or speed are your primary
    > concern. Enums were meant to replace these endless lists of defines you find
    > all over older C source:
    > #define FOO 0
    > #define BAR 1
    > ..
    > It is a technique commonly used to give index numbers meaningful names.
    > However, the variables which hold such numbers often do not need to be wider
    > than one byte and are used a lot. In one program I had many enum-type
    > variables as parts of my data structures and the amount of wasted RAM was
    > excessive when I used C enums, because the compiler (GCC) always used
    > (unsigned) ints, even if the entire range of valid values fitted into a
    > single byte.


    Isn't that GCC's fault to waste memory and not a language flaw?

    At least my understanding is that the actual enum type can be chosen
    freely by the implementation as long it can fit all values [1]. So GCC
    _could_ have used a byte (octet).

    Karthik

    [l]. From n869.txt 6.7.2.2 [#3]

    [#4] Each enumerated type shall be compatible with an
    integer type. The choice of type is
    implementation-defined,99) but shall be capable
    of
    representing the values of all the members of the
    enumeration. The enumerated type is incomplete until after
    the } that terminates the list of enumerator declarations.



    IMHO the definition of an enum should allow you to specify the
    > integer type. For example:
    >
    > typedef int8_t enum {
    > FOO,
    > BAR
    >
    > } MY_ENUM;
    >
    > Not giving the programmer this type of control makes me feel like I am using
    > another language. What was the reasoning behind that decision? Did the guy
    > who designed the enum type also suggest to replace all of C's integer types
    > with "number"?
    >
    > copx
     
    , Dec 1, 2007
    #10
  11. "" <> writes:
    > On Nov 26, 8:24 pm, "copx" <> wrote:
    >> C's enum type disappoints me a lot.
    >>
    >> You cannot define which type of integer variable is used. This contradicts
    >> C's low level spirit. "I want a number variable. I do not care about size or
    >> execution speed." feels like typical ultra high level scripting language
    >> design. The compiler could not optimize an enum if it wanted to, because you
    >> cannot even specify whether memory requirements or speed are your primary
    >> concern. Enums were meant to replace these endless lists of defines you find
    >> all over older C source:
    >> #define FOO 0
    >> #define BAR 1
    >> ..
    >> It is a technique commonly used to give index numbers meaningful names.
    >> However, the variables which hold such numbers often do not need to be wider
    >> than one byte and are used a lot. In one program I had many enum-type
    >> variables as parts of my data structures and the amount of wasted RAM was
    >> excessive when I used C enums, because the compiler (GCC) always used
    >> (unsigned) ints, even if the entire range of valid values fitted into a
    >> single byte.

    >
    > Isn't that GCC's fault to waste memory and not a language flaw?
    >
    > At least my understanding is that the actual enum type can be chosen
    > freely by the implementation as long it can fit all values [1]. So GCC
    > _could_ have used a byte (octet).


    Yes, it could, but why should it? On many systems, operations on
    words (e.g., int or unsigned int) are faster than operations on bytes.
    In some cases, data space saved by using bytes might be more than
    offset by the increase in code size.

    --
    Keith Thompson (The_Other_Keith) <>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 1, 2007
    #11
  12. copx

    Guest

    On Nov 30, 7:16 pm, Keith Thompson <> wrote:
    > "" <> writes:
    > > On Nov 26, 8:24 pm, "copx" <> wrote:
    > >> C's enum type disappoints me a lot.

    >
    > >> You cannot define which type of integer variable is used. This contradicts
    > >> C's low level spirit. "I want a number variable. I do not care about size or
    > >> execution speed." feels like typical ultra high level scripting language
    > >> design. The compiler could not optimize an enum if it wanted to, because you
    > >> cannot even specify whether memory requirements or speed are your primary
    > >> concern. Enums were meant to replace these endless lists of defines you find
    > >> all over older C source:
    > >> #define FOO 0
    > >> #define BAR 1
    > >> ..
    > >> It is a technique commonly used to give index numbers meaningful names.
    > >> However, the variables which hold such numbers often do not need to be wider
    > >> than one byte and are used a lot. In one program I had many enum-type
    > >> variables as parts of my data structures and the amount of wasted RAM was
    > >> excessive when I used C enums, because the compiler (GCC) always used
    > >> (unsigned) ints, even if the entire range of valid values fitted into a
    > >> single byte.

    >
    > > Isn't that GCC's fault to waste memory and not a language flaw?

    >
    > > At least my understanding is that the actual enum type can be chosen
    > > freely by the implementation as long it can fit all values [1]. So GCC
    > > _could_ have used a byte (octet).

    >
    > Yes, it could, but why should it? On many systems, operations on
    > words (e.g., int or unsigned int) are faster than operations on bytes.
    > In some cases, data space saved by using bytes might be more than
    > offset by the increase in code size.


    OTOH, CPUs run way faster than the memory latency (100/1000 times?).
    So any denser representation of data may not only result in less DRAM
    usage, it will reduce the amount of data that must come into the CPU.
    This can translate to a faster overall performance (less total time).

    These days memories are getting bigger and cheaper and hence saving
    DRAM usage may not be a bigger concern. Reducing the amount of data
    that is shuttled back/n/forth from CPU to the memory will contribute
    to a reduction in total time. Thus optimizing for space and time need
    not be mutually exclusive.

    In any case, I think this issue is orthogonal to the OP's original
    concern that the language mandates enum types to be int; which it does
    not (starting C99).

    Karthik


    >
    > --
    > Keith Thompson (The_Other_Keith) <>
    > Looking for software development work in the San Diego area.
    > "We must do something. This is something. Therefore, we must do this."
    > -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    , Dec 1, 2007
    #12
  13. copx

    Eric Sosman Guest

    wrote:
    > [...]
    > In any case, I think this issue is orthogonal to the OP's original
    > concern that the language mandates enum types to be int; which it does
    > not (starting C99).


    For clarity: The enumeration constants are all of type
    `int', but the enum type itself is a distinct type of its
    own, not an `int'. ANSI Classic 3.1.2.5p9 (before the ISO
    re-numbering).

    --
    Eric Sosman
    lid
     
    Eric Sosman, Dec 1, 2007
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DeMarcus

    Ain't JDialog tricky?

    DeMarcus, Nov 24, 2004, in forum: Java
    Replies:
    5
    Views:
    4,057
    DeMarcus
    Nov 24, 2004
  2. Betty

    ain't java grand ?

    Betty, May 24, 2005, in forum: Java
    Replies:
    4
    Views:
    429
    Joona I Palaste
    May 26, 2005
  3. Bulba!

    Speed ain't bad

    Bulba!, Dec 31, 2004, in forum: Python
    Replies:
    14
    Views:
    582
    Anders J. Munch
    Jan 4, 2005
  4. =?utf-8?b?QXNiasO4cm4gU8OmYsO4?=

    Enums without identifier, enums and typedef

    =?utf-8?b?QXNiasO4cm4gU8OmYsO4?=, Jan 19, 2007, in forum: C Programming
    Replies:
    10
    Views:
    1,173
    Keith Thompson
    Jan 20, 2007
  5. Wiseman
    Replies:
    13
    Views:
    621
    John Machin
    May 8, 2007
Loading...

Share This Page