Reputation: 59
The test code as follows and compiled by vs2013:
#include <memory>
#include <list>
#include <stdint.h>
int main()
{
uint32_t one_size = 32 * 1024;
uint64_t total_size = 0;
auto deleter = [](char* p) { delete[] p; };
using SharedBuffer = std::pair<std::shared_ptr<char>, int>;
std::list<SharedBuffer> buffers;
while (total_size < (uint32_t)2 * 1024 * 1024 * 1024)
{
std::shared_ptr<char> buffer;
try
{
buffer = std::shared_ptr<char>(new char[one_size], deleter);
total_size += one_size;
}
catch (const std::bad_alloc& e)
{
printf("%s\n", e.what());
break;
}
try
{
buffers.emplace_back(std::make_pair(buffer, one_size));
}
catch (const std::bad_alloc& e)
{
printf("%s\n", e.what());
break;
}
}
return 0;
}
When the process's memory reached to 2GB, it will catch a bad_alloc exception, but 10GB physical memory available of 32GB total.
So, why caused it?
Upvotes: 2
Views: 1177
Reputation: 3529
32 bit Windows programs will only have 2GB of virtual address space available unless the Large Address Aware flag is set in which case it will have 3GB on a 32 bit host and a full 4GB on a 64 bit host. Even so, you won't be able to allocate a contiguous single block over 3GB.
If you want to extend past that boundary you need to make a 64-bit build of your application.
Upvotes: 3