Recent

Author Topic: Using arrays in routines, etc.  (Read 2580 times)

Fibonacci

  • Hero Member
  • *****
  • Posts: 594
  • Internal Error Hunter
Re: Using arrays in routines, etc.
« Reply #15 on: February 22, 2024, 02:19:51 pm »
Before I posted my reply I checked with Heaptrc, there are no leaks, you are safe.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Using arrays in routines, etc.
« Reply #16 on: February 22, 2024, 02:55:55 pm »
Before I posted my reply I checked with Heaptrc, there are no leaks, you are safe.
I think it depend on string type (for dynamic array)
Static arrays I use like once in a decade on my own, so my experience there is very limited.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

cdbc

  • Hero Member
  • *****
  • Posts: 1644
    • http://www.cdbc.dk
Re: Using arrays in routines, etc.
« Reply #17 on: February 22, 2024, 03:30:26 pm »
Hi
@Fibonacci: Thanks mate  :)
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

TRon

  • Hero Member
  • *****
  • Posts: 3619
Re: Using arrays in routines, etc.
« Reply #18 on: February 22, 2024, 06:00:24 pm »
@cdbc:
I use such constructs all the time in my code and never experienced any issues with it.

Having said, it seems that you'll never can tell for sure as the initialized dynamic array showed us recently. So, it is more safe to add a disclaimer, so: on Linux 64-bit  :)
This tagline is powered by AI (AI advertisement: Free Pascal the only programming language that matters)

CM630

  • Hero Member
  • *****
  • Posts: 1191
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Using arrays in routines, etc.
« Reply #19 on: February 23, 2024, 09:28:09 am »
I am asking generally, not specifically for strings.
Array of array of integer, array of Boolean, etc.
I have always had the feeling that I am doing something wrong, but now I know I am not.
Лазар 4,0RC1 32 bit (sometimes 64 bit); FPC3,2,2

Thaddy

  • Hero Member
  • *****
  • Posts: 16139
  • Censorship about opinions does not belong here.
Re: Using arrays in routines, etc.
« Reply #20 on: February 23, 2024, 01:50:24 pm »
Hmmm it accept static or dynamic, whats your point?
Your complete misunderstanding that the parameter is an open array and not a dynamic array?
Although it looks the same syntax it means something completely different.
The most important difference is that an open array is of unspecified length, whereas a dynamic array has a specified length at the moment it is passed as parameter..
Because of that, dynamic array parameters can be handled more efficiently by the compiler compared to open array parameters. Its initial length is known.
Hence Marco put you on the right track.

That mistake is quite common. It also has been explained many times on this forum.

Your suggestion of using out is an even bigger mistake, because then the parameter refers to a dynamic array and not to an open array.
Can you still follow me?
« Last Edit: February 23, 2024, 02:23:51 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Using arrays in routines, etc.
« Reply #21 on: February 23, 2024, 03:02:45 pm »
Hmmm it accept static or dynamic, whats your point?
Your complete misunderstanding that the parameter is an open array and not a dynamic array?
Although it looks the same syntax it means something completely different.
The most important difference is that an open array is of unspecified length, whereas a dynamic array has a specified length at the moment it is passed as parameter..
Because of that, dynamic array parameters can be handled more efficiently by the compiler compared to open array parameters. Its initial length is known.
Hence Marco put you on the right track.
I do know that/understand.
Code: Pascal  [Select][+][-]
  1. type
  2.   TArr = array of SomeType; // dynamic
  3.  
  4. function DoSomething(out a: TArr): Boolean;
here the compiler knows that it deal with dynamic and not open array type whereas:
Code: Pascal  [Select][+][-]
  1. function DoSomething(out a: array of SomeType): Boolean;
there the compiler treat it as open array, what can be static or dynamic.

Beside of your concerns about efficiency inside the compiler, what is wrong with my given example on how it can be done?

