[fpc-pascal] getmem

Jonas Maebe jonas.maebe at elis.ugent.be
Tue May 24 21:36:15 CEST 2011


On 24 May 2011, at 20:57, Luis Fernando Del Aguila Mejía wrote:

> Yes  something  like that.  I'm  trying to  emulate  something  like  this:
> 
> VarLCad = array[0..2] of ansistring;
> Begin
>  LCad[0]:='01234';
>  LCad[1]:='56789';
>  LCad[2]:='11111';
>  Writeln(LCad[2]);
> End.
> 
> But using GetMem(LCad,4*3) for the array. (4 bytes for the ansistring pointer).
> This can be done with: TList. But I write these simple programs, to know how compiler works.

Using automated types in combination with manual memory management is hard. Figuring out "how the compiler works" by writing simple programs is extremely unlikely to work out in this context, because the reference counting of the automated types causes lots of extra code to be generated behind the scenes. Additionally, the compiler makes lots of assumptions which you are breaking if you use getmem and don't add the necessary initialization/finalization code. I don't think there is currently any documentation about how to combine the two.


Jonas

PS: using a TList is just as unsafe as using getmem. Use at least a tstringlist, or a dynamic array as others have suggested (and dynamic arrays themselves are also automated types, so make sure you don't put those either into a tlist/getmem area)


More information about the fpc-pascal mailing list