Delphi7 Deposit (iv)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.