473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

enums ain't no good

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
Nov 27 '07 #1
9 2696
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.
Nov 27 '07 #2
"copx" <co**@gazeta.plwrites:
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.
Nov 27 '07 #3

"Ben Pfaff" <bl*@cs.stanford.eduschrieb im Newsbeitrag
news:87************@blp.benpfaff.org...
"copx" <co**@gazeta.plwrites:
>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.
Nov 27 '07 #4
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
es*****@ieee-dot-org.invalid
Nov 27 '07 #5
On Nov 26, 8:24 pm, "copx" <c...@gazeta.plwrote:
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 '07 #6
Mark L Pappin wrote:
"copx" <co**@gazeta.plwrites:
>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

Dec 1 '07 #7
"ka*****@gmail.com" <ka*****@gmail.comwrites:
On Nov 26, 8:24 pm, "copx" <c...@gazeta.plwrote:
>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) <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"
Dec 1 '07 #8
On Nov 30, 7:16 pm, Keith Thompson <ks...@mib.orgwrote:
"kar1...@gmail.com" <kar1...@gmail.comwrites:
On Nov 26, 8:24 pm, "copx" <c...@gazeta.plwrote:
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"
Dec 1 '07 #9
ka*****@gmail.com 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
es*****@ieee-dot-org.invalid
Dec 1 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could...
2
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
4
by: David Rasmussen | last post by:
The problem comes up in all sorts of contexts. But here is an example: We want to model the basics of chess, maybe for making a chess program, or maybe just to make an interactive board or ... ...
27
by: Mark A. Gibbs | last post by:
i have been toying with the idea of making my enums smarter - ie, more in line with the rest of the language. i haven't tested it yet, but what i came up with is a template like this: template...
9
by: Jarmo Muukka | last post by:
Hello all! I have used C++ for many years and recently I have used C#. Now I am coding in C++ and I have a problem now. In C# you can do enums like this: enum Lane { Left,
4
by: Jon Slaughter | last post by:
is there a simple way to "step" through enums? I have a button that I want to click and have it "cycle" through a set of states defined by enums but the only way I can think of doing this...
10
by: =?utf-8?b?QXNiasO4cm4gU8OmYsO4?= | last post by:
I came over this code which puzzled me. Isn't the enum supposed to have an identifier? enum { BT_CONNECTED = 1, /* Equal to TCP_ESTABLISHED to make net code happy */ BT_OPEN, BT_BOUND,...
3
by: =?Utf-8?B?Sm9uYXRoYW4gU21pdGg=?= | last post by:
I have a class as follows: class GamesConsole { public int iReference; public enum Maker {Nintendo, Sega, Sony, Panasonic} }
4
by: Just Me | last post by:
I keep hitting the same minor problem, but dont have a really great solution. So Im looking for ideas. There are many instances where I want to force the developer ( me ) from a readability...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.