I am unable to capture a failure to allocate memory when inserting into a map, which results in a crash due to a segmentation fault.
struct Data
{
int64_t arr[100000000000000000]; // Large data to cause allocation failure
};
typedef unordered_map<int64_t, Data> UOM;
int main()
{
//void* p = malloc(sizeof(int64_t)*100000000000000000);
//void* p = new int64_t [100000000000000000];
//cout <<p <<endl;
UOM m;
try
{
m[1];
} catch(...)
{
}
}
If we used malloc it would return a NULL whereas new throws std::bad_alloc which can be caught. However, the insert into map which would need to allocate this memory just crashes (with a segmentation fault).
why is this the case and what should be behaviour as per standards?
EDIT: The platform, compiler are: Old compiler g++ 4.7.1 with "-std=c++11" flag on Redhat 6.10. Yeah we live in stone age!