Thread: Vector Size

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Vector Size

    I need a large string vector for file content manipulation.

    v1.max_size() gives me 153 391 689.

    But if I make a test by looping a vector pushing back strings it crashes around 16 - 26 000 000 though I still have a lot of ram (1GB left).

    How come and why isn't vector size limited by ram instead?
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I think it's because the number of elements is not equal to the actual size of the data used to store the vector and may max_size is in bytes, not elements.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    myvector.max_size() "Returns the maximum number of elements that the vector can hold."
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Post your test program.
    What are you actually storing in the vector?
    Is your system 32 or 64 bit?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    x64 machine x86 program.
    Storing words in the vector.
    This is the test program.

    Code:
    int count=0;
    
    int main ()
    {
        vector<string>v;
    
        while(1)
        {
            v.push_back("a");
            count++;
        }
    
        return 0;
    }
    Using Windows 10 with Code Blocks and MingW.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Notes:
    - Not all strings are equal in size; therefore, the maximum elements cannot be guaranteed as a maximum limit. Each string eats memory too.
    - In 32-bit programs, you are limited to around 2 GB of ram. If you don't want that, create a 64-bit program.

    I also seriously wonder if you need all that ram. Can you get away with less? Process it in chunks instead of reading all at once.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Not all strings are equal in size; therefore, the maximum elements cannot be guaranteed as a maximum limit. Each string eats memory too.
    In my test program I only push back "a"s and it crashes around 26 000 000.

    I cannot process it in chunks because I want to remove duplicates.
    Using Windows 10 with Code Blocks and MingW.

  8. #8
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by Ducky View Post
    How come and why isn't vector size limited by ram instead?
    It is.

    When you compile program for a 32-bit machine, the highest possible address is usually 0xFFFFFFFF (4294967295). Assuming that your std::string implementation needs (at least) 28 bytes for each string instance, this gives a theoretical maximum of 4294967295 / 28 = 153391689 strings.

    EDIT:

    Vector implementations have one drawback (which can also be an advantage) - they allocate memory contiguously. Each vector holds a preallocated storage which grows exponentially, usually by a factor between 1.5 - 2 (google its allocation strategy if you want to know more). This means that when your vector reaches the size of 2GB, next time it reallocates the memory, it attempts to find a contiguous chunk of 4GB (or 3), and it fails because it already occupies 2. This leaves you with a big chunk of free memory.

    You can try calling reserve() before you push anything, or try using a different container, e.g., std::deque.
    Last edited by kmdv; 12-10-2013 at 01:03 PM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ducky
    I cannot process it in chunks because I want to remove duplicates.
    Do you require the removal of consecutive duplicates only? Must ordering be maintained?

    If you just want to process the strings and get unique strings in any order, then perhaps you can process it in chunks using std::set<std::string>, assuming that the number of unique strings is sufficiently small.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Consecutive duplicates? Any duplicate in the file.

    The strings are small but I would need ordering, yes.

    I will try to use a different container then or maybe a multidimensional char or compile it in x64.
    Using Windows 10 with Code Blocks and MingW.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ducky
    The strings are small
    It then stands to reason that the number of unique strings is also small.

    Quote Originally Posted by Ducky
    I would need ordering, yes.
    That is rather ambiguous: do you mean to say that you need the strings to be ordered say, lexicographically, or do you mean to say that the order as given by the the first of each unique string must be maintained? A std::set is appropriate for the former, but for the latter it may either be impossible with std::set or be more complicated.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Sorry, meant to say ordered lexicographically.
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using stl's vector.size() with printf
    By manuels in forum C++ Programming
    Replies: 11
    Last Post: 08-18-2009, 07:47 PM
  2. vector::size() unitialized at creation
    By VirtualAce in forum C++ Programming
    Replies: 9
    Last Post: 03-08-2008, 01:48 AM
  3. A vector or an array of unknown size
    By strickey in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2005, 09:11 AM
  4. Vector their size and declarations
    By strickey in forum C++ Programming
    Replies: 13
    Last Post: 12-29-2004, 08:45 AM
  5. getting size of a vector
    By abrege in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2003, 09:48 PM