[fpc-pascal] const records passed incorrectly

Martin Schreiber fpmse at bluewin.ch
Mon Nov 16 14:54:23 CET 2009


On Monday 16 November 2009 08:59:50 Michael Van Canneyt wrote:
>
> It is nowhere written in the Delphi specs that const parameters
> are passed by reference. It is often so, but is by no means guaranteed.
>
There is a sentence in Delphi 7 "Language Guide" in chapter 12 "Parameters and 
function results" which possibly belongs to the theme:
http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devcommon/programcontrolov_xml.html
"
Value and constant (const) parameters are passed by value or by reference, 
depending on the type and size of the parameter:
[...]
Sets, records, and static arrays of 1, 2, or 4 bytes are passed as 8-bit, 
16-bit, and 32bit values. Larger sets, records, and static arrays are passed 
as 32-bit pointers to the value. An exception to this rule is that records 
are always passed directly on the stack under the cdecl, stdcall, and 
safecall conventions; the size of a record passed this way is rounded upward 
to the nearest double-word boundary.
"
An example from Delphi windows.pas:
"
type
  POverlapped = ^TOverlapped;
  _OVERLAPPED = record
    Internal: DWORD;
    InternalHigh: DWORD;
    Offset: DWORD;
    OffsetHigh: DWORD;
    hEvent: THandle;
  end;
  {$EXTERNALSYM _OVERLAPPED}
  TOverlapped = _OVERLAPPED;
  OVERLAPPED = _OVERLAPPED;
  {$EXTERNALSYM OVERLAPPED}
[...]
function LockFileEx(hFile: THandle; dwFlags, dwReserved: DWORD;
  nNumberOfBytesToLockLow, nNumberOfBytesToLockHigh: DWORD;
  const lpOverlapped: TOverlapped): BOOL; stdcall;
"
And the C++-header:
"
BOOL WINAPI LockFileEx(
  __in        HANDLE hFile,
  __in        DWORD dwFlags,
  __reserved  DWORD dwReserved,
  __in        DWORD nNumberOfBytesToLockLow,
  __in        DWORD nNumberOfBytesToLockHigh,
  __inout     LPOVERLAPPED lpOverlapped
);
"
So it is stdcall -> "An exception to this rule is that records are always 
passed directly on the stack" but LPOVERLAPPED is a pointer? There is a not 
documented special rule for const?

Please don't shot me if it does not belong to the theme. ;-)

Martin



More information about the fpc-pascal mailing list