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;