Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enumerated Type: Limit to number of items?

Tags:

delphi

Is there a limit in Delphi to the number of items you can have in an enumerated type? I need to create an enumerated type that might have several hundred items, and want to make sure there is not a limit at 255 items for example.

type 
  TMyType = (mtOne, mtTwo, mtThree, ..., mtThreeHundred);
like image 809
David Avatar asked Dec 17 '22 20:12

David


2 Answers

I believe the theoretical limit is 2^32 items; but in practice, RTTI generation is normally the limit, as RTTI can't exceed 65535 bytes to store everything, including the names of the enumeration elements; the names are stored in UTF-8, so it's not too bad.

On the other hand, enumerations with explicit values for the elements don't have full RTTI, so you can evade the limit that way. Here's a program which creates a source file with 500,001 enumeration elements, which itself compiles:

var
  i: Integer;
begin
  Writeln('type');
  Writeln('  E = (');
  for i := 1 to 500000 do
    Writeln('  x_', i, ' = ', i, ',');
  Writeln('x_last);');
  Writeln('begin');
  Writeln('end.');
end.

The output of this program takes some time to compile with dcc32 because the Delphi compiler uses a hash table with only 32 buckets for checking for enumeration identifier duplicates, and a hash table with only 256 buckets for file-level scope, which (in the absence of {$SCOPEDENUMS ON}) is where enumeration identifiers are added.

like image 90
Barry Kelly Avatar answered Dec 23 '22 07:12

Barry Kelly


I found a maximum of 65535 items in a german Delphi book.

After some digging in the documenation I found the respective section:

Enumerated Types

An enumerated type is stored as an unsigned byte if the enumeration has no more than 256 values and the type was declared in the {$Z1} state (the default). If an enumerated type has more than 256 values, or if the type was declared in the {$Z2} state, it is stored as an unsigned word. If an enumerated type is declared in the {$Z4} state, it is stored as an unsigned double-word.

So in fact there should be a possible maximum of 4294967295 ($FFFFFFFF) items.

like image 43
splash Avatar answered Dec 23 '22 06:12

splash