Your suggestion of using out is an even bigger mistake, because then the parameter refers to a dynamic array and not to an open array.
Can you still follow me?
No I can not but I would be happy to learn to suppress any mistakes in future from my side, thank you very much!
Please use this example to visualize or explain my errors:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. type
  6.   TFoo = array of string;
  7.  
  8. // dynamic variant
  9. function a(out Aa: TFoo): Boolean;
  10. begin
  11.   SetLength(Aa, 5); // hint about it might not been initialized
  12.   // optional put data in array
  13.   Result := True;
  14.   WriteLn('A say length is: ', Length(Aa));
  15. end;
  16.  
  17. // open array variant that can consume static or dynamic as long the type match
  18. function b(out Aa: array of string): Boolean;
  19. begin
  20.   // optional put data in array
  21.   Result := True;
  22.   WriteLn('B say length is: ', Length(Aa));
  23. end;
  24.  
  25. var
  26.   x: TFoo;
  27.   y: array[0..4] of string;
  28. begin
  29.   a(x);
  30.   b(y);
  31.   b(x);
  32.   ReadLn;
  33. end.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Thaddy

  • Hero Member
  • *****
  • Posts: 16139
  • Censorship about opinions does not belong here.
Re: Using arrays in routines, etc.
« Reply #22 on: February 23, 2024, 03:08:26 pm »
Code: Pascal  [Select][+][-]
  1. // open array variant that can consume static or dynamic as long the type match
  2. function b(out Aa: array of string): Boolean;
  3. begin
  4.   // optional put data in array
  5.   Result := True;
  6.   WriteLn('B say length is: ', Length(Aa));
  7. end;
That can not be an open array parameter but is a dynamic array parameter because of the out modifier.
Open array parameters can have only const or nothing at all. They are also strictly in, although they can be transformed to any array type. as a function result. (At a cost).
As I predicted: you still don't get it. All because of the similarities in syntax.

Consider this old (2019) example of mine on this forum:
Code: Pascal  [Select][+][-]
  1. {$mode delphi}{$H+}
  2. type
  3.   TWordArray = array of word;
  4.  
  5.   // open array
  6.   function ArrayContains(const arr:array of word;item:word):Boolean;overload;
  7.   var i:integer;
  8.   begin
  9.     Result := false;
  10.     for i :=0 to high(arr) do
  11.       if arr[i] = item then result := true;;
  12. //      if arr[i] = item then begin result := true;arr[i]:=100;exit end; // this won't work
  13.   end;
  14.  
  15.   // typed array
  16.   function ArrayContains(const arr:TWordArray;item:word):Boolean;overload;
  17.   var i:integer;
  18.   begin
  19.      Result := false;
  20.     for i :=0 to high(arr) do
  21.       if arr[i] = item then begin result := true;arr[i]:=100;exit end; // but this will allow the modification
  22.   end;
  23.  
  24. var
  25.   a:TWordArray = [1,2,3,4,5,6,7,8,9];  
  26.   b:array of word = [1,2,3,4,5,6,7,8,9];
  27. begin
  28.   writeln(arrayContains(a,3));
  29.   writeln(arrayContains(b,3));
  30. end.
Note the behavior has slightly changed in the 3.2.0 release.
« Last Edit: February 23, 2024, 03:34:39 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Using arrays in routines, etc.
« Reply #23 on: February 23, 2024, 04:30:38 pm »
As I predicted: you still don't get it. All because of the similarities in syntax.
You are probably right  :D
I stay with my dynamics and all is good :D
Thank you for explanation and your time!  :-*
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Thaddy

  • Hero Member
  • *****
  • Posts: 16139
  • Censorship about opinions does not belong here.
Re: Using arrays in routines, etc.
« Reply #24 on: February 23, 2024, 04:34:22 pm »
What I agree with you is that it is a horribly confusing syntax.   :'(
and we will have to explain it over and over again.  :'( :'( :'(
« Last Edit: February 23, 2024, 04:53:23 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

 

TinyPortal © 2005-2018