Dalija Prasnikar
Dalija Prasnikar

Reputation: 28516

Minimum enum size not working in Delphi

In order to interact with C/C++ structs I have to set minimum enum size in Delphi record to 4. However, I cannot get it to work properly.

type
  TFooEnum = (e1, e2, e3);

  {$Z4}
  TFoo = record
  public
    f: TFooEnum;
    b: byte;
  end;
  {$Z1}

  writeln(SizeOf(TFoo)); -> output is 2 instead of 8

I have also tried with {$Z+} and {$MINENUMSIZE 4} It only works if I set it in compiler options for whole project, but that messes up other records memory layouts where enums have to be 1 byte in size.

Upvotes: 2

Views: 1230

Answers (1)

David Heffernan
David Heffernan

Reputation: 613013

The size is a property of the enumerated type itself. So you must declare your enumerated type like so:

type
  {$Z4}
  TFooEnum = (e1, e2, e3);
  {$Z1}

The way to think about this is that record layout, for aligned records, is determined entirely by size and alignment properties of the record's members. The $MINENUMSIZE directive only inflences layour indirectly, by way of its impact on the size and alignment properties of any enumerated type members.

Upvotes: 7

Related Questions