[fpc-pascal] const records passed incorrectly

Michael Van Canneyt michael at freepascal.org
Mon Nov 16 08:59:50 CET 2009

On Sun, 15 Nov 2009, Anthony Walter wrote:

> Hi all. I am new to fpc and linux in general, though I do have a long
> time experience with Delphi.
> I was writing some code basic system (using the libc library) when I
> ran across the following  problem .. some code:
> const
>  libc = 'libc.so.6';
> type
>  TTimeSpec = record
>    Sec: Cardinal;  { Seconds }
>    NSec: Cardinal; { Nanoseconds }
>  end;
>  PTimeSpec = ^TTimeSpec;
> function nanosleep(const RequestedTime: TTimeSpec; Remaining:
> PTimeSpec): Integer; cdecl; external libc;
> The problem with fpc and the above import is with the RequestedTime
> parameter, declared as a const record. In Delphi declaring a const
> record parameter cause the compiler to generate code to pass the
> record by reference (that is to say passing the address). In fpc,
> however this is not happening.


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.

If you want to pass it as a reference, you should use var. Not const.

Const means that the called procedure does not change the contents.
The compiler may then decide to pass it by reference if it is more
advantageous to do so, but it is not a rule that this is done.


More information about the fpc-pascal mailing list