I would like to fail gracefully with an error message if vnl_matrix (c++ code) cannot allocate enough memory for initialisation or resizing. Just constructing a large matrix produces a segfault:
#include <vnl_matrix.h>
#include <iostream>
int main ()
{
vnl_matrix<float> B((unsigned)1000000000,6);
}
Also using the set_size method as in this example produces a segfault during set_size instead of returning false:
int main ()
{
vnl_matrix<float> A(3,3);
std::cout << "A is " << A.rows() << "x" << A.columns() << std::endl;
A.clear();
std::cout << "A is " << A.rows() << "x" << A.columns() << std::endl;
bool ok = A.set_size((unsigned)1069350838, 6);
if (ok)
std::cout << "A is " << A.rows() << "x" << A.columns() << std::endl;
else
{
std::cout << "some error occured" << std::endl;
return 1;
}
A[(unsigned)700000000][5] = 1.0;
}
In my original code (too long to post) I even have a situation where the set_size call works and return TRUE, but later when writing to a matrix element, it segfaults then. This is on a machine with hundreds of GB or RAM. The matrix above should only be around 25GB.
Link to vnl_matrix: https://vxl.github.io/doc/release/core/vnl/html/classvnl__matrix.html
Is there any way to check if memory allocation actually worked?
Thanks.