Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > enums ain't no good

Reply
Thread Tools

enums ain't no good

 
 
Keith Thompson
Guest
Posts: n/a
 
      12-01-2007
"" <> writes:
> On Nov 26, 8:24 pm, "copx" <c...@gazeta.pl> 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) <kst->
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"
 
Reply With Quote
 
 
 
 
kar1107@gmail.com
Guest
Posts: n/a
 
      12-01-2007
On Nov 30, 7:16 pm, Keith Thompson <ks...@mib.org> wrote:
> "kar1...@gmail.com" <kar1...@gmail.com> writes:
> > On Nov 26, 8:24 pm, "copx" <c...@gazeta.pl> 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) <ks...@mib.org>
> 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"


 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      12-01-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Enums without identifier, enums and typedef =?utf-8?b?QXNiasO4cm4gU8OmYsO4?= C Programming 10 01-20-2007 01:20 AM
Storing Enums in web.config Matt ASP .Net 2 06-07-2005 01:45 PM
Basic question about enums Hans De Schrijver ASP .Net 1 06-05-2004 11:47 AM
enums martin ASP .Net 2 05-28-2004 04:48 PM
Loop through enums Syd ASP .Net 1 08-28-2003 09:49 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57