Name

SysGetMem Function

Syntax

function SysGetMem(Size: Integer): Pointer;

Description

SysGetMem allocates Size bytes of memory using Delphi’s built-in memory manager. It returns a pointer to the newly allocated memory or nil for an error. The memory is not initialized.

SysGetMem is a real function.

Tips and Tricks

  • If you write your own memory manager, you can call SysGetMem to perform the memory allocation.

  • If you are not implementing a new memory manager, use New or GetMem, not SysGetMem, to allocate memory.

Example

// See SetMemoryManager for an explanation of this memory manager.

// Allocate Size bytes and return a pointer to the new memory.
// Return nil for errors, and Delphi will raise an exception.
// Allocate extra bytes for debugging.
function DebugGet(Size: Integer): Pointer;
var
  PArray: PIntegerArray;
begin
  Size := RoundUpSize(Size);

  // Allocate enough room for 2 guard words: at the start and end
  // of the block.
  PArray := SysGetMem(Size + 2*GuardSize);
  if PArray = nil then
    Result := nil
  else
  begin
    // Store the guard words.
    PArray[0] := AllocatedGuard;
    PArray[Size div GuardSize + 1] := AllocatedGuard;
    // Return a pointer to the memory just past the first guard.
    Result := @PArray[1];
  end;
end;

See Also

GetMem Procedure, GetMemory Function, GetMemoryManager Procedure, IsMemoryManagerSet Function, IsMultiThread Variable, New Procedure, SetMemoryManager Procedure, SysFreeMem Function, SysReallocMem Function, TMemoryManager Type

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.