Results 1 to 4 of 4

Thread: QVector::reserve() bug?

  1. #1
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QVector::reserve() bug?

    What is wrong in my code?
    Qt Code:
    1. #include <QVector>
    2.  
    3. int main()
    4. {
    5. QVector<int> v;
    6. printf("v.size()=%u\n", v.size());
    7. v.reserve(2);
    8. printf("v.size()=%u\n", v.size());
    9. }
    To copy to clipboard, switch view to plain text mode 
    Result:
    Qt Code:
    1. v.size()=0
    2. v.size()=0
    To copy to clipboard, switch view to plain text mode 
    I expected that reserve() does allocate the required memory, as resize() does, so how does reserve() work actually?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QVector::reserve() bug?

    QVector::reserve() doesn't actually resize the vector but attempts to allocate memory for given amount of items. This ensures that the underlying data structure has large enough continuous memory block to enlarge up till given amount of items without need of moving data.
    J-P Nurmi

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

    Caius Aérobus (3rd March 2008)

  4. #3
    Join Date
    Jun 2019
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QVector::reserve() bug?

    You need to check the capacity of the vector. The size will change once you add the elements into it.

  5. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    854
    Thanks
    68
    Thanked 56 Times in 54 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QVector::reserve() bug?

    Wow, only after 11 years you found a solution.

    Please, look the posts date before answering anything.
    Òscar Llarch i Galán

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.