tags:

views:

45

answers:

2

I am trying to model a network using C++. I have a struct called NetworkConnection:

struct NetworkConnection {
  int i, j, weight;
}

and I have a class called Network

class Network {
  public:
    std::vector<NetworkConnection> connections_for(int i) {
      return connections[i];
    }
    void connect(int i, int j, int weight) {
      NetworkConnection connection;
      connection.i = i;
      connection.j = j;
      connection.weight = weight;
      connections[i].push_back(connection)
    }
  private:
    std::vector< std::vector<NetworkConnection> > connections;
}

Now my problem is that I am getting segfaults when calling connections_for(i), specifically in the copy constructor. Confusingly however the precise circumstances of the segfault vary between runs of the application. I have tried using a vector of pointers and a vector of vectors of pointers like so:

std::vector< std::vector<NetworkConnection> * > connections;
std::vector< std::vector<NetworkConnection *> > connections;

with the appropriate adjustments to the interface but that did not solve the problem. Now I am at a loss as to how to fix this.

What am I doing wrong here? Or alternatively how would you model a network in C++ with an interface similar to the above?

+1  A: 

the segment fault problem usually comes from inappropriate or no initialization of a vector. you define the vector as

vector<vector<xxx> > xx;

you can use xx.pushback to add a new element, but when you use 'xx[i]' you need to make sure there are at least (i+1) elements in the vector. Initially there are 0.

you need to initialize the length of your vector. e.g.

connections.resize(nNodes);
Yin Zhu
+2  A: 

You want to use a map:

class Network {
  public:
    std::vector<NetworkConnection> connections_for(int i) {
      return connections[i];
    }
    void connect(int i, int j, int weight) {
      NetworkConnection connection;
      connection.i = i;
      connection.j = j;
      connection.weight = weight;
      connections[i].push_back(connection);
    }
  private:
    std::map<int, std::vector<NetworkConnection> > connections;
};

You may even want to use a map of maps:

class Network {
  public:
    std::map<int, NetworkConnection> &connections_for(int i) {
      return connections[i];
    }
    void connect(int i, int j, int weight) {
      NetworkConnection connection;
      connection.i = i;
      connection.j = j;
      connection.weight = weight;
      std::map<int, NetworkConnection> &map = connections_for(i);
      map[j] = connection;
    }
  private:
    std::map<int, std::map<int, NetworkConnection> > connections;
};
R Samuel Klatchko
My problem with the segfault turned out to be unrelated. I did however take this approach in modelling my network.
derfred