[Development] QList

Ville Voutilainen ville.voutilainen at gmail.com
Mon Mar 20 13:37:41 CET 2017


On 20 March 2017 at 14:13, Robin Burchell <robin.burchell at crimson.no> wrote:
>> And when size matters, have this in mind:
>>
>> sizeof(std::vector<int>) == 32
>> sizeof(QVector<int>) == 8
>> sizeof(QList<int>) == 8
>
> To provide a more helpful response than Marc's: I assume that you are
> aware that the Qt types just contain a pointer to a thing which contains
> the actual data (unlike std::vector), right?

libstdc++ vector is 24, it seems as if msvc's vector is just padded to
an alignment boundary(*). I don't
see anything particularly alarming in that size.

(*) it might be remotely possible that it fails to remove storage for
an empty allocator, but I kinda doubt that.

> That is, QVector<int> is actually (snipped from source):
>     template <typename T>
>     class QVector
>     {
>         typedef QTypedArrayData<T> Data;
>         Data *d;
>     }
>
> Where QTypedArrayData<T> is a QArrayData, which is:
>     struct Q_CORE_EXPORT QArrayData
>     {
>         QtPrivate::RefCount ref;
>         int size;
>         uint alloc : 31;
>         uint capacityReserved : 1;
>         qptrdiff offset;
>     }
>
> If I'm adding it up right, this gives me a total cost of 32 bytes on my
> system. Although the real cost is actually higher I'd guess, due to the
> two being allocated in separate pieces (given that malloc usually has
> some kind of book-keeping overhead).


Yep.



More information about the Development mailing list