An example of using a private heap:
Unit Unit1;InterfaceUses Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls;Type TForm1 =Class (Tform) Button1:tbutton;Procedure Button1Click (Sender:tobject);Endvar Form1:tform1;Implementation{$R *.DFM}var Myheap:thandle;{heap handle} P:pointer;Procedure TForm1. Button1Click (Sender:tobject);var I,num:integer; P2:pointer; Str:StringBegin{Build Heap} Myheap: = HeapCreate (Heap_zero_memory,1024*1024*2,0);{Build a heap of 2M}If Myheap =0Then Exit;{Quits if creation fails}{Memory allocated from heap} p: = HeapAlloc (Myheap,0,7);If p =NilThen Exit;{Error Exiting}{Get memory block size} num: = HeapSize (Myheap,0, p);{Assign a value to each byte of the memory block} p2: = P;For I: =0To Num-1DoBegin Byte (p2^): = i +65; P2: = Ptr (Integer (p2) +1);End{Fetch value} p2: = P; STR: =‘‘;For I: =0To Num-1DoBegin str: = str + CHR (Byte (p2^)); P2: = Ptr (Integer (p2) +1);End{Show the contents and size of memory blocks} SHOWMESSAGEFMT ('%s,%d ', [Str,num]);{abcdefg,7} {Expand Memory, this is only a different sentence, the following are repeating the above code} p: = HeapReAlloc (Myheap,0, p,26);If p =NilThen Exit;{Error Exiting}{Get memory block size} num: = HeapSize (Myheap,0, p);{Assign a value to each byte of the memory block} p2: = P;for I: = 0 to Num-1 do begin Byte (p2^): = i + 65; P2: = Ptr (Integer (p2) + 1); end; {value} p2: = P; str: = for I: = 0 to Num-1 do begin str: = str + CHR (Byte (p2^)); P2: = Ptr (Integer (p2) + 1); end; {shows the contents and size of the memory block} showmessagefmt ( '%s,%d ', [Str,num]); {abcdefghijklmnopqrstuvwxyz,26} {Free Memory} heapfree (MyHeap, 0, p); {Destroy Heap} HeapDestroy (MYHEAP); end; end.
Delphi7 Deposit (iv)