I have a puzzle for the following code:
#include <iostream>
#include <vector>
int main() {
const size_t LARGE = 12000000000;
std::vector<long> vec(LARGE);
vec[0] = -1;
vec[LARGE - 1] = 1;
std::cout << vec.size() << std::endl;
return 0;
}
The output is:
12000000000
My RAM is 16G, but why I can allocate a vector of size 12x8G? I thought that vector has to be allocated as a continuous memory in heap.
If the allocation is in virtual memory, my virtual memory setting for my Windows 11 is:
Total paging file size for all drivers: 9216MB
So the total usable memory is 16G+9G=25G, which is far less than 12x8G=96G.
When I changed LARGE to 16G, the program throws bad_alloc error. What determines the usable memory in my PC?