System. reallocmem-apply for memory again

Source: Internet
Author: User
This example is based on the getmem example:

Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs; Type tform1 = Class (tform) Procedure formcreate (Sender: tobject); end; vaR form1: tform1; implementation {$ R *. DFM} type tmyrec = record {definition structure; remember that the size of this structure is 12 bytes} Name: String [8]; age: word; {although word is 2 bytes; however, because it is 4-byte aligned, it occupies 4 bytes} end; pmyrec = ^ tmyrec; {definition structure pointer} var PR: pmyrec; Procedure tform1.fo Rmcreate (Sender: tobject); const STR = 'address: % d; Name: % s'; begin {apply for memory of 3 tmyrec structures} // getmem (PR, sizeof (tmyrec) * 3); reallocmem (PR, sizeof (tmyrec) * 3); {This sentence can also be replaced by a previous line} {value} PR. name: = 'zhang san'; PR. age: = 11; Inc (PR); PR. name: = 'Li si'; PR. age: = 22; Inc (PR); PR. name: = 'wang 5'; PR. age: = 33; {display the addresses and information of the three structures; the addresses should be consecutive (12 bytes)} Dec (PR, 2); showmessage (format (STR, [INTEGER (PR), Pr. name]); {address: 15278504; Name: James} Inc (PR); showmessage (format (STR, [INTEGER (PR), Pr. name]); {address: 15278516; Name: Li Si} Inc (PR); showmessage (format (STR, [INTEGER (PR), Pr. name]); {address: 15278528; Name: Wang Wu} {re-apply for memory, requires five structure sizes, and assigns two new structures} Dec (PR, 2); reallocmem (PR, sizeof (tmyrec) * 5); Inc (PR, 3); PR. name: = 'ma 6'; PR. age: = 44; Inc (PR); PR. name: = 'Sun 7'; PR. age: = 55; {displays the relevant information. Although the address is continuous, it is already different from the above !} Dec (PR, 4); showmessage (format (STR, [INTEGER (PR), Pr. name]); {address: 14875920; Name: Zhang San} Inc (PR); showmessage (format (STR, [INTEGER (PR), Pr. name]); {address: 14875932; Name: Li Si} Inc (PR); showmessage (format (STR, [INTEGER (PR), Pr. name]); {address: 14875944; Name: Wang Wu} Inc (PR); showmessage (format (STR, [INTEGER (PR), Pr. name]); {address: 14875956; Name: Ma 6} Inc (PR); showmessage (format (STR, [INTEGER (PR), Pr. name]); {address: 14875968; Name: Sun Qi} Dec (PR, 4); freemem (PR, sizeof (tmyrec) * 5 ); {You can also use freemem to clear the memory applied by reallocmem} end; end.
 
   
 

Public Function directory under System Unit

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.