Topic: can storage for enum types be restricted?
Author: rfg@netcom.com (Ronald F. Guilmette)
Date: Mon, 29 Aug 1994 09:44:20 GMT Raw View
In article <1994Aug22.231427.2148@atlas.tntech.edu> rad6938@gemini.tntech.edu writes:
>In article <rfgCuvK53.E3I@netcom.com>, rfg@netcom.com (Ronald F. Guilmette) writes:
>>In article <1994Aug19.152125.2114@atlas.tntech.edu> rad6938@gemini.tntech.edu writes:
>>>
>>>I believe the latest version of the C++ draft standard states that enums are
>>>now only required to be large enough to hold all values needed for the
>>>enumerators declared (rounded up to the nearest bit with).(paraphrased)
>>>This allows compilers to size the storage of enums according to need. Since
>>>this is new to the standard, current compilers still use the ansi-C rules which
>>>state that an enum is the size of an int.
>>
>>Correction... The ANSI C standard places no such requirement on variables
>>having enum types. (It *does* require that enumeration literals act like
>>type `int' literals, but that's something else entirely.) Some conformant
>>ANSI C compilers *will* allocate only a single byte to hold variables having
>>``small'' enum types.
>
>Hmmm, I suppose I misinterpretted "Each enumerated type shall be compatible
>with an integer type."
That is indeed what the C standard says.
>I assumed this meant that enums should be able to
>retain any integer type values assigned to them, etc.
You assumed wrong.
--
-- Ron Guilmette, Sunnyvale, CA ---------- RG Consulting -------------------
---- domain addr: rfg@netcom.com ----------- Purveyors of Compiler Test ----
---- uucp addr: ...!uunet!netcom!rfg ------- Suites and Bullet-Proof Shoes -
Author: kingsum@xtg800.intel.com (Kingsum Chow)
Date: 19 Aug 1994 15:51:40 GMT Raw View
Hi,
I am working on a project using many objects of many enum types
instead of int types so as to make use of C++'s type checking.
struct foo {
enum_A a;
enum_B b;
enum_C c;
...
};
Although each enum type I use has only a few different values (i.e.,
they can be stored in a byte), compilers actually generate 4 bytes (int)
for each data member, as expected. I am wondering if there is any
way to use enum type but restrict the compiler to use a size different
from int? Is it in c++ std, is it supported differently by different
compiler vendors, or is it not available at all?
Thanks,
Kingsum
--
summer: kingsum@ichips.intel.com
school: kingsum@cs.washington.edu
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 19 Aug 1994 18:36:04 GMT Raw View
In article 94Aug19085141@xtg800.intel.com, kingsum@xtg800.intel.com (Kingsum Chow) writes:
>
>Although each enum type I use has only a few different values (i.e.,
>they can be stored in a byte), compilers actually generate 4 bytes (int)
>for each data member, as expected. I am wondering if there is any
>way to use enum type but restrict the compiler to use a size different
>from int? Is it in c++ std, is it supported differently by different
>compiler vendors, or is it not available at all?
It is up to the compiler how much storage to allocate (as long as it allocates
enough space). If space is at a premium in a structure, use bitfields.
---
Steve Clamage, stephen.clamage@eng.sun.com
Author: rad6938@gemini.tntech.edu (Rad)
Date: 19 Aug 94 15:21:24 -0600 Raw View
In article <KINGSUM.94Aug19085141@xtg800.intel.com>, kingsum@xtg800.intel.com (Kingsum Chow) writes:
>...
>Although each enum type I use has only a few different values (i.e.,
>they can be stored in a byte), compilers actually generate 4 bytes (int)
>for each data member, as expected. I am wondering if there is any
>way to use enum type but restrict the compiler to use a size different
>from int? Is it in c++ std, is it supported differently by different
>compiler vendors, or is it not available at all?
I believe the latest version of the C++ draft standard states that enums are
now only required to be large enough to hold all values needed for the
enumerators declared (rounded up to the nearest bit with).(paraphrased)
This allows compilers to size the storage of enums according to need. Since
this is new to the standard, current compilers still use the ansi-C rules which
state that an enum is the size of an int.
----------------------------------------------------------------------------
Richard Deken Graduate student in electrical engineering
PGP public key available Tennessee Technological University
Internet: rad6938@gemini.tntech.edu Cookeville, TN, USA
Author: rfg@netcom.com (Ronald F. Guilmette)
Date: Sun, 21 Aug 1994 07:43:51 GMT Raw View
In article <1994Aug19.152125.2114@atlas.tntech.edu> rad6938@gemini.tntech.edu writes:
>In article <KINGSUM.94Aug19085141@xtg800.intel.com>, kingsum@xtg800.intel.com (Kingsum Chow) writes:
>>...
>>Although each enum type I use has only a few different values (i.e.,
>>they can be stored in a byte), compilers actually generate 4 bytes (int)
>>for each data member, as expected. I am wondering if there is any
>>way to use enum type but restrict the compiler to use a size different
>>from int? Is it in c++ std, is it supported differently by different
>>compiler vendors, or is it not available at all?
>
>I believe the latest version of the C++ draft standard states that enums are
>now only required to be large enough to hold all values needed for the
>enumerators declared (rounded up to the nearest bit with).(paraphrased)
>This allows compilers to size the storage of enums according to need. Since
>this is new to the standard, current compilers still use the ansi-C rules which
>state that an enum is the size of an int.
Correction... The ANSI C standard places no such requirement on variables
having enum types. (It *does* require that enumeration literals act like
type `int' literals, but that's something else entirely.) Some conformant
ANSI C compilers *will* allocate only a single byte to hold variables having
``small'' enum types.
--
-- Ron Guilmette, Sunnyvale, CA ---------- RG Consulting -------------------
---- domain addr: rfg@netcom.com ----------- Purveyors of Compiler Test ----
---- uucp addr: ...!uunet!netcom!rfg ------- Suites and Bullet-Proof Shoes -
Author: swf@ElSegundoCA.NCR.COM (Stan Friesen)
Date: Mon, 22 Aug 94 09:05:24 PDT Raw View
In article <1994Aug19.152125.2114@atlas.tntech.edu>, rad6938@gemini.tntech.edu writes:
|>
|> I believe the latest version of the C++ draft standard states that enums are
|> now only required to be large enough to hold all values needed for the
|> enumerators declared (rounded up to the nearest bit with).(paraphrased)
|> This allows compilers to size the storage of enums according to need. Since
|> this is new to the standard, current compilers still use the ansi-C rules which
|> state that an enum is the size of an int.
Not quite. The ANSI-C standard says that an *enumerator* (one of the
list of possible values of an enumerated type) is an 'int'.
An *enumerated* *type* must merely be compatible with some implementation
defined integral type - this need not be 'int' - it doesn't even have to
be the same integral type for all enumerated types, so long as the rules
for determining the compatible type are specified in the documentation.
Of course this system will not work in C++, since it makes the enumerators
a different type than the variables desinged to hold them!
--
swf@elsegundoca.ncr.com sarima@netcom.com
The peace of God be with you.
Author: rad6938@gemini.tntech.edu (Rad)
Date: 22 Aug 94 23:14:26 -0600 Raw View
In article <rfgCuvK53.E3I@netcom.com>, rfg@netcom.com (Ronald F. Guilmette) writes:
>In article <1994Aug19.152125.2114@atlas.tntech.edu> rad6938@gemini.tntech.edu writes:
>>
>>I believe the latest version of the C++ draft standard states that enums are
>>now only required to be large enough to hold all values needed for the
>>enumerators declared (rounded up to the nearest bit with).(paraphrased)
>>This allows compilers to size the storage of enums according to need. Since
>>this is new to the standard, current compilers still use the ansi-C rules which
>>state that an enum is the size of an int.
>
>Correction... The ANSI C standard places no such requirement on variables
>having enum types. (It *does* require that enumeration literals act like
>type `int' literals, but that's something else entirely.) Some conformant
>ANSI C compilers *will* allocate only a single byte to hold variables having
>``small'' enum types.
Hmmm, I suppose I misinterpretted "Each enumerated type shall be compatible
with an integer type." I assumed this meant that enums should be able to
retain any integer type values assigned to them, etc.
----------------------------------------------------------------------------
Richard Deken Graduate student in electrical engineering
PGP public key available Tennessee Technological University
Internet: rad6938@gemini.tntech.edu Cookeville, TN, USA