On Mon, 29 Jul 1996, Charles Canning wrote:
> Hi,
>
> I was just wondering why this is considered safer? I see no difference in
> the two. Both will crash if i = NULL. So what's up? Or am I completely
> off my rocker this evening.
>
> Later...
> ...Chuck
>
Its how for() works:
for( x; y; z) { sentence }
Step 1: Init in _x_
Step 2: if _y_ is true preform _sentence_ else stop
Step 3: do _z_
Step 4: Go to Step 2
Lets look at Step 3: i = i->next_in_room
_sentence_ is preformed before _z_ is done, so if i->next_in_room is
modified inside _sentence_ then you will never get to the next in room,
which can end in a crash or just go on without touching anyone else in
the room. [which was the idea since its an area affected spell]
By doing a 'i = next_i' in _z_ and first in _sentence_ init 'next_i' with
'i->next_in_room' [next_i = i->next_in_room;] then you make sure
'i->next_in_room' are being kept and transfered to the _z_ part in for()
..
It is slower, but been a common error since the DIKU base code long time
ago.. So if you know i->next_in_room got a chance to be modified, then
do it the 'i = next_i' way..
> On Sun, 28 Jul 1996, Niese-Petersen wrote:
>
> > On Sun, 28 Jul 1996 goamkows@kirk.geog.scarolina.edu wrote:
> >
> > This is not an answer to the question, but a warning for a common
> > bug....
> >
> > > for (i = world[ch->in_room].people; i; i = i->next_in_room)
> > ^^^^^^^^^^^^^^^
> > |
> > This is dangerous. Special since 'i' has a chance for dying.
> > If 'i' die, then 'i->next_in_room' is NULL or something weird, since
> > 'i' is being extracted..
> >
> > what to do instead:
> >
> > Add a struct char_data *next_i;
> >
> > for (i = world[ch->in_room; i; i = next_i) {
> > next_i = i->next_in_room;
> > ...etc...
> >
---
Erik Niese-Petersen
Aka Quint The typo God
Realms of Darkness IMP [matrix.xiii.com 6666. Playertesting]
+-----------------------------------------------------------+
| Ensure that you have read the CircleMUD Mailing List FAQ: |
| http://cspo.queensu.ca/~fletcher/Circle/list_faq.html |
+-----------------------------------------------------------+
This archive was generated by hypermail 2b30 : 12/07/00 PST