'Where is the origin of this munmap_chuck(): invalid pointer error in C++?
I try to implement Hierholzer's algorithm in C++. The underlying multigraph is represented using a two-dimensional map, which maps two vertices to the number of edges between them. My approach is to decrement this count of an edge every time it is traversed and completely removing it if the count is 0.
This is the function:
vector<int> euler_tour(map_2d edge_copies) {
map<int, int> available_nodes;
available_nodes.insert({ 0, 0 });
vector<int> tour = { 0 };
while (!edge_copies.empty()) {
int n0 = (available_nodes.begin())->first;
int n = n0;
vector<int> subtour = { n0 };
while (!edge_copies.at(n).empty()) {
int n_ = (edge_copies.at(n).begin())->first;
subtour.push_back(n_);
cout << n << " " << n_ << endl;
cout << "graph and soubtour: " << endl;
print_map_2d(edge_copies);
print_vector(subtour);
edge_copies.at(n).at(n_) -= 1;
edge_copies.at(n_).at(n) -= 1;
if (edge_copies.at(n).at(n_) == 0) {
edge_copies.at(n).erase(n_);
edge_copies.at(n_).erase(n);
}
if (edge_copies.at(n).empty()) {
edge_copies.erase(n);
available_nodes.erase(n);
} else {
available_nodes[n] = available_nodes.at(n0) + subtour.size() - 1;
}
if (edge_copies.at(n_).empty()) {
edge_copies.erase(n_);
available_nodes.erase(n_);
} else {
available_nodes[n_] = available_nodes.at(n0) + subtour.size();
}
n = n_;
}
tour.insert(tour.begin() + available_nodes[n0], subtour.begin(), subtour.end());
print_vector(tour);
}
}
When I execute it, i get munmap_chunk(): invalid pointer after the first iteration of the second while loop. I experimented a bit, and I think it breaks at the line subtour.push_back(n_);. But why? This is a standard operation. Or do you see any other problematic lines?
The functions print_map_2d and print_vector are implemented elsewhere, they are surely not the problem.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
