Disposes of a dynamic variable of a given size.
procedure FreeMem(var P: Pointer); overload; procedure FreeMem(var P: Pointer; Size: Integer); overload;
FreeMem(void * P); FreeMem(void * P, int Size);
System
In Delphi code, FreeMem destroys the variable referenced by P and returns its memory to the heap. If P does not point to memory in the heap, a runtime error occurs. If P points to a structure that includes long strings, variants, dynamic arrays, or interfaces, call Finalize before calling Freemem.
P is a variable of any pointer type previously assigned by the GetMem procedure.
Size specifies the size in bytes of the dynamic variable to dispose of; if specified, it must be exactly the number of bytes previously allocated to that variable by GetMem.
After calling FreeMem, the value of P is undefined.
Delphi Examples:
{ The following example uses a button, a string grid, and an Open dialog box on a form. When the button is clicked, the user is prompted for a filename. When the user clicks OK, the specified file is opened, read into a buffer, and closed. Then the buffer is displayed in two columns of the string grid. The first column contains the character values in the buffer. The second column contains the numeric values of the characters in the buffer. } procedure TForm1.Button1Click(Sender: TObject); var iFileHandle: Integer; iFileLength: Integer; iBytesRead: Integer; Buffer: PChar; i: Integer; begin if OpenDialog1.Execute then begin try iFileHandle := SysUtils.FileOpen(OpenDialog1.FileName, fmOpenRead); iFileLength := SysUtils.FileSeek(iFileHandle,0,2); FileSeek(iFileHandle,0,0); Buffer := PChar(System.AllocMem(iFileLength + 1)); iBytesRead := SysUtils.FileRead(iFileHandle, Buffer^, iFileLength); FileClose(iFileHandle); for i := 0 to iBytesRead-1 do begin StringGrid1.RowCount := StringGrid1.RowCount + 1; StringGrid1.Cells[1,i+1] := Buffer[i]; StringGrid1.Cells[2,i+1] := IntToStr(Integer(Buffer[i])); end; finally FreeMem(Buffer); end; end; end;
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
|
What do you think about this topic? Send feedback!
|