Search code examples
qtsizeofqlist

Why the size of the QList object is 4 bytes?


When trying to get the size of a QList object using sizeof(), function it gave me 4 bytes. I tried to change the number of items in the list, it also gave 4 bytes? I tried to change the type of the items in the list, again it gave 4 bytes?

Why the change in the number or type of items does not affect the object size?


Solution

  • To get the number of items in the list, use the QList::size() not sizeof(). Something like this:

    QList<MyObject> list;
    list.append(object1);
    list.append(object2);
    int size = list.size();
    

    sizeof() returns the size of the QList class, which is, as is often the case for Qt classes, made of only one member: a pointer to a private structure. Assuming you are on a 32bit OS, it takes 4 bytes to store an address, hence why you sizeof(list) returns 4.