[fpc-pascal]Pointer Help

Jonas Maebe jonas at zeus.rug.ac.be
Mon Feb 10 10:00:10 CET 2003


On zondag, feb 9, 2003, at 15:06 Europe/Brussels, Anton Tichawa wrote:

> That's exactly what I meant: I'm not sure if fpc is able to keep track 
> of the
> destruction of the strings in case of AnsiString.

It is, as long as the type of the variable is clear when getmem/freemem 
is called (getmem also needs to know it, because if you use 
ansistrings, the memory must be cleared after it's allocated). What I 
mean is that

***
type
   ta = array[0..9] of ansistring;
   pa = ^ta;

var
   p: pa;
begin
   getmem(p,10*sizeof(ansistring));
   readln(p^[0]);
   freemem(p);
end;
***

Will correctly release all memory. If you change the freemem into

freemem(pointer(p));

however, then the memory allocated for the array will be freed, but the 
ansistring won't be finalized. This may seem strange at first sight, 
but it can occur when people construct generic datacontainers. It's one 
of the reason I dislike ansistrings and all the other things that 
require initialization/finalization: it completely breaks when you go 
low-level and as such it doesn't mix well with legacy Pascal code (and 
as your question indicates: it's not always clear at first sight 
whether a certain construct will behave correctly, you just have to 
know how far the compiler magic extends).


Jonas




More information about the fpc-pascal mailing list