[fpc-pascal]Another pointer related question

Gabor DEAK JAHN djg at tramontana.co.hu
Mon Oct 22 18:40:21 CEST 2001


At 10/22/01 10:41 AM, you wrote:

Jim,

 > I created the following Win32/GO32 procedure but the compiler doesn't seem
 > to like it. Is there something obvious that I'm doing wrong here?

Dispose is supposed to free the whole memory block pointed by the pointer,
so it has to know exactly what structure it references to (otherwise it
wouldn't know how many bytes were occupied originally). Consequently, it
cannot be used with a generic pointer parameter, only with some actual,
typed one.

There is an alternative, GetMem and FreeMem but then you have to keep track
of the amount of memory you allocated yourself, so the end result is about
the same.

If you want to use the same procedure to dispose of different types, you can
overload the procedure, providing parallel implementations for each of the
types you want to dispose of:

Procedure DisposeMemory (p : P1);
begin
   if p <> NIL then
   begin
     dispose (p);
     p := NIL;
   end;
end;

Procedure DisposeMemory (p : P2);
begin
   if p <> NIL then
   begin
     dispose (p);
     p := NIL;
   end;
end;

You can spare some typing if you use macros:

{$MACRO ON}
{$DEFINE DoDispose := if p <> nil then begin dispose (p); p := nil; end}
procedure DisposeMemory (p : P1); begin DoDispose; end;
procedure DisposeMemory (p : P2); begin DoDispose; end;
procedure DisposeMemory (p : P3); begin DoDispose; end;



Bye,
    Gábor

-------------------------------------------------------------------
Gabor DEAK JAHN -- Budapest, Hungary.
E-mail: djg at tramontana.co.hu





More information about the fpc-pascal mailing list