Description |
The GetMem procedure attempts to get the specified StorageSize bytes of storage, storing a pointer to the storage in StoragePointer.
If the allocation fails, then a EOutOfMemory exception is raised.
The storage is not initialised in any way.
|
|
Notes |
The GetMem procedure is Thread safe as long as IsMultiThread is true.
It is better to use New to allocate storage for records - the example is for illustration of GetMem and pointer manipulation.
|
|
Related commands |
Dispose |
|
Dispose of storage used by a pointer type variable |
FillChar |
|
Fills out a section of storage with a fill character or byte value |
FreeMem |
|
Free memory storage used by a variable |
New |
|
Create a new pointer type variable |
ReallocMem |
|
Reallocate an existing block of storage |
|
|
|
Example code : Get storage for 3 records and use this storage |
// Full Unit code. // ----------------------------------------------------------- // You must store this code in a unit called Unit1 with a form // called Form1 that has an OnCreate event called FormCreate. unit Unit1; interface uses // The System unit does not need to be defined Forms, Dialogs; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} // Include form definitions procedure TForm1.FormCreate(Sender: TObject); type
TRecord = Record
name : string[10];
age : Byte;
end;
var
recPointer : ^TRecord;
begin // Allocate storage for three records // Note : It is better to use New for this // It is used here for illustration purposes only
GetMem(recPointer, 3 * SizeOf(TRecord));
// Fill out these 3 records with values
recPointer.name := 'Brian';
recPointer.age := 23;
Inc(recPointer);
recPointer.name := 'Jim';
recPointer.age := 55;
Inc(recPointer);
recPointer.name := 'Sally';
recPointer.age := 38;
// Now display these values
Dec(recPointer, 2);
ShowMessageFmt('%s is %d',[recPointer.name, recPointer.age]);
Inc(recPointer);
ShowMessageFmt('%s is %d',[recPointer.name, recPointer.age]);
Inc(recPointer);
ShowMessageFmt('%s is %d',[recPointer.name, recPointer.age]);
end; end.
|
Hide full unit code |
Brian is 23
Jim is 55
Sally is 38
|
|