Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Delphi 10, Record


This question is answered.


Permlink Replies: 8 - Last Post: May 9, 2017 9:19 AM Last Post By: Rudy Velthuis (...
Jim Sawyer

Posts: 214
Registered: 1/3/10
Delphi 10, Record  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 7, 2017 9:14 PM
I haven't used records before in delphi, but I need to do so now.
The record is

  TStuff = Record
    Related: Integer;
    Date: TDateTime;
    Charge: Double;
    Percent: Double;
    Discount: Double;
  end;
....
var
  Stuff: Array of TStuff;


I have up to maybe a hundred or so elements in the array.

My question is - after I finish using the array of records, do
I need to free the memory they use, and if so, what is the
code for doing so?

Thanks,
Jim Sawyer
Paulo França La...

Posts: 16
Registered: 1/26/17
Re: Delphi 10, Record  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 7, 2017 10:54 PM   in response to: Jim Sawyer in response to: Jim Sawyer
Your (dynamic) array will be automatically freed as soon as it goes out of scope.
However, in case you prefer to do it explicitly, just use this:
  SetLength (Stuff, 0);
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Delphi 10, Record [Edit]
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 8, 2017 1:20 PM   in response to: Paulo França La... in response to: Paulo França La...
Paulo wrote:

Your (dynamic) array will be automatically freed as soon
as it goes out of scope.

More accurately, dynamic arrays are reference counted, so the array will
be freed when its reference count falls to zero. When the Stuff variable
goes out of scope, the reference count gets decremented.

However, in case you prefer to do it explicitly, just use this:

SetLength (Stuff, 0);

Or simpler:

Stuff := nil;


--
Remy Lebeau (TeamB)
Paulo França La...

Posts: 16
Registered: 1/26/17
Re: Delphi 10, Record [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 8, 2017 3:44 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Or simpler:
Stuff := nil;
Good to know. Simple and clean.
Jim Sawyer

Posts: 214
Registered: 1/3/10
Re: Delphi 10, Record [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 8, 2017 4:35 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thank you.

Remy Lebeau (TeamB) wrote:
Paulo wrote:

Your (dynamic) array will be automatically freed as soon
as it goes out of scope.

More accurately, dynamic arrays are reference counted, so the array will
be freed when its reference count falls to zero. When the Stuff variable
goes out of scope, the reference count gets decremented.

However, in case you prefer to do it explicitly, just use this:

SetLength (Stuff, 0);

Or simpler:

Stuff := nil;


--
Remy Lebeau (TeamB)
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: Delphi 10, Record [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 9, 2017 9:04 AM   in response to: Paulo França La... in response to: Paulo França La...
Paulo França Lacerda wrote:

Your (dynamic) array will be automatically freed as soon as it goes
out of scope. However, in case you prefer to do it explicitly, just
use this:
  SetLength (Stuff, 0);

Or simply:

Stuff := nil;


--
Rudy Velthuis http://www.rvelthuis.de

"Wisdom is knowing what to do next. Virtue is doing it."
-- David Starr Jordan
Robert Triest

Posts: 687
Registered: 3/24/05
Re: Delphi 10, Record  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 8, 2017 5:23 AM   in response to: Jim Sawyer in response to: Jim Sawyer
but I need to do so now.

why? It's about storing "stuff" in a list (or array) so, why you need records and not just use objects in a list or dictionary?
Like, if you want to search in your array for a record?! Maybe you want to use list functions like "find" or "indexof"..

http://docwiki.embarcadero.com/CodeExamples/Tokyo/en/Generics_Collections_TDictionary_(Delphi)

Edited by: Robert Triest on May 8, 2017 2:24 PM
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: Delphi 10, Record [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 9, 2017 9:10 AM   in response to: Robert Triest in response to: Robert Triest
Robert Triest wrote:

but I need to do so now.

why? It's about storing "stuff" in a list (or array) so, why you need
records and not just use objects in a list or dictionary?

Why not records, which can be used as value types (are not allocated on
the heap, do not need to be freed) as well as reference types (using
New and Dispose)?

If possible, I prefer lightweight records over full blown objects.
Assigning a record means you get a full copy, with updated refcounts on
the contents, if necessary. No ownership problems, no lifetime
problems, and most definitely a unique copy each time. That solves many
problems people have with objects and classes.

Oh right, you can't inherit from them. Well, if things are not part of
a class hierarchy, inheritance is overrated.
--
Rudy Velthuis http://www.rvelthuis.de

"Everything secret degenerates, even the administration of
justice." -- Lord Acton
Rudy Velthuis (...


Posts: 7,731
Registered: 9/22/99
Re: Delphi 10, Record [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 9, 2017 9:19 AM   in response to: Robert Triest in response to: Robert Triest
Robert Triest wrote:

but I need to do so now.

why? It's about storing "stuff" in a list (or array) so, why you need
records and not just use objects in a list or dictionary? Like, if
you want to search in your array for a record?! Maybe you want to use
list functions like "find" or "indexof"..

http://docwiki.embarcadero.com/CodeExamples/Tokyo/en/Generics_Collections_TDictionary_(Delphi)

You can search a TDictionary<somekeytype, MyRecord> or TList<MyRecord>
too. Why use objects (which must be allocated and freed again) if you
can use records? The fact that you can do:

var
MyRec: MyRecord;
MyList: TList<MyRecord>;
...
...
while ... do
begin
MyRec.A := SomeFunc(...);
etc..

MyList.Add(MyRec);
end;

The list will contain unique copies of the record. If you'd have used
an object:

var
MyObj: MyObject;
MyList: TObjectList<MyObj>;
...
...
while ... do
begin
MyObj.MyProperty := SomeFunc(...);
etc..
MyList.Add(MyObj)
end;

Then the list would contain multiple references to that one single
MyObj, and it would have the properties set in the last iteration of
the loop. Not what most people expect.

And when and by whom should MyObj be freed? If the list contains
multiple references to the same object, chances are it will try to free
the same object multiple times.

So I disagree with you: records are a **much** better choice for this.

--
Rudy Velthuis http://www.rvelthuis.de

"You can't have great software without a great team, and most
software teams behave like dysfunctional families." -- Jim McCarthy

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02