This shows how to access the pointer to the raw data in std::vector. I want something like that in Qt for QVector, QQueue and QList (If possible other containers).
For instance if we have some containers:
int * p1 = &vector;
int * p2 = &queue;
int * p3 = &list;
int * P1 = &vector;
int * P2 = &queue;
int * P3 = &list;
This does not seem like a good idea and might fail with a segmentation fault at some random point in the future. The reason that QList and QQueue don't have a
data() function is that QList does not guarantee that the list items are next to each other in memory. Compare:
QVector is one of Qt's generic container classes. It stores its items in adjacent memory locations and provides fast index-based access.
List is one of Qt's generic container classes. It stores a list of values and provides fast index-based access as well as fast insertions and removals.
Your test indicates that it works right now, and it also seems to work with 10000 or 1000000 items. But that's an implementation detail and there's no guarantee that some future version of Qt won't change the internal workings of QList for performance or other reasons.
Also, there's a catch: Everything larger than a pointer (e.g.
QList<someStruct>) will only be stored in the list as a pointer, while the actual item resides on the heap.
In fact, the Qt documentation states that QList does use arrays internally:
Internally, the QList is implemented using an array, ensuring that index-based access is very fast.
But even that wouldn't necessarily mean that all items are contiguous. One might imagine a far-fetched implementation where the list stores blocks of items, such that each block is an individual array.