Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-11

Objects in dynamic arrays

8 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Timothy Groves

How exactly are instantiated classes treated in an array? Can I copy one into another? And if I unallocate an array member, does it automatically Destroy the object?

I have objects stored in a dynamic array, and I want to delete an arbitrary record. What I have right now is:

var
   index,
   last : integer;
begin
   last := length (t_volumes) - 1;
   index := 0;
   while ((t_volumes [index]  t_current_volume) and (index < last)) do
       inc (index);
   if (t_current_volume = t_volumes [index]) then
     t_volumes [index].Destroy;
   SetLength (t_volumes, last);
end;

This produces bad results, as it currently sits. What am I doing wrong?

Timothy Groves
to Timothy Groves
Timothy Groves wrote 10 lines
How exactly are instantiated classes treated in an array? Can I copy one into another? And if I unallocate an array member, does it automatically Destroy the object? I have objects stored in a dynamic array, and I want to delete an arbitrary record. What I have right now is: <snip incorrectly pasted code> This produces bad results, as it currently sits. What am I doing wrong?

Here's the code I *actually* have in the method:

var
   index,
   last : integer;
begin
   last := length (t_volumes) - 1;
   index := 0;
   while ((t_volumes [index]  t_current_volume) and (index < last)) do
       inc (index);
   if (index < last) then begin
     t_volumes [index] := t_volumes [last];
   t_volumes [last].Destroy;
   end;
   SetLength (t_volumes, last);
end;
Martin
to Timothy Groves
Timothy Groves wrote 13 lines
Here's the code I *actually* have in the method: var index, last : integer; begin last := length (t_volumes) - 1; index := 0; while ((t_volumes [index] t_current_volume) and (index < last)) do inc (index); if (index < last) then begin t_volumes [index] := t_volumes [last];

Objects are actually stored via a reference (internal pointer) So t_volumes [last] just points to the objects data

The above statement duplicates the reference

t_volumes [last].Destroy;

destroys the object pointed to by both: t_volumes [last] and t_volumes [index]

You should have destroyed t_volumes [index] *before* copying the value

Sven Barth
to Timothy Groves: Here's the code I *actually* have in the method: var last : integer; begin last := length…
Timothy Groves wrote 18 lines
> This produces bad results, as it currently sits. What am I doing wrong? Here's the code I *actually* have in the method: var index, last : integer; begin last := length (t_volumes) - 1; index := 0; while ((t_volumes [index] t_current_volume) and (index < last)) do inc (index); if (index < last) then begin t_volumes [index] := t_volumes [last]; t_volumes [last].Destroy; end; SetLength (t_volumes, last); end;

Martin already said the necessary points, but I'd like to say you a note not related to your problem: if you use "High(t_volumes)" you get the value of the highest index of the array, so you could replace your "last" with that. Just saying...

Regards,
Sven

Andreas Schneider
to Martin: Objects are actually stored via a reference (internal pointer) So t_volumes [last] just…
Martin wrote 25 lines
On 16/11/2013 01:03, Timothy Groves wrote: > > Here's the code I *actually* have in the method: > > var > index, > last : integer; > begin > last := length (t_volumes) - 1; > index := 0; > while ((t_volumes [index] t_current_volume) and (index < last)) do > inc (index); > if (index < last) then begin > t_volumes [index] := t_volumes [last]; Objects are actually stored via a reference (internal pointer) So t_volumes [last] just points to the objects data The above statement duplicates the reference > t_volumes [last].Destroy; destroys the object pointed to by both: t_volumes [last] and t_volumes [index] You should have destroyed t_volumes [index] *before* copying the value

Maybe a bit late, but that might not be true.
If Timothy really talks about _object_ (and not _class(TObject)_) it should work as he does it. Also Destroy would probably be appropriate.

Just saying :-)

The actual type defs used are missing.

Best Regards, Andreas
Sven Barth
to Andreas Schneider
Andreas Schneider wrote
Maybe a bit late, but that might not be true. If Timothy really talks about _object_ (and not _class(TObject)_) it should work as he does it. Also Destroy would probably be appropriate. Just saying :-) The actual type defs used are missing.

Unlikely. Quote of Timothy:

How exactly are instantiated classes treated in an array?

So it's very likely that he is indeed talking about descendants of TObject.

Regards,
Sven

Tomas Hajny
to Sven Barth
Sven Barth wrote 9 lines
Am 28.11.2013 08:33, schrieb Andreas Schneider: > Maybe a bit late, but that might not be true. If Timothy really talks > about _object_ (and not _class(TObject)_) it should work as he does > it. Also Destroy would probably be appropriate. Just saying :-) The > actual type defs used are missing. Unlikely. Quote of Timothy: > How exactly are instantiated classes treated in an array? So it's very likely that he is indeed talking about descendants of TObject.

Agreed.

BTW, note that reference to "TObject" is not the best solution for differentiation between "old" objects and classes, because TObject type exists for both.

Tomas