[fpc-pascal] dynamic array as a part of dynamically allocated records behavior

Mattias Gaertner nc-gaertnma at netcologne.de
Sat Feb 25 13:28:30 CET 2012


On Sat, 25 Feb 2012 04:23:24 -0800 (PST)
leledumbo <leledumbo_cool at yahoo.co.id> wrote:

> Consider the following program:
> 
> {$mode objfpc}
> type
>   TJam = array of Integer;
> 
>   TRuang = record
>     nm_ruang: string;
>     avl: TJam;
>   end;
> 
>   PRuang = ^TRuang;
> 
> var
>   x: array of integer;
>   TR: TRuang;
>   PR: PRuang;
> begin
>   try
>     WriteLn(Length(x));
>     WriteLn(Length(TR.avl));
>     PR := GetMem(SizeOf(TRuang));

New(PR);

>     WriteLn(Length(PR^.avl));
>   finally
>     FreeMem(PR);

Dispose(PR);

>   end;
> end.
> 
> 
> The code results in AV in the last WriteLn. Is this correct (i.e. intended
> behavior)? If yes, what is the correct way to have the PR^.avl initialized
> just like TR.avl and x?

New is GetMem+Initialization.
Dispose is Finaliziation+FreeMem.

Mattias



More information about the fpc-pascal mailing list