While experimenting with dynamic memory allocation using new, I discovered that is possible to request an amount of memory that is so large that the implementation doesn't throw a bad_alloc exception, but simply crashes with an invalid allocation error. I attempted testing the size of the allocation using a simple test to the effect
if (n >= SIZE_MAX / sizeof(double))
// throw some exception
else
// try to allocate memory
where n is the number of doubles that I am attempting to allocate, but I discovered that requests that pass this test can still cause the program to crash. Is there a simple test that is portable that can determine if a request is safe?
Thank you for your time.
edit: I apologize, here is an example of a program that crashes on my system. I created the program using visual studio 2013.
#include <cstdint>
#include <new>
int main()
{
size_t n = SIZE_MAX / sizeof(double);
double* ptr;
try {
new double[n];
} catch (std::bad_alloc& e){ };
}