5

Check this demo source from the excellent Detour library:

implementation

{$R *.dfm}

var
  TrampolineGetMemory: function(Size: NativeInt): Pointer;
cdecl = nil;

Please look at the cdecl = nil; statement. What does it mean in this context?

Note - I already know that cdecl stands for a calling convention.

Edwin Yip
  • 4,089
  • 4
  • 40
  • 86

2 Answers2

7

This is just another way to initialize the variable. For example:

program Project1;

{$APPTYPE CONSOLE}

var
  i : integer = 5;
begin
  WriteLn(i);
  ReadLn;
end.

it may be clearer if it was written on one line as

var
  TrampolineGetMemory: function(Size: NativeInt): Pointer; cdecl = nil;

Or maybe even better if a type was defined :

type
  TTrampolineGetMemory = function(Size: NativeInt): Pointer; cdecl;

//... 
 var
   TrampolineGetMemory: TTrampolineGetMemory = nil;
J...
  • 30,968
  • 6
  • 66
  • 143
  • Note that it completely redundant as global variables are initialised to `0/nil/false` anyway - see [Are delphi variables initialized with a value by default?](http://stackoverflow.com/questions/132725/are-delphi-variables-initialized-with-a-value-by-default). This is like initialising an event handler to nil or a boolean field to false in a constructor - completely pointless – Gerry Coll Apr 19 '16 at 21:19
  • @GerryColl Indeed it is. Sometimes coding standards will dictate this sort of thing, though (I guess?). Could be explicit for clarity, or possibly just a reflex carried over from locals. It's not necessary, but I'm not sure I'd go so far as to call it wrong. – J... Apr 19 '16 at 22:44
  • Seeing initialisation to 0 in a constructor is a code smell to me - it implies that the original coder either didn't understand Delphi, or couldn't be bothered to find out what the default is. – Gerry Coll Apr 19 '16 at 23:34
  • 4
    @Gerry or wanted the reader to be in no doubt and not have to think – David Heffernan Apr 20 '16 at 04:52
7

TrampolineGetMemory is a procedural variable initialized to nil.

It's easier to see if rewritten like

type
  TTrampolineGetMemory = function(Size: NativeInt): Pointer; cdecl;
var
  TrampolineGetMemory: TTrampolineGetMemory = nil;
Uli Gerhardt
  • 13,748
  • 1
  • 45
  • 83