[fpc-pascal] getmem

Jürgen Hestermann juergen.hestermann at gmx.de
Tue May 24 18:31:11 CEST 2011

Luis Fernando Del Aguila Mejía schrieb:
> Var LCad:^ansiString;
AnsiString is already a pointer. LCad is now a pointer to a pointer. 
That should read

Var LCad: AnsiString;

>   getmem(LCad,5*3);  //ansistring is a pointer 4 bytes
The memory allocation and freeing is done by the compiler automatically. 
Just assign a string or set a length with SetLength. There is no need to 
use getmen or new for AnsiStrings.

>   LCad[0]:='01234';
What should LCad[0] mean? LCad is not an array. You declared it as a 
pointer. If you declared LCad as AnsiString then you can write LCad[1] 
(which is the first character of the string) because Free Pascal does an 
automatic derefferencing of the AnsiString pointer but you cannot write 
LCad[0] because there is no character before the first one. Indexes of 
AnsiStrings are not zero-based.

>   freemem(LCad)
Again: No need to free memory for AnsiStrings. It's done automatically.

More information about the fpc-pascal mailing list