[Qt-interest] QQueue / QList memory allocation behaviour

Thiago Macieira thiago at kde.org
Mon May 16 08:24:31 CEST 2011


On Monday, 16 de May de 2011 08:13:09 Schimkowitsch Robert wrote:
> I have a QQueue<MyClass*>, or a QList<MyClass*> where I mainly append at
> the end, and remove in front.

QQueue is implemented by inheriting a QList.

> Let's assume I start out with the maximum size of Queue or List, and
> then let it shrink and grow as needed, but never beyond the original
> size.
> 
> Will it perform new and delete operations continuously, once in a while,
> or will it only re-use already allocated memory?

It will, sometimes. Since you're using a pointer as the type contained, QList 
will store the pointers in its own pointer array, so it will avoid an 
indirection to the pointer.

If you keep the container at the same size and never exceed the maximum, it 
will probably not reallocated. If you exceed the allocated size, it will 
reallocate and memcpy all the pointers. If you shrink too much, it might 
reallocate and discard unused memory.

> The question behind this: Will I create memory fragmentation when using
> QQueue / QList in this fashion in multiple threads?

That's a very hard question to answer, as memory fragmentation depends on what 
the memory allocator does.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Senior Product Manager - Nokia, Qt Development Frameworks
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110516/d8423006/attachment.bin 


More information about the Qt-interest-old mailing list