Recent

Author Topic: Copying records with dynamic arrays  (Read 4577 times)

Zvoni

  • Hero Member
  • *****
  • Posts: 2896
Re: Copying records with dynamic arrays
« Reply #45 on: January 29, 2025, 11:01:04 am »
Here some weird code (IMHO) for total dynamic 2D-Array
10 "Copy"'s instead of a loop with 100 assignments.
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. Var
  3.   i,j,x,l:Integer;
  4.   a:Integer;
  5.   a1:Array Of Integer;
  6.   a2:Array Of Array Of Integer;
  7.  
  8. begin
  9.   SetLength(a1,100);
  10.   a:=10;
  11.   l:=10;
  12.   SetLength(a2,a,l);
  13.   For x:=0 To 99 Do a1[x]:=x;
  14.   x:=Low(a1);
  15.   Repeat
  16.     a2[x div l]:=Copy(a1,x,l*SizeOf(Integer));
  17.     Inc(x,l);
  18.   until x>=High(a1);
  19.   For i:=0 To a-1 Do
  20.     Begin
  21.       For j:=0 To l-1 Do Write(a2[i,j],' ');
  22.       Writeln;
  23.     end;
  24.   Readln;
  25. end.

I had to use those helper-vars a and l, because of something weird going on with "High(a2[0])" --> that one returned "39" ?!?!??!
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

McDoob

  • New Member
  • *
  • Posts: 29
Re: Copying records with dynamic arrays
« Reply #46 on: January 29, 2025, 01:17:38 pm »
Here some weird code (IMHO) for total dynamic 2D-Array
10 "Copy"'s instead of a loop with 100 assignments.
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. Var
  3.   i,j,x,l:Integer;
  4.   a:Integer;
  5.   a1:Array Of Integer;
  6.   a2:Array Of Array Of Integer;
  7.  
  8. begin
  9.   SetLength(a1,100);
  10.   a:=10;
  11.   l:=10;
  12.   SetLength(a2,a,l);
  13.   For x:=0 To 99 Do a1[x]:=x;
  14.   x:=Low(a1);
  15.   Repeat
  16.     a2[x div l]:=Copy(a1,x,l*SizeOf(Integer));
  17.     Inc(x,l);
  18.   until x>=High(a1);
  19.   For i:=0 To a-1 Do
  20.     Begin
  21.       For j:=0 To l-1 Do Write(a2[i,j],' ');
  22.       Writeln;
  23.     end;
  24.   Readln;
  25. end.

I had to use those helper-vars a and l, because of something weird going on with "High(a2[0])" --> that one returned "39" ?!?!??!

Doesn't 'copy' do exactly that? Make a second (or third, or tenth) copy of the existing array? If so, wouldn't your code end with ten copies of the array in RAM? That would defeat the purpose of using dynamic arrays, at least in my use case. It's also why I've tried to avoid, and remove, 'copy' calls in my code. Am I wrong in my supposition?

-McD

alpine

  • Hero Member
  • *****
  • Posts: 1372
Re: Copying records with dynamic arrays
« Reply #47 on: January 29, 2025, 02:08:00 pm »
Doesn't 'copy' do exactly that? Make a second (or third, or tenth) copy of the existing array? If so, wouldn't your code end with ten copies of the array in RAM? That would defeat the purpose of using dynamic arrays, at least in my use case. It's also why I've tried to avoid, and remove, 'copy' calls in my code. Am I wrong in my supposition?
It's a bit more complicated. The new copy will be allocated on heap and the reference to it will be assigned into the first array level (ie. a2[ x div l ]). The memory pointed by the old reference will be freed automatically and there will be no leaks. But there will be constant allocations, copying and deallocations. Not the quickest thing and eventually will lead to something called fragmentation.

Dynamic arrays are references (hidden pointers) and thus each array with a base type of another dynarray actually consists of pointers to different heap chunks . In contrast, the multidimensional static array is always one continuous block of memory.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Zvoni

  • Hero Member
  • *****
  • Posts: 2896
Re: Copying records with dynamic arrays
« Reply #48 on: January 29, 2025, 02:14:59 pm »
In contrast, the multidimensional static array is always one continuous block of memory.
Correct.
As proven with my code for static arrays. Then it's one shot, and done
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

