Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2001-10

Another pointer related question

11 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Unknown author

The recent pointer thread has sparked my memory about another pointer related issue that I've come upon. It's probably something I'm doing wrong, or a lack of understanding on my point, but here goes anyway...

I have a program that uses pointers in a number of different places for various things. Some are straight pointers while others are pointers to a record type. I'm trying to make one generic pointer "disposal" routine for the all (as opposed to having separate procedures for each different type of pointer). I thought it should be fairly easy, but I guess it's not (for me anyway :-)

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?

TIA...

Jim

Procedure ReleaseMemory (PointerName : pointer; Index : longint);
begin
  if PointerName [Index]  NIL then
  begin
    dispose (PointerName [Index]);
    PointerName [Index] := NIL;
  end;
end;

Reply by email

Gabor DEAK JAHN
to Unknown author

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:

Reply by email

Unknown author
Show quoted text 5 lines
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.

I was afraid it was going to be something like that. I had hoped that by merely referencing the pointer I could just reclaim all the memory it was using, regardless of the structure. Sigh...

Thanks for your help.

Jim

Reply by email

Peter Vreman
to Unknown author
Show quoted text 9 lines
> 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. I was afraid it was going to be something like that. I had hoped that by merely referencing the pointer I could just reclaim all the memory it was using, regardless of the structure. Sigh...
Getmem(p,20000);
Freemem(p);

works fine. The heap manager knows about the size of each allocation and can therefor free the correct amount of memory. The following code will even give a runtime error:

Getmem(p,20000);
Freemem(p,30000);

Reply by email

Marco van de Voort
to Gabor DEAK JAHN: Interesting, I never thought about trying it as the documentation (Ref.pdf) is quite…

[ Charset ISO-8859-1 unsupported, converting... ]

Show quoted text 9 lines
At 10/22/01 10:04 PM, you wrote: Peter, > Freemem(p); > works fine. The heap manager knows about the size of each allocation and Interesting, I never thought about trying it as the documentation (Ref.pdf) is quite silent about this possibility... :-))

It is a Delphi extension, and might only be available in -S2, -Sd modes.

Reply by email

Gabor DEAK JAHN
to Unknown author: I was afraid it was going to be something like that. I had hoped that by merely…

At 10/22/01 03:05 PM, you wrote:

Jim,

I was afraid it was going to be something like that. I had hoped that by merely referencing the pointer I could just reclaim all the memory it was using, regardless of the structure. Sigh...

There are programing environments where memory management is more or less automated (garbage collection, reclaiming unused memory, a contemporary example is Java) but all this comes at a price: lower speed and more memory. For many situations, this might be an acceptable compromise but just to get rid of some typing, it's not worth it...

Bye,
Gábor

------------------------------------------------------------------- Gabor DEAK JAHN -- Budapest, Hungary.
E-mail:

Reply by email

Gabor DEAK JAHN
to Peter Vreman: Getmem(p,20000); Freemem(p); works fine. The heap manager knows about the size of each…

At 10/22/01 10:04 PM, you wrote:

Peter,

Freemem(p); works fine. The heap manager knows about the size of each allocation and

Interesting, I never thought about trying it as the documentation (Ref.pdf) is quite silent about this possibility... :-))

Bye,
Gábor

------------------------------------------------------------------- Gabor DEAK JAHN -- Budapest, Hungary.
E-mail:

Reply by email

Peter Vreman
Show quoted text 7 lines
> > Freemem(p); > > works fine. The heap manager knows about the size of each allocation and > > Interesting, I never thought about trying it as the documentation (Ref.pdf) > is quite silent about this possibility... :-)) It is a Delphi extension, and might only be available in -S2, -Sd modes.

It's available in all modes, as it is a normal RTL function.

Reply by email

Gabor DEAK JAHN
to Peter Vreman

At 10/23/01 06:49 AM, you wrote:

Peter,

It's available in all modes, as it is a normal RTL function.

FreeMem is a normal RTL function all right but the fact that you can omit its second parameter is not documented, at least not in the version of the Ref.doc I have. But you're right, it's not a Delphi extension, I've just tried it, it compiles without changing the {$MODE}.

Bye,
Gábor

------------------------------------------------------------------- Gabor DEAK JAHN -- Budapest, Hungary.
E-mail:

Reply by email

Jonas Maebe
to Gabor DEAK JAHN
Gabor DEAK JAHN wrote 6 lines
FreeMem is a normal RTL function all right but the fact that you can omit its second parameter is not documented, at least not in the version of the Ref.doc I have. But you're right, it's not a Delphi extension, I've just tried it, it compiles without changing the {$MODE}.

It is an extension introduced by Delphi, but since it is implemented as a normal procedure in the RTL, it is available in all modes. There is no procedure directive to allow a procedure only to be called in certain modes.

Jonas

Reply by email