Delphi "default" keyword with post types in older versions of Delphi

I have this code in the Delphi Detours library that I am trying to execute on a port:

type TInstruction = record Archi: Byte; { CPUX32 or CPUX64 ! } AddrMode: Byte; { Address Mode } Addr: PByte; VirtualAddr: PByte; NextInst: PByte; { Pointer to the Next Instruction } OpCode: Byte; { OpCode Value } OpType: Byte; OpKind: Byte; OpTable: Byte; { tbOneByte,tbTwoByte,... } OperandFlags: Byte; Prefixes: Word; { Sets of Prf_xxx } ... end; var Inst: TInstruction; begin ... Inst := default (TInstruction); // <- Inst.Archi := CPUX; Pvt := PPointer(AIntf)^; // vTable ! PCode := PPointer(Pvt + Offset)^; // Code Entry ! Inst.NextInst := PCode; ... end; 

What does the keyword " default " do? I suppose something like:

 FillChar(Inst, SizeOf(TInstruction), 0); 

Is my assumption correct?

+7
delphi delphi-7 delphi-5
source share
1 answer

Default() is an undocumented internal function introduced to support generics. The Delphi generic design was heavily inspired by .net generators, and reading the analytic documentation for .net might come in handy: https://msdn.microsoft.com/en-GB/library/xwth0h0d.aspx p>

The purpose of Default() is to allow you to initialize the default variable. When working with the common types Default() you can do this for a variable whose type is shared.

If you want to reproduce the Default() behavior, follow these steps:

 Finalize(Inst); FillChar(Inst, SizeOf(Inst), 0); 

A call to Finalize necessary in the case of type control. That is, if the type is managed or contains any elements that are managed. Managed types include strings, dynamic arrays, interfaces, variants, anonymous methods, etc.

If the type does not contain managed types, the Finalize call may be omitted. This does not hurt to enable it, because the compiler will eliminate it if it is not needed. If you can be 100% sure that no managed type is assigned a value, you can also omit this call.

Initialization by default means the following:

  • Zero for numeric types.
  • A value with ordinal zero for the listed types.
  • False for boolean types.
  • #0 for character types.
  • Empty line for lines.
  • An empty option for Variant .
  • nil for classes, dynamic arrays, interfaces, and anonymous methods.
+13
source share

All Articles