McDoob

  • New Member
  • *
  • Posts: 29
Re: Copying records with dynamic arrays
« Reply #49 on: January 29, 2025, 04:18:19 pm »
Today's update: I've finished converting my menu arrays to dynamic, as well. That was quite a bit more work than expected, as all of my menu handling routines were static-based and had to be converted to Low()/High().

Next up, I'll be studying and implementing the concepts introduced by TRon: advanced records, and OOP in general. Eventually, I'd like to have Window be totally self-contained. Then I can do something like:

Code: Pascal  [Select][+][-]
  1. type
  2.   WindowType = record
  3.     ...
  4.   end;
  5.   WindowArrayType = record
  6.     ...
  7.   end;
  8.  
  9. var
  10.   OpenWindows:WindowArrayType;
  11.   MyWindowID:byte;
  12.  
  13. begin
  14.   OpenWindows.Create('My Window',MyWindowID,1,1,79,24,true);
  15.   OpenWindows[MyWindowID].AddContent(0,0,'This is my window.')
  16.   OpenWindows[MyWindowID].AddContent(0,1,'There are many windows like it.');
  17.   OpenWindows[MyWindowID.AddContent(0,2,'But this one is mine.');
  18.   OpenWindows[MyWindowID].DrawAll;
  19.   OpenWindows[MyWindowID].Destroy;
I'll probably make use of parent/child properties and other stuff I currently barely understand...

Then, when that's working:
Code: Pascal  [Select][+][-]
  1. type
  2.   MenuType = record
  3.     ...
  4.   end;
  5.  
  6. var
  7.     MyMenu:MenuType;
  8.  
  9. begin
  10.   MyMenu.Create('My Menu');
  11.   MyMenu.AddItem('Something', something); //given that 'something' is a proc
  12.   MyMenu.Select(0,0); //would execute 'something'
Et cetera...but this will take some time to complete. This will all be a bit more complex than I'm used to.

Thanks to everyone, again, for all the help!
-McDoob
« Last Edit: January 29, 2025, 05:03:17 pm by McDoob »

TRon

  • Hero Member
  • *****
  • Posts: 4139
Re: Copying records with dynamic arrays
« Reply #50 on: January 30, 2025, 07:07:14 am »
@McDoob:
Note that there exist something named Free Vision.

Also note that advanced records do not offer full OOP functionality, see also reference manual:
Quote
Some of the restrictions when compared to classes or objects are obvious from the syntax diagram:
-  No inheritance of records.
-  No published and protected section exists.
-  Constructors or destructors cannot be defined.
-  Class methods (if one can name them so) require the static keyword.
-  Methods cannot be virtual or abstract – this is a consequence of the fact that there is no inheritance.

But going the direction were you seem to be heading is also not a bad thing as you might be able to learn a lot from it. It can at least be a smooth introduction into some OOP functionality.

2 cents.
Today is tomorrow's yesterday.

McDoob

  • New Member
  • *
  • Posts: 29
Re: Copying records with dynamic arrays
« Reply #51 on: January 30, 2025, 12:35:49 pm »
@McDoob:
Note that there exist something named Free Vision.
I wasn't aware that Turbo Vision had been modernized by the Free Pascal team, but I was aware that there were ready-made options for what I'm trying to accomplish, e.g. nCurses & nCrt/oCrt.
Quote
Also note that advanced records do not offer full OOP functionality, see also reference manual:
I've been perusing the reference manual since you brought up the advanced records concept, and I had noticed the section you quoted. I figured I'd have to use objects rather than records.
Quote
But going the direction were you seem to be heading is also not a bad thing as you might be able to learn a lot from it. It can at least be a smooth introduction into some OOP functionality.
Precisely why I'm trying to do this myself! If I'm ever going to write the game I keep falling asleep imagining, I need to learn how to code sufficiently efficiently in my language of choice!

Regarding Free Vision: since I'm working in a Debian-based OS, I found I'd have to install the fp-units-fv package. This took me down a whole new rabbit hole of available fp-units-* packages that may or may not be of use to me in the future...thanks for that.  :D

-McDoob

 

TinyPortal © 2005-2018