C++ Large 2D array (on the heap) crashes the application

It looks like you’re using too much memory.
So, if operator new can’t allocate memory for some reasons (it’s not enough of free memory, for example), it throws an exception std::bad_alloc.
Use try-catch block to detect thrown exceptions, like this:

try{
   float **heights;
   heights = new float*[numVertices];
   for (int i = 0; i < numVertices; i++){
      heights[i] = new float[numVertices];
   }
}
catch(std::exception exc){ std::cout << exc.what() << std::endl; }
system("pause");

Also, read some articles about std::bad_alloc and operator new

Leave a Comment