Declaration of a very large vector ints?

Is there a way to do this in C ++ without crashing at runtime?

Now i'm announcing

vector<vector<int> > myvec(veclength);

How can I crank the veclength as high as possible (correctly)? Even at 10 ^ 7 it crashes when I should have more than enough computer memory.

+3
source share
2 answers

It will take approximately 250 MB of space 1 (or less, depending on architecture), therefore memory is definitely not a problem here, and none of them should max_size, which will be in order 10 17 (≈ 2 64 / 8 + 8 + 8)

, , std::vector GCC 'libstd++ LLVM lib++, . 1:1 , OPs veclength = 10e7.

, .


1) 64- , , , 0. , , , .

+2

, , .

max_size , max_size . , limit - int, , .

, . , int ( 4) 1073741823, 10 ^ 9. p > , , , .

Max elements that can be inserted into a vector having elements of size '1' is: 4294967295
Max elements that can be inserted into a vector having elements of size '4' is: 1073741823
Max elements that can be inserted into a vector having elements of size '8' is: 536870911
Max elements that can be inserted into a vector having elements of size '4' is: 1073741823

vector vector<int>, . , , vector.


: http://codepad.org/nAoPi7cV

int main()
{
   std::cout << "Max elements that can be inserted into a vector having elements of size '" 
             << sizeof( std::vector<int> ) << "' is: "
             << std::vector<std::vector<int> >().max_size() << std::endl;
}   

, , "4": 1073741823

, , "28": 153391689

log (153391689) ~ = 8.2

, max_size , 10 ^ 7 . .


, , segfaults : http://codepad.org/agKMMEjQ

int main()
{
   std::vector<std::vector<int> > myvec(153391689);
}


, (10 ^ 7), : http://codepad.org/zMG0VCeg

std::vector<std::vector<int> > myvec(10000000);

std:: bad_alloc: St9bad_alloc

.


, , : http://codepad.org/sbMPppgx

std::vector<std::vector<int> > myvec(100000);

std::cout << myvec.size();

100000


, , - - std::vector. Codepad , .

-2

All Articles