Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
Remove from LinkedList

 
Post new topic   Reply to topic Printable version
 View previous topic  makefiles Post :: Post LVZ file location  View next topic  
Author Message
Samapico
No, these DO NOT look like penises, ok?


Joined: May 08 2003
Posts: 1252
Offline

PostPosted: Wed Nov 03, 2010 8:58 pm    Post subject: Remove from LinkedList Reply to topic Reply with quote

Is this safe?

Code: Show/Hide
      FOR_EACH(&indicators, ind, link)
         if (ind->t.type == T_PLAYER && ind->t.u.p == p)
         {
            if (ind->state)
               afree(ind->state);
            ind->state = NULL;
            ind->statelen = 0;

            LLRemove(&indicators, ind);
         }

Is it safe to remove an element from a list while iterating through it?

The FOR_EACH macro seems to assign the Link->Next element at the beginning of each loop, so it seems safe... but I'm not quite sure, and I'm having some bad crashes, so I don't know if it's related to that.
_________________
(Insert a bunch of dead links here)
Back to top
View users profile Send private message Add User to Ignore List
Arnk Kilo Dylie
Seasoned Helper


Age:36
Gender:Gender:Male
Joined: Jul 14 2006
Posts: 108
Offline

PostPosted: Wed Nov 03, 2010 9:25 pm    Post subject: Reply to topic Reply with quote

FOR_EACH is designed to be safe for removing the element you're working with. That's why it's not a simple link=head;link;link=link->next.

Make sure you're locking properly with all linked lists or other complicated structures.
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Samapico
No, these DO NOT look like penises, ok?


Joined: May 08 2003
Posts: 1252
Offline

PostPosted: Thu Nov 04, 2010 12:34 am    Post subject: Reply to topic Reply with quote

Alright, thanks...

I fixed my crash now... not sure exactly what it was, but I fixed a lot of possible usage of obsolete pointers and such
Back to top
View users profile Send private message Add User to Ignore List
Cheese
Wow Cheese is so helpful!


Joined: Mar 18 2007
Posts: 1017
Offline

PostPosted: Sun Jan 09, 2011 2:16 am    Post subject: Reply to topic Reply with quote

this might be late, but i also ran into this awhile ago

personally i find its easier to delete the link im working with instead of jumping through extra hoops not to, but for a long time i was using the following:
Code: Show/Hide
#define FOR_EACH_LINK(x) Link *l; for(l=LLGetHead(&(x)); l; l=l->next)

then things started crashing, and it was a few hours before i made the following:
Code: Show/Hide
#define SAFE_FOR_EACH_LINK(x,y,z) Link *(y), *(z); for((y)=LLGetHead(&(x)); (y); (y)=(z))
#define SAFE_LINK_DATA(y,z) (y)->data; (z)=(y)->next

and ive been using that since, and it works as long as you get the data before you kill the link

like so:
Code: Show/Hide
   SAFE_FOR_EACH_LINK(list,l,ll)
   {
      Data *d=SAFE_LINK_DATA(l,ll);
      if(d->p == p) LLRemove(&list,d);
   }


i like it :P
_________________
SSC Distension Owner
SSCU Trench Wars Developer
Back to top
View users profile Send private message Add User to Ignore List Visit posters website AIM Address
Dr Brain
Flip-flopping like a wind surfer


Age:38
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Sun Jan 09, 2011 8:39 am    Post subject: Reply to topic Reply with quote

Cheese, FOR_EACH does that in one line, uses one fewer variable, and doesn't require you to change the compiler to C99.

Perhaps you misunderstood Arnk's reply to mean the exact opposite of what he said?
_________________
Hyperspace Owner

Smong> so long as 99% deaths feel lame it will always be hyperspace to me
Back to top
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
Cheese
Wow Cheese is so helpful!


Joined: Mar 18 2007
Posts: 1017
Offline

PostPosted: Sun Jan 09, 2011 3:51 pm    Post subject: Reply to topic Reply with quote

isnt it already in c99?

if not why cant i do
for(int i=0; i<10; i++)?



also, i never looked at the existing macros too hard because i was always just using
Link *l; for(l=LLGetHead(&list); l; l=l->next)
all the time, and then wrote my own when that stopped working D:
Back to top
View users profile Send private message Add User to Ignore List Visit posters website AIM Address
Dr Brain
Flip-flopping like a wind surfer


Age:38
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Sun Jan 09, 2011 7:15 pm    Post subject: Reply to topic Reply with quote

Cheese wrote:
isnt it already in c99?


Not on every platform.

Cheese wrote:
if not why cant i do
for(int i=0; i<10; i++)?


Because it's not C99 on your platform.

Cheese wrote:
also, i never looked at the existing macros too hard because i was always just using
Link *l; for(l=LLGetHead(&list); l; l=l->next)
all the time, and then wrote my own when that stopped working D:


No one expects you to look at code that isn't useful to you, but we all appreciate it when you read a two month old thread before bumping.
Back to top
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
Samapico
No, these DO NOT look like penises, ok?


Joined: May 08 2003
Posts: 1252
Offline

PostPosted: Mon Jan 10, 2011 2:05 pm    Post subject: Reply to topic Reply with quote

Still in the field of linked lists...

On Windows, my server was crashing once in a while, and I never knew why. On Linux it crashed much more frequently, and the backtrace allowed me to find the source. Just to be sure...

If I declare a linked list as a global variable in a module (or an array of linked lists), I need to call:

LLInit(&mylist); somewhere in the module load or attach,

and
LLEmpty(&mylist); somewhere in the unload or detach?

I was using LLFree on unload... upon further reading the comments I saw it was pretty bad tongue.gif It was trying to free the variable itself...
Back to top
View users profile Send private message Add User to Ignore List
Cheese
Wow Cheese is so helpful!


Joined: Mar 18 2007
Posts: 1017
Offline

PostPosted: Mon Jan 17, 2011 3:45 am    Post subject: Reply to topic Reply with quote

now that i looked again, the SAFE_FOR_EACH_LINK was a bad way to do things tongue.gif


ive decided to stick with

Code: Show/Hide


#define FOR_EACH_LINK(x,y,z) Link *(z); for((z)=LLGetHead(&(x)); (z) && (((y)=(z)->data, (z)=(z)->next) || 1); )

//like so:
   LinkedList list;
   Chopsticks *cs;
   FOR_EACH_DATA(list,cs,l)
   {
      printf("number: %i",cs->count);
   }



Back to top
View users profile Send private message Add User to Ignore List Visit posters website AIM Address
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> ASSS Questions All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 646 page(s) served in previous 5 minutes.

phpBB Created this page in 0.836597 seconds : 34 queries executed (94.2%): GZIP compression disabled