[fpc-pascal] Re: Code conversion question - alloc and dealloc memory
Michael Van Canneyt
michael at freepascal.org
Thu Oct 15 11:01:05 CEST 2009
On Thu, 15 Oct 2009, Graeme Geldenhuys wrote:
> On 15/10/2009, Graeme Geldenhuys <graemeg.lists at gmail.com> wrote:
>>
>> ------------------------
>> function AllocateMemory( const Size: longint ): pointer;
>>
>> procedure DeallocateMemory( Var P: pointer );
>
> OK, I managed to convert those two functions to the following.. it now
> compiles under my 64bit FPC.
>
> -----------------------------
> function AllocateMemory( const Size: ValUInt ): pointer;
> begin
> GetMem( Result, size + sizeof( Size ) );
> PtrUInt(Result^) := Size;
> inc( Result, sizeof( Size ) );
> end;
>
> procedure DeallocateMemory( Var P: pointer );
> var
> Size: ValUInt;
> begin
> if P = nil then
> exit;
>
> dec( P, sizeof( size ) );
> Size := ValUInt(P^);
> FreeMem( P, Size + sizeof( Size ) );
> P := nil;
> end;
> -----------------------------
>
> But based on Florian's reply, I assume I don't need to bother using
> these methods, because FreeMem() will automatically know much much
> memory to deallocate.
Yes.
Michael.
More information about the fpc-pascal
mailing list