some questions about records in Delphi:
- As records are almost like classes, why not use only classes instead of records?
- In theory, memory is allocated for a record when it is declared by a variable; but, and how is memory released after?
- I can understand the utility of pointers to records into a list object, but with Generics Containers (
TList<T>), are there need to use pointer yet? if not, how to delete/release each record into a Generic Container? If I wanna delete a specific record into a Generic Container, how to do it?
For 1 and 2: records are value types, while classes are reference types. They’re allocated on the stack, or directly in the memory space of any larger variable that contains them, instead of through a pointer, and automatically cleaned up by the compiler when they go out of scope.
As for your third question, a
TList<TMyRecord>internally declares anarray of TMyRecordfor storage space. All the records in it will be cleaned up when the list is destroyed. If you want to delete a specific one, use theDeletemethod to delete by index, or theRemovemethod to find and delete. But be aware that since it’s a value type, everything you do will be making copies of the record, not copying references to it.