Solved Segmentation fault / multidimansional arrays
-
Hi all. I've got a problem during creating 2-dimensional array.
Graph::Graph(){ vertexList = new Vertex[MAX_VERTS]; adjMat = new int*[MAX_VERTS]; // int** adjMat; for(int i(0); i < MAX_VERTS; i++){ //const int MAX_VERTS = 10; adjMat[MAX_VERTS] = new int[MAX_VERTS]; } for(int i(0); i < MAX_VERTS; i++) for(int j(0); j < MAX_VERTS; j++) adjMat[i][j] = 0; //sigmentation fault here nVerts = 0; }
qt 5.5.1, gcc compiler.
-
Hi and welcome to devnet,
Nothing Qt specific here, look again at your for loop, you are initializing the same value over and over.
-
Yes, exactly as SGaist says above, try changing
adjMat[MAX_VERTS] = new int[MAX_VERTS];
to
adjMat[i] = new int[MAX_VERTS];
-
@SGaist thanks a lot=)