德尔福记录 [英] Records in Delphi
问题描述
- 由于记录几乎像类,为什么不使用类而不是记录? li>
- 从理论上讲,当记录由变量声明时被分配给记录;但是,如何释放内存?
- 我可以理解指针到记录列表对象中的效用,但是可以使用泛型容器(
TList
),有没有需要使用指针呢?如果没有,如何删除/释放每个记录到通用容器?如果我想删除一个特定的记录到一个通用容器,怎么办?
对于1和2:记录是值类型,而类是引用类型。它们分配在堆栈上,或者直接在包含它们的任何较大变量的内存空间中,而不是通过指针分配,并在编译器超出范围时自动清除。
对于您的第三个问题, TList< TMyRecord>
内部声明一个数组TMyRecord
用于存储空间。列表中的所有记录将被清除。如果要删除特定的一个,请使用删除
方法按索引删除,或使用删除
方法来查找并删除。但是请注意,由于它是一种价值类型,所做的一切都将制作记录副本,而不是复制其引用。
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 an array of TMyRecord
for 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 the Delete
method to delete by index, or the Remove
method 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.
这篇关于德尔福记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!