Two examples of dynamic array pointer operations

Source: Internet
Author: User
Recently, I suddenly felt that Delphi's dynamic array is very useful. Using It can omit a lot of getmem and reallocmem, And it is automatically maintained.

The dynamic array variable arr itself is a pointer and cannot be obtained with @ arr;

However, to obtain a pointer to an element position, it should be @ arr [X]. But with the redistribution of the array, this address changes.

// Dynamic array address test var I: integer; arr: tbytes; P: pbyte; begin setlength (ARR, 5); for I: = 0 to 4 Do arr [I]: = I + 65; showmessage (stringof (ARR); {ABCDE} p: = pbyte (ARR); showmessage (CHR (P ^); {A} p: = pbyte (@ arr [2]); showmessage (CHR (P ^); {c} P ^: = ord ('-'); showmessage (stringof (ARR); {AB-de} end; // combines two dynamic arrays: var arr1, arr2: tbytes; Len: integer; begin setlength (arr1, 3); setlength (arr2, 3); arr1 [0]: = 65; arr1 [1]: = 66; arr1 [2]: = 67; arr2 [0]: = 68; arr2 [1]: = 69; arr2 [2]: = 70; Len: = length (arr1); setlength (arr1, Len + Length (arr2 )); copymemory (@ arr1 [Len], arr2, length (arr2); showmessage (stringof (arr1); {abcdef} end;

 
Related Article

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.