2D dynamic array

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
I don't think you've provided enough information for us to find your segfault.

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
#include <iostream>

template<typename T>
class Array2D {
    int rows_, cols_;
    T** m_;
public:
    Array2D(int rows, int cols) : rows_(rows), cols_(cols) {
        m_ = new T*[rows];
        m_[0] = new T[rows * cols];
        for (int i = 1; i < rows; ++i)
            m_[i] = m_[i - 1] + cols;
        clear();
    }
    ~Array2D() {
        delete[] m_[0];
        delete[] m_;
    }
    void clear() {
        for (int r = 0; r < rows_; ++r)
            for (int c = 0; c < cols_; ++c)
                m_[r][c] = T();
    }
    void print() {
        for (int r = 0; r < rows_; ++r) {
            for (int c = 0; c < cols_; ++c)
                std::cout << m_[r][c] << ' ';;
            std::cout << '\n';
        }
        std::cout << '\n';
    }
    int rows() { return rows_; }
    int cols() { return cols_; }
    T* operator[](size_t i) { return m_[i]; }
    const T* operator[](size_t i) const { return m_[i]; }
};

int main() {
    Array2D<bool> m(5, 5);
    m.print();
    m[2][3] = true;
    m.print();
}

That's the code.


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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
class Graph
{
    public:
        Graph();
        void AddEdge(int no1,int no2);
        int GetVertices(){return (vertices-1);}
        int GetEdges(){return edges;}
        bool DeleteEdge(int no1,int no2);
    protected:

    private:
        int graphsize = 10;
        bool **adjacencymatrix;
        int *garray;
        int vertices = 0;
        int edges = 0;
        void Expand();
};


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();
    }
}

//Expands the graphs size
void Graph::Expand()
{
    int oldgraphsize = graphsize;
    graphsize = graphsize + 100;
    bool **newadjacencymatrix = new bool*[graphsize];
    for (int i;i<oldgraphsize;i++)
    {
        newadjacencymatrix[i] = new bool[graphsize];
        for(int k;k<oldgraphsize;k++)
        {
            newadjacencymatrix[i][k] = adjacencymatrix[i][k];
            newadjacencymatrix[k][i] = adjacencymatrix[k][i];
        }
    }
    delete[] adjacencymatrix;
    adjacencymatrix = newadjacencymatrix;
    int *newgarray = new int[graphsize];
    for(int i=0;i<oldgraphsize;i++)
    {
        newgarray[i] = garray[i];
    }
    delete[] garray;
    garray = newgarray;


}

//Deletes the edge of the given numbers,if they exist
bool Graph::DeleteEdge(int no1,int no2)
{
    bool flag = false;
    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 exist deletes the edge
    if((index1 != -1) && (index2 != -1))
    {
        adjacencymatrix[index1][index2] = false;
        adjacencymatrix[index2][index1] = false;
        flag = true;
    }
    return flag;

}
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
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
@GeorgeGento, you'll have to post your current version of the code to proceed with us here.
Well, i've fixed the code and now it works perfectly.Thanks a lot for the help.
Topic archived. No new replies allowed.