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?
How do I make a record in Delphi?
TMember = Record ... For example, the following declaration creates a record type called TMember, the one we could use in our case. Essentially, a record data structure can mix any of Delphi's built-in types including any types you have created. Record types define fixed collections of items of different types.
What is record in Pascal?
A record is a highly structured data type in Pascal. They are widely used in Pascal, to group data items together logically. While simple data structures such as array s or sets consist of elements all of the same type, a record can consist of a number of elements of different types, and can take on a huge complexity.
What is an array in Delphi?
Array := Series of Values Arrays allow us to refer to a series of variables by the same name and to use a number (an index) to call out individual elements in that series. Arrays have both upper and lower bounds and the elements of the array are contiguous within those bounds.
What is Delphi structure?
Instances of a structured type hold more than one value. Structured types include sets, arrays, records, and files as well as class, class-reference, and interface types.
There are lots of differences between records and classes; and no "Pointer to record" <> "Class". Each has its own pros and cons; one of the important things about software development is to understand these so you can more easily choose the most appropriate for a given situation.
- This question is based on a false premise. Records are not almost like classes, in the same way that Integers are not almost like Doubles.
- Classes must always be dynamically instantiated, whereas this is a possibility, but not a requirement for records.
- Instances of classes (which we call objects) are always passed around by reference, meaning that multiple sections of code will share and act on the same instance. This is something important to remember, because you may unintentionally modify an object as a side-effect; although when done intentionally it's a powerful feature. Records on the other hand are passed by value; you need to explicitly indicate if you're passing them by reference.
- Classes do not 'copy as easily as records'. When I say copy, I mean a separate instance duplicating a source. (This should be obvious in light of the value/reference comment above).
- Records tend to work very nicely with typed files (because they're so easy to copy).
- Records can overlay fields with other fields (case x of/unions)
- These were comments on certain situational benefits of records; conversely, there are also situational benefits for classes that I'll not elaborate on.
- Perhaps the easiest way to understand this is to be a little pedantic about it. Let's clarify; memory is not really allocated 'when its declared', it's allocated when the variable is in scope, and deallocated when it goes out of scope. So for a local variable, it's allocated just before the start of the routine, and deallocated just after the end. For a class field, it's allocated when the object is created, and deallocated when it's destroyed.
- Again, there are pros and cons...
- It can be slower and require more memory to copy entire records (as with generics) than to just copy the references.
- Passing records around by reference (using pointers) is a powerful technique whereby you can easily have something else modify your copy of the record. Without this, you'd have to pass your record by value (i.e. copy it) receive the changed record as a result, copy it again to your own structures.
- Are pointers to records like classes? No, not at all. Just two of the differences:
- Classes support polymorphic inheritance.
- Classes can implement interfaces.
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.