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

2012-02-25 Thread Mattias Gaertner
On Sat, 25 Feb 2012 04:23:24 -0800 (PST)
leledumbo leledumbo_c...@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
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


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

2012-02-25 Thread Jonas Maebe

On 25 Feb 2012, at 13:23, leledumbo wrote:

 The code results in AV in the last WriteLn. Is this correct (i.e. intended
 behavior)?

Yes.

 If yes, what is the correct way to have the PR^.avl initialized
 just like TR.avl and x?

Use new(PR) instead of GetMem(SizeOf(TRuang)), or alternatively call 
initialize(PR^) after calling getmem (which new() does for you automatically).


Jonas___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal