Delphi Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreDelphi Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old April 29th, 2008, 12:49 AM
invarbrass invarbrass is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 6 invarbrass User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 43 m 22 sec
Reputation Power: 0
How to selectively delete from TList

I have a snippet of code that iterates through a TList and deletes some items. The code is as follows:

Code:
procedure PurgeList;
var
  P: PSomeRecord;
  I: Integer;
begin
  I := 0;
  while (I < FList.Count) do  
  begin
    P := PSomeRecord(FList[I]);
    if P^.CanDelete then
    begin
      FreeMem(P);
      P := nil;
      FList.Delete(I);
      Continue;
    end;
    Inc(I);
  end;
end;


Is this the right way to delete items from a tlist? or will it enter into an infinite loop?
can anyone show me any other way to remove items from a tlist?

Reply With Quote
  #2  
Old April 29th, 2008, 01:02 AM
clivew clivew is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 713 clivew User rank is Sergeant (500 - 2000 Reputation Level)clivew User rank is Sergeant (500 - 2000 Reputation Level)clivew User rank is Sergeant (500 - 2000 Reputation Level)clivew User rank is Sergeant (500 - 2000 Reputation Level)clivew User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 17 h 48 m 33 sec
Reputation Power: 12
Your code could land you in trouble.

This is better:
When deleting from a list count down from the top.
That way the items you have not yet visited maintain their index place in the loop.
Pseudo Code:
Code:
for i := FList.count-1 down to 0 do begin
  // if FList[i] should be deleted delete it
end;


Clive

Last edited by clivew : April 29th, 2008 at 01:04 AM. Reason: Made code a little clearer

Reply With Quote
  #3  
Old April 29th, 2008, 11:19 AM
invarbrass invarbrass is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 6 invarbrass User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 43 m 22 sec
Reputation Power: 0
thanks for the tip. i've updated my code accordingly.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreDelphi Programming > How to selectively delete from TList


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway