qt-interest@trolltech.com
[Top] [All Lists]

Re: i must be a valid index position in the list (i.e., 0 <= i < size())

Subject: Re: i must be a valid index position in the list (i.e., 0 <= i < size()) or what?
From: Andreas Pakulat
Date: Mon, 27 Nov 2006 10:03:25 +0100
On 27.11.06 01:33:25, Steven T. Hatton wrote:
> On Sunday 26 November 2006 20:13, Andreas Pakulat wrote:
> > On 26.11.06 18:32:18, Steven T. Hatton wrote:
> > > http://doc.trolltech.com/4.2/qlist.html#at
> > > "const T & QList::at ( int i ) const
> > > Returns the item at index position i in the list.
> > > i must be a valid index position in the list (i.e., 0 <= i < size()).
> > > This function is very fast (constant time).
> > > See also value() and operator[]()."
> > >
> > > Shall I assume it doesn't do what I should assume it does?
> >
> > If you would tell us what you assume, we might be able to help you.
> >
> > > http://www.research.att.com/~bs/3rd_safe.pdf
> >
> > You don't really expect anyone here to read through 34 pages to find
> > out, what you assume do you? Which page shows what you mean?
> >
> > Andreas
> 
> I find this behavior undesierable:
> 
> QStringList sl(QString("a:b:c").split(":"));
> sl.at(sl.size());
> 
> ASSERT failure in QList<T>::at: "index out of range", 
> file /home/hattons/opt/com/trolltech/qt-4-snapshot/include/QtCore/qlist.h, 
> line 370
> Aborted

Well, doc's tell you not to do so, i.e. before you index into a list
check the size against your parameter ;)

> You do that with a production server a few times, and you are out of the 
> server business.  I concede that a greater problem is corrupted data due to 
> something such as a QList::takeLast() throwing an exception and not restoring 
> the initial state.  I believe that is one reason I'm unlikely to see 
> QList::at() throw an exception.  

Exceptions are not used inside Qt itself (except for a handful of
places, where they are transported to the outside) for totally different
reasons. IIRC the signal/slot mechanism is not exception-safe, but there
have been discussions about that here some time ago...

> Nonetheless, I believe that wise use of exceptions is superior to the use of 
> ASSERTs calling aborts.  They also provide a superior approach to finding 
> errors than does dumping the life story of a process to stderr.

see above, Qt never used exceptions. First because they were not
supported on all platforms, lastly because they don't play well with
signals/slots.

> Sure, you can do that by passing error codes around, but that tends to lead 
> to 
> errors.

Some people have a different opinion about that. Exceptions vs. error
codes is a bit like the "best editor", or "best ide"...

Andreas

-- 
You now have Asian Flu.

--
To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
"unsubscribe" in the subject or the body.
List archive and information: http://lists.trolltech.com/qt-interest/

<Prev in Thread] Current Thread [Next in Thread>