0

In Multidimensional arrays as class member with arbitrary bounds I was advised I should use a vector of vectors of ints to represent a dynamic 2D array in my array-backed graph object. However, I'm getting segmentation faults whenever I try to run my code. GDB points to the line where a variable is defined as the arraygraph type; I presume that means I did something wrong specifying the type of edges. Here is the class definition:

class arraygraph {
private:

public:
    void open(char *filename);
    bool exists(int nodeid);
    int node_count(void);
    int weight(int nodea, int nodeb);
    void print();
private:
    int count;
    vector <vector <int> > edges; //A vector of vectors! [Inception Noise]
};

All the methods of this object are defined (funny how you can compile with prototypes that haven't been filled out...). The code compiles without errors or warnings. Why's it segfaulting?

EDIT:

Here's some disassembler output:

163       int main(int argc, char *argv[]) {
          main(int, char**):
00401c86:   lea 0x4(%esp),%ecx
00401c8a:   and $0xfffffff0,%esp
00401c8d:   pushl -0x4(%ecx)
00401c90:   push %ebp
00401c91:   mov %esp,%ebp
00401c93:   push %ebx
00401c94:   push %ecx
00401c95:   sub $0x20,%esp
00401c98:   mov %ecx,%ebx
00401c9a:   call 0x402350 <__main>
164         arraygraph thegraph;
00401c9f:   lea -0x18(%ebp),%eax  (this is where the problem occured according to GDB)
00401ca2:   mov %eax,%ecx
00401ca4:   call 0x403e90 <arraygraph::arraygraph()>

So apparently the segfault happened before the arraygraph was constructed? I don't know what to make of this.

EDIT:

The entirety of main() is as such:

int main(int argc, char *argv[]) {
    arraygraph thegraph;
    thegraph.open(argv[1]);
    return 0; //Added this after I noticed I'd omitted it - didn't fix anything
}

And here's arraygraph::open():

//Only call .open() once. A second call will leave the old graph's dessicated corpse around the edges of the new one.
void arraygraph::open(char *filename){
    int count;
    int x, y;
    tomgraph loader;

    loader.open(filename);

    count=loader.node_count();

    //delete edges;
    //edges = new vector <vector <int> >;

    for (x=1; x <= count; x++) {
        for (y=1; y <= count; y++) {
            int weight;
            if (loader.is_connected(x,y,&weight) || loader.is_connected(y,x,&weight)) {
                edges[x-1][y-1]=weight;
            } else {
                edges[x-1][y-1]=0; //0 represents "no edge"
            }
        }
    }
}

But according to the disassembler this never gets called anyway.

EDIT:

Full code here. Umm... I think it's saying it worked? Lemme try a different machine...

EDIT:

Nope. Similar segfault after compiling on a completely separate Linux machine.

EDIT:

Just to confirm, I commented out the call to thegraph.open(). Didn't fix it.

Community
  • 1
  • 1
Schilcote
  • 2,344
  • 1
  • 17
  • 35
  • You probably want a constructor for that, also can you provide the source code where the error happens? the error is not necessarily caused by the declaration but maybe the code surrounding it. – Alex Díaz Mar 30 '15 at 00:23
  • @AlejandroDíaz What exactly would the constructor do? There is no code surrounding it; `arraygraph mygraph` is the first line in `main()`. – Schilcote Mar 30 '15 at 00:25
  • It would make sure that the object is in a defined state(ie count is 0), http://ideone.com/U3pHyg your code works as it is so the class definition is not the problem. – Alex Díaz Mar 30 '15 at 00:29
  • How does your instantiation actually look like? – πάντα ῥεῖ Mar 30 '15 at 00:29
  • Can you provide a complete compiling example that shows the problem. – Neil Kirk Mar 30 '15 at 00:33
  • @Schilcote weird, ideone.com says it works but in my computer I get a sigsegv as well... – Alex Díaz Mar 30 '15 at 00:40

1 Answers1

0

You never resize edges or push_back to it. So its size is 0 and doing edges[x-1][y-1] you go out of bounds. You probably have to do something like this before the loop in arraygraph::open:

edges.resize(count, std::vector<int>(count));
Anton Savin
  • 40,838
  • 8
  • 54
  • 90
  • Adding resizes changes nothing. As the disasm shows, open() never gets called anyway. – Schilcote Mar 30 '15 at 00:58
  • @Schilcote Sometimes you have to put away the debugger and use the good old eyes. You never sized the array before you used `[ ]`, thus you are accessing an invalid item. If the issue isn't `open`, then remove that function altogether and see if that program actually works. If it doesn't work, then you have a seriously broken compiler or broken standard library. – PaulMcKenzie Mar 30 '15 at 02:40
  • @PaulMcKenzie Yeah, turned out commenting out the call *did* stop the segfaults. But not the first time I tried it. Apparently GDB was just stopping on a random instruction. I just sat down and rewrote the whole thing in python today. :P – Schilcote Mar 31 '15 at 03:27