Server Help

ASSS Questions - Remove from LinkedList

Samapico - Wed Nov 03, 2010 8:58 pm
Post subject: Remove from LinkedList
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.
Arnk Kilo Dylie - Wed Nov 03, 2010 9:25 pm
Post subject:
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.
Samapico - Thu Nov 04, 2010 12:34 am
Post subject:
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
Cheese - Sun Jan 09, 2011 2:16 am
Post subject:
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
Dr Brain - Sun Jan 09, 2011 8:39 am
Post subject:
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?
Cheese - Sun Jan 09, 2011 3:51 pm
Post subject:
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:
Dr Brain - Sun Jan 09, 2011 7:15 pm
Post subject:
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.
Samapico - Mon Jan 10, 2011 2:05 pm
Post subject:
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...
Cheese - Mon Jan 17, 2011 3:45 am
Post subject:
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);
   }




All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group