Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2012-02

dynamic array as a part of dynamically allocated records behavior

4 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

leledumbo

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));
    WriteLn(Length(PR^.avl));
  finally
    FreeMem(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?

Reply by email

Mattias Gaertner
to leledumbo
Show quoted text 22 lines
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);
Show quoted text 7 lines
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

Reply by email