Name
GetMem Procedure
Syntax
procedure GetMem(var P: Pointer; Size: LongInt);
Description
GetMem allocates Size bytes of
dynamic memory and stores a pointer to the memory in
P. It does not initialize the allocated memory.
GetMem is not a real procedure.
Tips and Tricks
The memory that
GetMemallocates is not initialized. If you are allocating strings, dynamic arrays, orVariants, you should callInitializeor set the memory to zero withFillChar.GetMemin Delphi’s default memory manager is thread-safe, that is, you can callGetMemfrom multiple threads simultaneously, but only ifIsMultiThreadis True.If you are allocating a record or fixed-size array, call
Newinstead ofGetMem.A common use for
GetMemis to allocate an array where you do not know the array size at compile time. Although dynamic arrays have largely replaced the need for this trick, it is still commonly found in legacy Delphi code.
Example
// Create a grayscale palette, and return the palette handle. // Although you can create a palette with up to 255 shades of gray, // many video adapters can display only 15 or 16 bits per pixel, // which means 5 bits per gray shade, or 32 distinct shades of gray. function CreateGrayScalePalette(NumShades: Bytes); var LogPalette: PLogPalette; I: Integer; begin // TLogPalette already has room for one palette entry, so allocate // room for NumShades-1 additional palette entries. GetMem(LogPalette, SizeOf(TLogPalette) + (NumShades-1)*SizeOf(TPaletteEntry)); try LogPalette.palVersion ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access