[comp.lang.c++] Possible error in Stroustrup ?

anthony@batserver.cs.uq.oz (Anthony Lee) (06/01/89)

On page 205 of "The C++ programming language" by Stroustrup, there is 
listing of a get function for circular list class slist.

ent slist::get()
{
   if (last == 0) slist_handler("get from empty slist");
   slink* f = last->next;
   ent r = f->e;
   last = (f==last) ? 0 : f->next;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   delete f;
   return r;
}

The line starting with "last = ...." seems to be wrong.  The function
get is suppose to return and remove the head of the list.
Can anyone confirm this ?
Anthony Lee (Humble PhD student) (alias Doctor(Time Lord))
ACSnet:	anthony@batserver.cs.uq.oz	TEL: (07) 3712651
					     (07) 3774139 (w)
SNAIL: 243 Carmody Rd, St Lucia, 4067 Australia

diamond@diamond.csl.sony.junet (Norman Diamond) (06/05/89)

In article <774@batserver.cs.uq.oz> anthony@batserver.cs.uq.oz writes:

>On page 205 of "The C++ programming language" by Stroustrup, there is 
>listing of a get function for circular list class slist.

>ent slist::get()
>{
>   if (last == 0) slist_handler("get from empty slist");
>   slink* f = last->next;
>   ent r = f->e;
>   last = (f==last) ? 0 : f->next;
>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>   delete f;
>   return r;
>}

>The line starting with "last = ...." seems to be wrong.  The function
>get is suppose to return and remove the head of the list.
>Can anyone confirm this ?

Looks to me like it was a bug, and it looks to me like it looked to
Bjarne Stroustrup like it was a bug, too.  In my copy of The Book, it
now looks like:

if (f == last)
    last = 0;
else
    last->next = f->next;

I guess it is no longer nececessary to get the wrong answer in as few
lines as possible.

Once upon a time, someone suggested testing examples before publishing
them.  Does no one learn?

--
Norman Diamond, Sony Computer Science Lab (diamond%csl.sony.co.jp@relay.cs.net)
  The above opinions are my own.   |  Why are programmers criticized for
  If they're also your opinions,   |  re-implementing the wheel, when car
  you're infringing my copyright.  |  manufacturers are praised for it?

scott@apple.apple.com (scott douglass) (06/07/89)

In article <10330@socslgw.csl.sony.JUNET> diamond@diamond.csl.sony.junet 
(Norman Diamond) writes:
> In article <774@batserver.cs.uq.oz> anthony@batserver.cs.uq.oz writes:
> 
> >On page 205 of "The C++ programming language" by Stroustrup, there is 
> >listing of a get function for circular list class slist.
> > ...
> >The line starting with "last = ...." seems to be wrong.
> >... Can anyone confirm this ?
> 
> Looks to me like it was a bug, and it looks to me like it looked to
> Bjarne Stroustrup like it was a bug, too. 

On the same page is a bug the definition of slist::clear().  (My copy of 
The Book is "Reprinted with corrections July, 1987").  The function 
operator deletes all of the elements in the list but fails to clear the 
pointer to the last element (thus leaving the deleted elements in the 
list).  Just add the line
    last = 0;
as the last statement in slist::clear().