[fpc-devel] How FPC handles dynamic arrays with const modifier?

Maciej Izak hnb.code at gmail.com
Mon Aug 12 23:32:07 CEST 2013


Hi, I have some code (very important for next version of
Generics.Collections):

------ code ------
{$IFDEF FPC}{$MODE DELPHI}{$ENDIF}
{$APPTYPE CONSOLE}

uses
  Types;

{$IFDEF FPC}
function DynArraySize(p : pointer): tdynarrayindex; external name
'FPC_DYNARRAY_LENGTH';
{$ENDIF}

procedure Foo(A: Pointer);
begin
  WriteLn(DynArraySize(A));
end;

type
  TFoo = procedure(const A: TIntegerDynArray);

var
  Test: TFoo;
begin
  Test := @Foo;
  Test(TIntegerDynArray.Create(1, 2, 3, 4));
  ReadLn;
end.
------ code ------

In Delphi all is ok (DynArraySize in Foo returns 4). In FPC DynArraySize
return 1 (and program crash at the end). Is dynamic array with const
modifier in FPC passed by value? With small modifications especially for
FPC (by adding constref) all works fine:

------ code ------
procedure Foo(constref A: Pointer);
begin
  WriteLn(DynArraySize(A));
end;

type
  TFoo = procedure(constref A: TIntegerDynArray);
------ code ------

I can live with constref, but I'm much worried by this behavior (in version
without constref). Why my code don't work in both compilers same? I would
like to understand...

Regards,
HNB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20130812/cc17ff87/attachment.html>


More information about the fpc-devel mailing list