Results 1 to 8 of 8

Thread: Size of enumerated types

  1. #1

    Size of enumerated types

    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

  2. #2

    Size of enumerated types

    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.

  3. #3

    Size of enumerated types

    http://www.delphibasics.co.uk/RTL.asp?Name=$MinEnumSize
    From brazil (:

    Pascal pownz!

  4. #4

    Size of enumerated types

    Worth to note is that the default value of the minenumflag in freepascal isnt 1 as in delphi, wich has caused me some headace.
    Amnoxx

    Oh, and this code appears to be an approximate replacement for return(random() & 0x01);

    Phoenix Wiki
    http://www.phoenixlib.net/

    Phoenix Forum
    http://www.pascalgamedevelopment.com/viewforum.php?f=71

  5. #5

    Size of enumerated types

    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.

  6. #6

    Size of enumerated types

    I'm not the guru, but I can say that I've done something like that. You simply do:
    [pascal]
    var
    S: TStream;
    X: TSimpleEnum;
    begin
    S.Write(X, SizeOf(TSimpleEnum));
    end;
    [/pascal]
    That's all you need, it worked for me.

  7. #7

    Size of enumerated types

    Oh, so you can set the value with $minenumsize ? That's great! Thanks.

  8. #8

    Size of enumerated types

    Quote Originally Posted by Andreaz
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •