Copy Dynamic Array

Source: Internet
Author: User
Unit unit1; interfaceuses windows, messages, extensions, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; Procedure formcreate (Sender: tobject); Procedure button1click (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} type Tarr = array of int64; {first define the required dynamic array as a type, because the following type conversion is required} var arr1, arr2: Tarr; {two array variables waiting for test} p: pointer; {This pointer is used to transit data in the array} {assign the test value to the first array} procedure tform1.formcreate (Sender: tobject ); vaR I: integer; begin for I: = 0 to 1000-1 do begin setlength (arr1, length (arr1) + 1); arr1 [high (arr1)]: = I * 2; end; {spot check display} showmessage (inttostr (arr1 [9]); {18} end; {copy to second array} procedure tform1.button1click (Sender: tobject); var size: Cardinal; begin size: = length (arr1) * sizeof (arr1 [0]); {source array size} getmem (p, size ); {Memory Allocation Based on source array size} copymemory (p, arr1, size); {copy} arr2: = Tarr (P ); {conversion} {spot check display} showmessage (inttostr (arr2 [9]); {18} end; end.
 
   
 

If the array element is a structure, test it again:

 unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; Procedure formcreate (Sender: tobject); Procedure button1click (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} type TREC = record STR: string; INT: integer; end; Tarr = array of TREC; var arr1, arr2: Tarr; P: pointer; Procedure tform1.formcreate (Sender: tobject); var I: integer; begin for I: = 0 to 1000-1 do begin setlength (arr1, length (arr1) + 1); arr1 [high (arr1)]. STR: = inttostr (I); arr1 [high (arr1)]. INT: = I * 2; end; showmessagefmt ('% s, % d', [arr1 [9]. STR, arr1 [9]. int]); {9,18} end; Procedure tform1.button1click (Sender: tobject); var size: Cardinal; begin size: = length (arr1) * sizeof (arr1 [0]); getmem (p, size); copymemory (p, arr1, size); arr2: = Tarr (p); showmessagefmt ('% s, % d', [arr2 [9]. STR, arr2 [9]. int]); {9, 18} end; end.

   

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.