Jun 16, 2019 at 2:42pm UTC
Hello.I have a problem using the 2d dynamic array.Althought i dont see any fault the debugger shows segmentation fault when i'm trying to access the the array using the index1 and index2.Thanks in advance.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
Graph::Graph()
{
adjacencymatrix = new bool *[graphsize];
for (int i = 0;i<graphsize;i++)
{
adjacencymatrix[i] = new bool [graphsize];
}
for (int i = 0;i<graphsize;i++)
{
for (int k = 0;k<graphsize;k++)
{
adjacencymatrix[i][k] = false ;
adjacencymatrix[k][i] = false ;
}
}
garray = new int [graphsize];
for (int i=0;i<graphsize;i++)
garray[i] = -1;
}
//Adds new edge
void Graph::AddEdge(int no1,int no2)
{
if (vertices == 0)
{
int vertices2 = vertices;
garray[vertices] = no1;
vertices++;
garray[vertices] = no2;
adjacencymatrix[vertices2][vertices] = true ;
adjacencymatrix[vertices][vertices2] = true ;
adjacencymatrix[vertices2][vertices2] = true ;
adjacencymatrix[vertices][vertices] = true ;
}
int index1 = -1;
int index2 = -1;
//Tries to find the indexes that the numbers are located
for (int i = 0;i<=vertices;i++)
{
if (garray[i] == no1)
index1 = i;
if (garray[i] == no2)
index2 = i;
}
//If the numbers aren't in the array we put them in
if (index1 == -1)
{
index1 = vertices;
garray[index1] = no1;
adjacencymatrix[index1][index1] = true ;
vertices++;
}
if (index2 == -1)
{
index2 = vertices;
garray[index2] = no2;
adjacencymatrix[index2][index2] = true ;
vertices++;
}
adjacencymatrix[index1][index2] = true ;
adjacencymatrix[index2][index1] = true ;
edges++;
if (vertices == graphsize)
{
Expand();
}
Last edited on Jun 16, 2019 at 2:48pm UTC
Jun 16, 2019 at 4:10pm UTC
See anything wrong here?
1 2 3 4
for (int i;i<oldgraphsize;i++)
{
newadjacencymatrix[i] = new bool [graphsize];
for (int k;k<oldgraphsize;k++)
Also you aren't deleting the rows of the old adjacentymatrix.
And you should have a destructor.
Also, this doesn't make much sense:
1 2 3 4 5 6 7 8
for (int i = 0;i<graphsize;i++)
{
for (int k = 0;k<graphsize;k++)
{
adjacencymatrix[i][k] = false ;
adjacencymatrix[k][i] = false ;
}
}
It should just be
1 2 3
for (int i = 0;i<graphsize;i++)
for (int k = 0;k<graphsize;k++)
adjacencymatrix[i][k] = false ;
You do a similar thing in Expand, except that there you are trying to access memory through uninitialized pointers!
Last edited on Jun 16, 2019 at 4:17pm UTC
Jun 16, 2019 at 5:08pm UTC
Since it's undirected graph shouldn't i make both false?Else that would be directed graph right?Also i changed it so that it deletes the rows too and i was planning to create a destructor.I still get the segmantation faults even after fixing all these.Also after a few tests
it seems like it works fine for 9 inserts but not for more
Last edited on Jun 16, 2019 at 5:20pm UTC
Jun 16, 2019 at 6:28pm UTC
@GeorgeGento, you'll have to post your current version of the code to proceed with us here.
Jun 16, 2019 at 6:47pm UTC
Well, i've fixed the code and now it works perfectly.Thanks a lot for the help.