[fpc-pascal] getmem

Sven Barth pascaldragon at googlemail.com
Tue May 24 21:22:43 CEST 2011


On 24.05.2011 17:22, Luis Fernando Del Aguila Mejía wrote:
> I know that you can use dynamic arrays (array of). Is better.
> But this does not understand and confuse me.
> Why to create an array of 3 elements (ANSIString) with GetMem, I have to
> put a size of 15 bytes (5*3) and not 12 bytes (4*3).?
> The program works with 15 bytes, but do not understand why not work with
> 12 bytes.
> Thanks.
>
> {$codepage utf8}
> Var LCad:^ansiString;
> Begin
> getmem(LCad,5*3); //ansistring is a pointer 4 bytes
> LCad[0]:='01234';
> LCad[1]:='56789';
> LCad[2]:='11111';
> Writeln(LCad[2]);
> freemem(LCad)
> End.

Question out of curiosity:
Is there a reason why you allocate an array "by hand"? Why don't you try 
this:

var
   LCad: array of AnsiString;
begin
   SetLength(LCad, 3);
   LCad[0] := '01234';
   LCad[1] := '56789';
   LCad[2] := '11111';
   Writeln(LCad[2]);
   SetLength(LCad, 0);
end.

Regards,
Sven



More information about the fpc-pascal mailing list