Reputation: 1953
I think I am creating this pair incorrectly because I am getting a segfault when I debug using DDD. Can anyone see where I made a mistake? Thanks! The segfault happens when processing the input file:
char vertex = 'a';
Vertex* newVertex = new Vertex();
map.insert(pair<char,Vertex*>(vertex,newVertex));
Code:
void MSTapp::processFile()
{
int pos1;
int pos2;
map<char, Vertex*> adjacencyList;
vector<char> listOrder;
string input;
bool test = false;
while (getline(cin, input)) {
pos1 = pos2 = 0;
if(std::string::npos != input.find_first_of("0123456789"))
{
char source = input[0];
char destination = input[2];
stringstream ss(input.substr(4));
int weight;
ss >> weight;
Edge newEdge(destination, weight);
adjacencyList[source]->addEdge(destination, newEdge);
Edge roadBack(source, weight);
adjacencyList[destination]->addEdge(source, roadBack);
}
else
{
while(input.find(' ', pos1) != string::npos)
{
pos2 = input.find(' ', pos1);
char vertex = input[pos1];
listOrder.push_back(vertex);
Vertex* newVertex = new Vertex(vertex);
adjacencyList[vertex] = newVertex;
pos1 = pos2;
};
};
};
Graph graph(listOrder, adjacencyList);
prim(graph, adjacencyList[listOrder[0]]);
}
Input:
A B C D E F G
A B 3
A E 4
B C 7
B E 6
B F 5
C D 9
C F 8
D F 9
D G 4
E F 6
F G 8
Upvotes: 1
Views: 161
Reputation: 3525
If you are able to compile but are getting memory faults on this, I would look at the comparison function of your std::map<char, Vertex *, ...>
. The STL's map should be a Red-Black balanced binary tree, which does a less than/not less than comparison on nodes as it inserts and re-balances itself. I would guess that you either have a bad node comparison function or that Vertex members sometimes contain bad pointers that are getting touched by the comparison function in the insert()
call. Also, calling your std::map instance 'map' as above might be confusing even in example form.
Edit: nevermind, just saw the code added above, and you aren't doing anything fancy with your map definition/compare function.
If you are crashing on one of the two following lines, it means that your node lookups are failing and that you are trying to modify objects that aren't really there. Check these values (the Vertex *) before you attempt to read or modify them:
adjacencyList[source]->addEdge(destination, newEdge);
adjacencyList[destination]->addEdge(source, roadBack);
Put a breakpoint on your Vertex::addEdge(char, Vertex *) call and see what this
's address and contents show. std::map::operator[]
will silently create an element (using the default constructor) for a key on a failed lookup, so you might well be making null Vertex *s and then trying to add elements to them.
Upvotes: 0
Reputation: 726809
There are several ways of adding items to an std::map<K,V>
. The most common one is by using the index operator, like this:
map[vertex] = newVertex;
You can also make a pair explicitly by calling std::make_pair
:
map.insert(std::make_pair(vertex,newVertex));
Note that you do not need to specify type parameters of the pair being created, because std::make_pair
infers the types from the context.
Upvotes: 2