Results 1 to 2 of 2

Thread: vector memory allocation

  1. #1
    Join Date
    Jan 2006
    Location
    Munich, Germany.
    Posts
    111
    Thanks
    29
    Thanked 3 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default vector memory allocation

    Hi all,
    I know this is all very compiler/machine specific but...

    how much space will
    std::vector<int> aVector;
    assign usually?

    will my first aVector.push_back(n) have to assign memory before doing the push?

    when aVector.push_back(n) is called repeatedly will it double the memory for the vector when needed or will it add blocks of some size?

    eg.
    std::vector<int> aVector;
    aVector.reserve(1000);
    The reserve() might not be necessary and could even be counter-productive if the definition assigns a block of 1024 anyway(?)
    I'm searching an array for some condition that could appear once or 100000 times or not at all i.e I have no idea how big my vector will get but I don't want to assign 100000 places from the start. The process should be as fast as possible. Is it best to leave memory mangament to the machine, or should try to use reserve() cleverly?

    thanks
    K

  2. #2
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: vector memory allocation

    Which is more time critical ... adding elements or searching them? Will you be inserting/prepending or just appending?

    I generally just let the std::vector handle allocation for me unless I have specific knowledge about how many elements I need.

    You might consider the std::deque. It's usually better at managing memory ... especially when dealing with unknown growth since it allocates in blocks rather than total reallocation.

Similar Threads

  1. QDrag : memory allocation
    By kghose in forum Qt Programming
    Replies: 1
    Last Post: 14th August 2008, 23:57
  2. Memory allocation failure and crash in QVector
    By ashatilo in forum Qt Programming
    Replies: 16
    Last Post: 21st October 2007, 00:27
  3. Vector allocation
    By ToddAtWSU in forum General Programming
    Replies: 4
    Last Post: 22nd June 2007, 22:37
  4. limit memory allocation
    By magland in forum General Programming
    Replies: 10
    Last Post: 23rd March 2007, 10:21

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.