Results 1 to 3 of 3

Thread: Can QHash::capacity() be smaller than QHash::size()?!?

  1. #1
    Join Date
    Jul 2007
    Posts
    35
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Can QHash::capacity() be smaller than QHash::size()?!?

    I'm trying to optimize memory usage in my code.
    I found that some Qt containers have capacity(), reserve() and squeeze() functions for this purpose.
    Making some tests I found a strange result (among normal ones!):
    in some conditions I get capacity() < size().
    If I understand correctly the documentation, capacity() should give the number of allocated items (which generally is greater than the number of inserted items, for speed reasons).

    Qt Code:
    1. QHash< DataUniqueId, DrawItem* > m_lookupTable;
    2.  
    3. // Insert data into m_lookupTable
    4. for (...) {
    5. m_lookupTable.insert(..., ...);
    6. }
    7.  
    8. qDebug() << m_lookupTable.size() << m_lookupTable.capacity();
    9.  
    10. // No more insertions, so release any unused memory
    11. m_lookupTable.squeeze();
    12.  
    13. qDebug() << m_lookupTable.size() << m_lookupTable.capacity();
    To copy to clipboard, switch view to plain text mode 

    This is the output:
    Qt Code:
    1. 500 521
    2. 500 257
    To copy to clipboard, switch view to plain text mode 

    As you can see, after the squeeze(), size() > capacity().
    How can the number of allocated items be less than the number of real items?
    Am I doing something wrong?

    Thanks in advance,
    Alessandro

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can QHash::capacity() be smaller than QHash::size()?!?

    Quote Originally Posted by iw2nhl View Post
    If I understand correctly the documentation, capacity() should give the number of allocated items (which generally is greater than the number of inserted items, for speed reasons).
    In case of QHash the docs say:
    int QHash::capacity () const
    Returns the number of buckets in the QHash's internal hash table.
    Buckets are not items --- they might contain more than one (key, value) pair.

  3. The following user says thank you to jacek for this useful post:

    iw2nhl (24th August 2007)

  4. #3
    Join Date
    Jul 2007
    Posts
    35
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Can QHash::capacity() be smaller than QHash::size()?!?

    Quote Originally Posted by jacek View Post
    Buckets are not items --- they might contain more than one (key, value) pair.
    Thank you very much for the explanation!
    Now I'm more secure about my code...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.