Take the 2-minute tour ×  I want to create and initialize a 2D
array but the initialization is failed. I encounter "The program
has unexpectedly finished." in Qt Creator.  What is wrong?  in
.h file      int pop;     int d;     float **uye;  in .cpp file
uye = new float*[pop]; for(int i=0; i<d; i++) uye[i] = new
float[d];  for(int n=0; n<pop; n++)     for(int m=0; m<d; m++)
uye[n][m] = (float) n*m; share|improve this question A 2D jagged
array with dimensions determined at runtime that doesn't leak:
std::vector<std::vector<float>>. –  R. Martinho Fernandes Dec 25
'11 at 11:49 add comment  2 Answers  up vote 3 down vote
accepted  The first loop for(int i=0; i<d; i++) should probably
be for(int i=0; i<pop; i++). Otherwise, you are only reserving
space for 12 elements, but later try to access 50.  Note that
having raw pointer members is considered a very bad idea in
modern C++, because you need to worry about copying semantics.
Better use a flat std::vector<float> and do the 2D to 1D mapping
manually, o