PDA

View Full Version : Size of enumerated types



masonwheeler
07-09-2008, 06:55 PM
If I'm saving an object to a stream, how do I know how many bytes a variable of an enumerated type takes? There are 7 possible values in the one I'm looking at. I can't find this information in the helpfile. (I can't find practically any information in the D2007 helpfile, for that matter. Who wrote this thing?!?)

Anyone know an authoritative answer?

Mason

Robert Kosek
07-09-2008, 07:49 PM
I won't call mine authoritative, but I know something of this.

Enumerated types are really just integer fields with defined constants associated in name and in incremental value. If you use them as just an enumerated type they should default to 1 byte in size, unless you have more than 255 elements. In a set, however, each element represents a bit toggle ... and so you need to divide your elements by 8 and round up (minimum of 1).

Thus an enumerated type of 16 elements is 1 byte in size, but a set of that type is 2 bytes in size.

Hope that helps. :)

arthurprs
07-09-2008, 09:30 PM
http://www.delphibasics.co.uk/RTL.asp?Name=$MinEnumSize

Andreaz
08-09-2008, 05:50 AM
Worth to note is that the default value of the minenumflag in freepascal isnt 1 as in delphi, wich has caused me some headace.

Robert Kosek
08-09-2008, 12:04 PM
Frankly, I never assume the size of anything when playing with memory or streams, but always use sizeof(TMyEnum) for safety. This way it would work between compilers, as each would report the size and you wouldn't actually have to know.

Brainer
08-09-2008, 06:02 PM
I'm not the guru, but I can say that I've done something like that. You simply do:

var
S: TStream;
X: TSimpleEnum;
begin
S.Write(X, SizeOf(TSimpleEnum));
end;

That's all you need, it worked for me. :)

masonwheeler
08-09-2008, 06:17 PM
Oh, so you can set the value with $minenumsize ? That's great! Thanks.

marcov
09-09-2008, 01:22 PM
Worth to note is that the default value of the minenumflag in freepascal isnt 1 as in delphi, wich has caused me some headace.

It is 1 in Delphi mode.