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

Tony Maro tonym at nlisc.com
Fri Oct 18 14:18:52 EDT 2002


On Sun, 2002-09-29 at 20:44, Andrew Johnson wrote:
  
> 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

Andrew (and others):

I've successfully been using the above to manage arrays of records, but
I finally tried to do objects this way and it won't work.  Here's the
code snippett:

procedure TFrmMain.PrintChecks;
var
   MyPrints: ^TChkTransaction; // array of transactions to print
begin
     writeln('Reallocating memory');
     reallocmem(MyPrints, 0);
     writeln('Zeroed.');
end;

Notice, I define a pointer and then attempt to make sure that the array
is zero length to begin with (actually I added this just because I was
testing why it crashed when I tried to set it the first time.)

It access violations before the writeln('Zeroed.') is executed.

At this point, I can't think of any reason it would cause a crash.  All
I have is a pointer and I attempt to reallocate the memory for it...

It crashes on reallocmem regardless of the size I specify:
reallocmem(MyPrints, 0)
and
reallocmem(MyPrints,(2 * sizeof(TChkTransaction)))
both result in a crash.  Am I doing something wrong?

-Tony






More information about the Lazarus mailing list