Search code examples
qtstackheap-memoryqstringqstringlist

Does QStringList store data on stack or on heap?


In the code below:

void fun()
{
    QString aa("aa");
    QString bb("bb");
    QStringList a;
    a<<aa;
    a<<bb;
}

I know QString stores its data on heap, but how about QStringList? Does it allocate an array on stack, or on heap and only store the pointer to the array on stack?


Solution

  • QList technically is:

    class QList
    {
        QListData::Data* p;
    };
    

    where QListData::Data is:

    struct Data 
    {
        QtPrivate::RefCount ref;
        int alloc, begin, end;
        void *array[1];
    };
    

    So QList itself allocates memory on the heap and stores a pointer to it.