[lazarus] More of a FPC question but... dynamic arrays

Andrew Johnson aj_genius at hotmail.com
Sun Sep 29 21:39:56 EDT 2002


On 29 Sep 2002 20:11:42 -0500
"Tony Maro" <tonym at nlisc.com> wrote:

>In Delphi 4 I used dynamically sized arrays using setlength to resize
>them as needed.

>I haven't been able to get it to work in FPC / Lazarus, and it looks
>like the setlength function isn't there.  In fact, defining the array
>using:

>var
>	MyArray: array of TObject;

>Doesn't work either...

>This was a feature they added in Delphi 4.  I've got over 60,000 lines
>of code that rely heavily on this that I was looking to migrate to
>Lazarus.

>Have I just missed something, or must I write my own linked list manager
>or use a TCollection to get this to work?

Well this anoyed me to no end when I started working with FPC, but basically 
Dyn. Arrays don't exist in the FPC 1.0 branch, only in the unstable 1.1 
branch. However, C dynamic arrays, aka Pointer arrays, do work. I am 
guessing you do not know how to do them, but it is really quite simple:

var
  myarray : ^TObject;
begin
  ...//do stuff
  reallocmem(myarray, 4*SizeOf(TObject));//allocmem to handle 4 objects
  myarray[2] := thisobject;//set the 3 entry
  ...//do stuff.
  reallocmem(myarray, 0);//free memory
end;

you simply use reallocmem in place of setlength, and multiply the length by 
the sizeof the type it contains.. at one point I actually created wrappers 
arround this to make more like Delphi dynamic arrays..but its been awhile so 
I do not know if I still have it. essentially though you should be able to 
port a lot of code with ease. by doing some sort of wrappers.. etc.

Andrew

_________________________________________________________________
MSN Photos is the easiest way to share and print your photos: 
http://photos.msn.com/support/worldwide.aspx






More information about the Lazarus mailing list