From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: 'Size hack for enumerated types Date: Sat, 05 Jul 2014 22:47:50 +0100 Organization: A noiseless patient Spider Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx05.eternal-september.org; posting-host="117da9042fa4d6f5956a9b8f72035635"; logging-data="8158"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/bS7u3RJyqS+lMaOyaKajeVb+IfdpxBMo=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (darwin) Cancel-Lock: sha1:lrtF/8i+cyS1qFhecUUncyXkD0M= sha1:YnsTP1J6humidjxMlySwoxmz0Jk= Xref: news.eternal-september.org comp.lang.ada:20735 Date: 2014-07-05T22:47:50+01:00 List-Id: Victor Porton writes: > I have written the following code: > > type Flag_Type is (Libxml_Error_Save, > Libxml_Structured_Error_Save, > URI_Interning, > WWW_Skip_Init_Finish); > for Flag_Type'Size use Interfaces.C.int'Size; -- hack > for Flag_Type use (Libxml_Error_Save => 1, > Libxml_Structured_Error_Save => 2, > URI_Interning => 3, > WWW_Skip_Init_Finish => 4); > > The code above is intended to be able to pass flag variables into > certain C functions (accepting enum types). > > But it seems that the above code has the drawback of storing in 4 or 8 > bytes instead of one byte. Well, you specified 'Size to be (almost certainly) 32 bits, so it's not surprising that that's what the compiler does. But there's a difference between the minimum number of bits required to hold a *value* of the type (3 in your example) and the number of bits the compiler uses when holding an *object* of the type, which will be what is most efficient. GNAT has 'Object_Size. > What is a better way to do this? Just use convention C: type Flag_Type is (Libxml_Error_Save, Libxml_Structured_Error_Save, URI_Interning, WWW_Skip_Init_Finish) with Convention => C; and let the compiler worry about sizes.