Sign in Join

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

Mailing lists / fpc-pascal / 2011-11

More fun with dynamic arrays

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Unknown author

This program:

Program TestMem ;
Const ASize = 1000 ;
Type DataA = Array Of Longint ;
Var A : DataA ;
Procedure Init(Out X : DataA ; Const N : Longint) ;
Begin
  Writeln(Length(X)) ; { Writes: 0 }
End ;
Begin
  SetLength(A, ASize) ;
  Writeln(Length(A)) ; { Writes: 1000 as expected }
  Init(A, ASize) ;
End.

gives the results noted in the comments.
The correct results are obtained if
1) I substitute "Var" for "Out". This leads to the warning that X is not initialized.
2) I substitute "Array Of Longint" for "DataA".
What am I doing wrong this time?

Andrew Bennett

Reply by email

Jürgen Hestermann
to Unknown author
Show quoted text 5 lines
Procedure Init(Out X : DataA ; Const N : Longint) ; 1) I substitute "Var" for "Out". This leads to the warning that X is not initialized.

Which is correct. If you define a parameter to be for "out"put only the compiler does exactly this.

2) I substitute "Array Of Longint" for "DataA".

I don't know why this should work if you still use "Out".

Reply by email

leledumbo
to Unknown author: This program: Program TestMem ; Const ASize = 1000 ; Type DataA = Array Of Longint ; Var…

The correct results are obtained if

1) I substitute "Var" for "Out". This leads to the warning that X is not

initialized.

"var" could be used to treat the parameter as in-out, so the warning is expected if you haven't initialized the variable before. As stated in the docs (http://www.freepascal.org/docs-html/ref/refsu57.html): "The initial value of the parameter on function entry is discarded, and should not be used." which is why you get 0.

2) I substitute "Array Of Longint" for "DataA".

That would make X parameter to be treated as "open array of longint" not "dynamic array of longint"

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/More-fun-with-dynamic-arrays-tp5002872p5006382.html Sent from the Free Pascal - General mailing list .

Reply by email

Unknown author

On Fri, 18 Nov 2011 23:11:14 -0800 (PST) leledumbo wrote

re use of "Out" parameters

... As stated in the docs (http://www.freepascal.org/docs-html/ref/refsu57.html): "The initial value of the parameter on function entry is discarded, and should not be used." which is why you get 0.

It seems a bit drastic to me to discard the existence of the dynamic array as well as the (non-existent) values stored in it! Presumably, all will be well if I size the array inside the initializing routine, breaking my rule that the routine that makes the array must also contain the code to get rid of it.

Show quoted text 5 lines
> 2) I substitute "Array Of Longint" for "DataA". That would make X parameter to be treated as "open array of longint" not "dynamic array of longint"

That is a distinction too subtle for me!

Andrew Bennett

Reply by email

leledumbo
to Unknown author
It seems a bit drastic to me to discard the existence of the dynamic array

as well as the (non-existent) values stored in it! Presumably, all will be well
if I size the array inside the initializing routine, breaking my rule that the
routine that makes the array must also contain the code to get rid of it.

Essentially, dynamic arrays are just pointers. If you intend to create something to initialize it, better use function that returns the initialized array. e.g.:

function Init(const N: LongInt): DataA;
begin
  SetLength(Result,N);
  ... // initialize Result
end;

you can then simply:

var
  a: DataA;
begin
  ...
  a := Init(N);
  ...
end.
That is a distinction too subtle for me!

I agree, but this is how Borland designed the language, in which we follow for compatibility reason.

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/More-fun-with-dynamic-arrays-tp5002872p5007151.html Sent from the Free Pascal - General mailing list .

Reply by email