Board index » delphi » TList, Remove & Holes

TList, Remove & Holes

Hi all,

Quick question: Is it possible for TList.Remove to leave "holes" in
the TList? I ask this because I do

FList.Remove (aPtr);
for a := 0 to FList.Count-1 do
  ptr := Flist[a] ;
  // some stuff involving ptr ...

This sometimes gives me an invalid pointer op, and has confused the
hell out of me as the doc's say that TList.Remove will suffle all the
stuff above the removed element up, thus leaving no holes.

Any thoughts?

Tim.

 

Re:TList, Remove & Holes


Quote
"Tim Shields" <t...@bcd-modelling.com> wrote in message

news:98b90000.0302030509.6caaef42@posting.google.com...

Quote
> Hi all,

> Quick question: Is it possible for TList.Remove to leave "holes" in
> the TList? I ask this because I do

> FList.Remove (aPtr);
> for a := 0 to FList.Count-1 do
>   ptr := Flist[a] ;
>   // some stuff involving ptr ...

> This sometimes gives me an invalid pointer op, and has confused the
> hell out of me as the doc's say that TList.Remove will suffle all the
> stuff above the removed element up, thus leaving no holes.

> Any thoughts?

iterate backwards:
for a := FList.Count-1 downto 0 do

Re:TList, Remove & Holes


Quote
"Smola" <supersmola...@VEinet.hr> wrote in message <news:b1lvhs$kbb$1@garrison.globalnet.hr>...
> "Tim Shields" <t...@bcd-modelling.com> wrote in message
> news:98b90000.0302030509.6caaef42@posting.google.com...
> > Hi all,

> > Quick question: Is it possible for TList.Remove to leave "holes" in
> > the TList? I ask this because I do

> > FList.Remove (aPtr);
> > for a := 0 to FList.Count-1 do
> >   ptr := Flist[a] ;
> >   // some stuff involving ptr ...

> > This sometimes gives me an invalid pointer op, and has confused the
> > hell out of me as the doc's say that TList.Remove will suffle all the
> > stuff above the removed element up, thus leaving no holes.

> > Any thoughts?

> iterate backwards:
> for a := FList.Count-1 downto 0 do

Ermm, why?

I'll still give it a go though :-)

Cheers,

Tim

Re:TList, Remove & Holes


Quote
Tim Shields wrote in message

<98b90000.0302031133.11a28...@posting.google.com>...
Quote
>"Smola" <supersmola...@VEinet.hr> wrote in message

<news:b1lvhs$kbb$1@garrison.globalnet.hr>...

Quote
>> "Tim Shields" <t...@bcd-modelling.com> wrote in message
>> news:98b90000.0302030509.6caaef42@posting.google.com...
>> > Hi all,

>> > Quick question: Is it possible for TList.Remove to leave "holes" in
>> > the TList? I ask this because I do

>> > FList.Remove (aPtr);
>> > for a := 0 to FList.Count-1 do
>> >   ptr := Flist[a] ;
>> >   // some stuff involving ptr ...

>> > This sometimes gives me an invalid pointer op, and has confused the
>> > hell out of me as the doc's say that TList.Remove will suffle all the
>> > stuff above the removed element up, thus leaving no holes.

>> > Any thoughts?

>> iterate backwards:
>> for a := FList.Count-1 downto 0 do

>Ermm, why?

Because some stuff involving ptr may remove it from the list,
and FList.Count-1 may have been calculated just once before
starting the loop.

Many things run through one another here. Somewhere in the
help, it says that removing a pointer from a TList can/should
be done by putting a nil pointer in its place. This avoids the
abovementioned problem, but requires extra logic to react to
nil pointers correctly. It also assumes that any nil pointers
in the list are invalid. There are cases where they simply mean
something else.

Index:=0;
while (Assigned(FList) and (Index<FList.Count))
do begin
  if (Test(FList.Items[Index]))
  then begin
    Process(FList.Items[Index]);
    Inc(Index);
  end
  else FList.Delete(Index);
end;

That's what I mostly do; usually after I've been reminded by an
exception that a for loop wasn't appropriate.

Groetjes,
Maarten Wiltink

Re:TList, Remove & Holes


In article <3e3ed74d$0$49099$e4fe5...@news.xs4all.nl>, "Maarten Wiltink"
<maar...@kittensandcats.net> replying to Tim Shields writes:

Quote
>Because some stuff involving ptr may remove it from the list,
>and FList.Count-1 may have been calculated just once before
>starting the loop.

"for" loop limits _are_ calculated only once at loop start. Use "while" loops
if you must have the limit calculated as you go.

Quote
>Many things run through one another here. Somewhere in the
>help, it says that removing a pointer from a TList can/should
>be done by putting a nil pointer in its place.

"can" not "should", and it appears in TList.Delete, an alias for TListRemove.

Tim - if you think about a fixed loop limit of Count - 1, when the code has
removed half the items, the number of items has _reduced_ to half the original
Count. The next loop will have an invalid index because the index is greater
than the actual number of items in the TList.

Alan Lloyd
alangll...@aol.com

Re:TList, Remove & Holes


"Smola" <supersmola...@VEinet.hr> skrev i melding
news:b1lvhs$kbb$1@garrison.globalnet.hr...

Quote

> "Tim Shields" <t...@bcd-modelling.com> wrote in message
> news:98b90000.0302030509.6caaef42@posting.google.com...
> > Hi all,

> > Quick question: Is it possible for TList.Remove to leave "holes" in
> > the TList? I ask this because I do

> > FList.Remove (aPtr);
> > for a := 0 to FList.Count-1 do
> >   ptr := Flist[a] ;
> >   // some stuff involving ptr ...

> > This sometimes gives me an invalid pointer op, and has confused the
> > hell out of me as the doc's say that TList.Remove will suffle all the
> > stuff above the removed element up, thus leaving no holes.

> > Any thoughts?

> iterate backwards:
> for a := FList.Count-1 downto 0 do

I believe this is the best solution (if direction doesn't affect logics),
because:
- it's the simplest solution
- (more important:) When you get used to this (and end up writing maybe
every 20th loop as a backwards one), it is a chance this solution pops up in
your mind when you're dealing with a problem. In some cases it makes a
complex problem easier, because you may actually add or delete what you want
with no extra handling.
- (Conclusion:) Use 'downto' loops when they are appropriate, to keep the
"reverse logic" active in your mind.

--
Bj?rge S?ther
bjorge@hahaha_itte.no

Re:TList, Remove & Holes


Quote

> Tim - if you think about a fixed loop limit of Count - 1, when the code has
> removed half the items, the number of items has _reduced_ to half the original
> Count. The next loop will have an invalid index because the index is greater
> than the actual number of items in the TList.

> Alan Lloyd
> alangll...@aol.com

I understand about using downto when removing items ... learned it the
hard way :-)

However, I am removing some items in a different procedure. The
procedure that appears to be giving me the trouble is simply going
through the list comparing an element in the TMyStruct with a TagID
value. This is why I am confused.

Of course, I've been getting some really odd error messages with my
app (am currently trying to get another so I can post) and these may
be leading me up a blind ally :-(

Cheers for your help & suggestions.

Tim.

Other Threads