Re: Infinite loops

From: Gary Barnett (gbarnett@POLARNET.COM)
Date: 08/21/97


On Thursday, August 21, 1997 9:35 AM, George
[SMTP:greerga@DRAGON.HAM.MUOHIO.EDU] wrote:
> I recommend this construct for every for() loop in CircleMUD:
>
>     for (toy = get_buffer_head(); toy; toy = toy->next)
>       if (toy == toy->next) {
>         log("SYSERR: BUF: do_buffer: Infinite loop!");
>         toy->next = NULL;
>       } else if (!toy->used && toy->size == size &&

A tricky thing that is easy to forget:

Consider the following (broken) code:

   for (vict=world[ch->in_room].people;vict;vict=vict->next)
        send_to_char("blah",vict);

Here's a better version:

   for (vict=world[ch->in_room].people;vict;vict=vict->next_in_room)
        send_to_char("blah",vict);

The best (?) version: (slower though)

   struct char_data *vict, *next_vict, *last_vict=NULL;

   for (vict=world[ch->in_room].people;vict;vict=next_vict) {
        next_vict = vict->next_in_room;
        if ( (last_vict == vict) || (next_vict == vict) ) {
           log("SYSERR: Your character list is messed up.. so sad :-)");
           exit(1);
        }
        send_to_char("blah",vict);
        last_vict = vict;
  }

I remember being stumped by the strangest bug in a piece of code I was
writing
some months back. A few hundred lines of code and it's was acting very
strange. Had the above problems with looping, and other random affects.

Turned out I had forgotten to use next_in_room..

Good one to put in the memory banks if you are susceptible to doing stupid
stuff like the above :-)

--Mallory
I can picture in my mind a world without war, a world without hate.
And I can picture us attacking that world, because they'd never
expect it.     - Jack Handey


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/08/00 PST