|
GetMem/FreeMem |
Top Previous Next |
|
What is translated > Special RTL/VCL-functions > GetMem/FreeMem Delphi2C# provides runtime support for Delphi-style memory functions such as GetMem, FreeMem, and ReallocMem.
These functions are used by translated code when Delphi code explicitly allocates, releases, or resizes memory blocks. They are especially relevant for translated pointer code, typed pointers, untyped pointers, PChar, and record-pointer scenarios.
GetMem
GetMem allocates memory and assigns the resulting pointer to the supplied pointer variable.
Delphi:
GetMem(P, Size);
C#:
GetMem(ref p, size);
For untyped pointers and raw byte pointers, GetMem allocates a raw memory block.
For typed pointers, Delphi2C# distinguishes between byte-addressable and non-byte-addressable types. If the target type can safely be represented as raw bytes, the runtime may use raw memory. If the type contains managed fields, such as string references, the runtime uses managed typed backing storage instead.
This allows translated pointer code to work with both low-level byte-compatible records and higher-level translated records that contain managed C# fields.
FreeMem
FreeMem releases memory previously allocated by GetMem.
Delphi:
FreeMem(P);
C#:
FreeMem(ref p);
The ref form is preferred because it allows the runtime to clear the pointer after releasing the memory.
Not every pointer view owns memory. A pointer may refer to a managed array, a local variable represented by DelphiCell<T>, a string view, or a borrowed memory block. In such cases there may be no native memory to release. The runtime handles this distinction and clears the pointer variable safely.
ReallocMem
ReallocMem resizes an existing memory block while preserving the existing contents as far as possible.
Delphi:
ReallocMem(P, NewSize);
C#:
ReallocMem(ref p, newSize);
The runtime allocates a new block or backing store, copies the existing contents, and then releases the old storage if it owns it. For typed pointers, the same byte-addressability rules as for GetMem apply. |
|
This page belongs to the Delphi2C# Documentation |
Delphi2C# home Content |