Understanding memory allocation for TList <RecordType>

I have to keep a TList of what can be easily implemented as a Delphi entry (five simple fields). However, I do not understand what happens when I do TList<TMyRecordType>.Add(R).

Since R is a local variable in the procedure in which I create my TList, I assume that the memory for it will be released when the function returns. Does this leave an invalid entry pointer in the list? Or is the list known for copying? If the first one, I assume that I will have to manually manage the memory for R using New () and Dispose (), is this correct?

Alternatively, I can “push” my record type into a class type by simply declaring open fields (without even bothering with formal properties). Is this considered OK, or should I spend time creating a class with private fields and public properties?

+3
source share
1 answer

Simplified: records are data blocks and are transmitted by value - that is, by copying them - by default. TList<T>stores the values ​​in an array of type T. So, TList<TMyRecordType>.Add(R)copy the value Rto the array at position Countand increase the value Countby one. No need to worry about allocating or freeing memory.

, : , , , , ; CopyRecord System.pas, . , Move .

+10

Source: https://habr.com/ru/post/1781872/


All Articles