Hi again,
I might post the essential problem in a short code snippet. Is the following code guaranteed to evaluate right for MyList = QLinkedList?
#include <QtCore/QList>
#include <QtCore/QLinkedList>
#include <QtCore/QString>
#include <QtCore/QDebug>
#define MyList QLinkedList
int main(int argc, char *argv[])
{
MyList<QString> qList;
qList.append("One");
MyList<QString>::iterator qListElement1 = qList.end() - 1;
qList.append("Two");
MyList<QString>::iterator qListElement2 = qList.end() - 1;
qList.append("Three");
MyList<QString>::iterator qListElement3 = qList.end() - 1;
qList.append("Four");
MyList<QString>::iterator qListElement4 = qList.end() - 1;
//This will crash for QList, but not for QLinkedList
qList.erase(qListElement2);
for(MyList<QString>::iterator i = qList.begin();
i != qList.end();
++i)
{
qDebug() << *i;
}
return 0;
}
#include <QtCore/QList>
#include <QtCore/QLinkedList>
#include <QtCore/QString>
#include <QtCore/QDebug>
#define MyList QLinkedList
int main(int argc, char *argv[])
{
MyList<QString> qList;
qList.append("One");
MyList<QString>::iterator qListElement1 = qList.end() - 1;
qList.append("Two");
MyList<QString>::iterator qListElement2 = qList.end() - 1;
qList.append("Three");
MyList<QString>::iterator qListElement3 = qList.end() - 1;
qList.append("Four");
MyList<QString>::iterator qListElement4 = qList.end() - 1;
//This will crash for QList, but not for QLinkedList
qList.erase(qListElement2);
for(MyList<QString>::iterator i = qList.begin();
i != qList.end();
++i)
{
qDebug() << *i;
}
return 0;
}
To copy to clipboard, switch view to plain text mode
I assume that it does not work with QList, because it is somehow implemented like a kind of std::deque, which means it allocates a whole memory block. When deleting an element in such a data structure, other Iterators might point to some illegal position in memory. But: Can I be sure that this is not the case with a QLinkedList, because each element always remains at the same position in memory, no matter if new elements are inserted or some elements are deleted?
Greetz,
N3O
Bookmarks