[Qt-creator] QList and QStringList, or qDebug() odd Behaviour?

Andre Poenitz andre.poenitz at mathematik.tu-chemnitz.de
Tue Mar 23 21:31:40 CET 2010


On Tue, Mar 23, 2010 at 10:07:07AM -1000, Victor Sardina wrote:
> Trolls:
> 
> I have just recently noticed something rather unusual going on with the
> behaviour of the "[]" operator for the QList and QStringList objects via
> qDebug(). For example:
> 
> for(int i = 0; i != staNames->length(); i++)
> {  qDebug() << staNames[i] << endl; //->at(i);
>    qDebug() << staLats[i] << endl;  //->at(i);
>    qDebug() << staLons[i] << endl;  //->at(i);
> }
> 
> or
> 
> for(int i = 0; i != staNames->length(); i++)
> {  qDebug() << *(staNames+ i) << endl; //->at(i);
>    qDebug() << *(staLats + i) << endl;  //->at(i);
>    qDebug() << *(staLons + i) << endl;  //->at(i);
> }

What is "staNames"? A pointer to a single QStringList?
Then staNames + 1 would be out-of-bounds...
 
> will produce a "serialized" output of the three objects (QStringList,
> QList<qreal>, and QList<qreal>, with all values in one object followed
> by all values in the next, and so on, as though the index "0" retrieves
> all values in memory at once, before it crashes once the "i" counter
> increments to 1. I allocated all three objects dynamically via operator
> new and populated them via "append" (push_back). The interesting thing,
> however, has to do with the fact that replacing the call to the "[]"
> operator with a call to "at" produces the intended output, namely, all
> three values one at a time for each object as the counter increments.
> 
> Can someone provide some insight on the reasons behind the seemingly odd
> behaviour (for me at least)?

Assuming you run into undefined behaviour, "randomness" is all you might
expect - and it's hard to tell from the snippet what you are doing
exactly. Minimal but self-contained test code usually helps finding
explanations.

The fact that at() does not detach on shared objects while [] does might
contribute to the observed difference.

Andre'



More information about the Qt-creator-old mailing list