Memory management

Top  Previous  Next

User interface > Translation options > Extended "System.pas" > Memory management


The function for the memory management GetMem, ReallocMem and FreeMem are defined in d2c_system.pas.


procedure GetMem(var P: Pointer; Size: Integer);

procedure FreeMem(var P: Pointer; Size: Integer = -1);

procedure ReallocMem(var P: Pointer; Size: Integer);


These functions are defined there by use of the C functions malloc, realloc and free.

It is often warned against mixing malloc and new. (Delphi2Cpp translates the construction of VCL classes with new.) But there is no danger, if both are used coherently,  i.e. that memory that was allocated with new is freed with delete and   memory that was allocated with malloc. is freed with free. Memory that was  allocated with malloc can be reallocated, but a reallocation of memory that was allocated with new is not possible. That's why it sometimes may be difficult to abstain from using malloc.


As already explained for the procedure SetString, the translator needs the Delphi declarations to adapt parameters accordingly. For the memory managing procedures there are additional implementations inserted in the C++ code, which are made as templates. E.g.:


template <class T>

void GetMem(T*& P, int Size)


  P = ( T* ) malloc(Size);



The advantage is, that there will be no problems with type casts.



BTW: the original System.pas contains only the functions:


function _FreeMem(P: Pointer): Integer;

function _GetMem(Size: Integer): Pointer;

function _ReallocMem(var P: Pointer; NewSize: Integer): Pointer;




This page belongs to the Delphi2Cpp Documentation

Delphi2Cpp home  Content