具有相同数组索引的C ++分段错误(C++ Segmentation fault with same array index)

编程入门 行业动态 更新时间:2024-10-07 08:27:03
具有相同数组索引的C ++分段错误(C++ Segmentation fault with same array index)

我有以下一段代码:

bool *pho=new bool[n]; memset(pho, 0, sizeof(bool) * n); for (int i = 0; i < m; i++) { int d=2; cout << "i=" << i << ", d="<<d<< endl; pho[d] = true; }

以输入n=8运行会得到以下输出:

i=0, d=2 i=1, d=2 [Segfault]

我不明白为什么会发生这种情况! 由于某种原因,在阵列中设置相同的位置会导致段错误。 我已经多次运行该程序,并且它总是生成相同的输出。

通过调试器遍历代码,我可以看到当访问数组时,d(索引)的值为2。

我曾尝试使用全局数组和静态全局数组,这两者都会导致相同的错误。

我的IDE和编译器有问题吗? 我正在使用MinGW和Eclipse CDT,并且启用了std / c ++ 11选项。

这是整个源文件,以防程序的其他部分导致问题:

#include <iostream> #include <queue> #include <vector> #include <unordered_set> #include <utility> #include <algorithm> #include <cstring> using namespace std; vector<unordered_set<int>> adj; static bool *visited; pair<int, int> dfs(int node) { if (visited[node]) return make_pair(0, node); pair<int, int> best = make_pair(0, node); for (int neigh : adj[node]) { pair<int, int> alt = dfs(node); alt.second++; best = max(best, alt); } return best; } int main(int argc, char** argv) { int n, m, def; cin >> n ; cin >> m; bool *pho=new bool[n]; memset(pho, 0, sizeof(bool) * n); int *degrees=new int[n]; memset(degrees, 0, sizeof(int) * n); cout << "n="<<n<<", m="<<m<<endl; for (int i = 0; i < m; i++) { int d=2; cout << "i=" << i << ", d="<<d<< endl; pho[d] = true; } for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; adj[a].insert(b); adj[b].insert(a); degrees[a]++; degrees[b]++; } queue<int> next; for (int i = 0; i < n; i++) { if (degrees[i] == 0) { next.push(i); } } while (!next.empty()) { int node = next.front(); next.pop(); if (pho[node]) continue; for (int neigh : adj[node]) { adj[node].erase(neigh); adj[neigh].erase(node); degrees[node]--; degrees[neigh]--; if (degrees[neigh] == 1) next.push(neigh); } } visited=new bool[n]; memset(visited, 0, sizeof(bool) * n); pair<int, int> pivot = dfs(def); memset(visited, 0, sizeof(bool) * n); pair<int, int> end = dfs(pivot.second); int dist = end.first; //number of edges she only has to walk once int tree = n - 1; //number of edges in tree int otherdist = tree - dist; //number of edges she has to walk twice int total = dist + otherdist * 2; cout << total << endl; return 0; }

I have the following piece of code:

bool *pho=new bool[n]; memset(pho, 0, sizeof(bool) * n); for (int i = 0; i < m; i++) { int d=2; cout << "i=" << i << ", d="<<d<< endl; pho[d] = true; }

Running with input n=8 results in the following output:

i=0, d=2 i=1, d=2 [Segfault]

I don't understand why this is happening! Setting the same location in the array results in a segfault for some reason. I have run the program several times and it always produces the same output.

Stepping through the code with a debugger, I can see that the value of d (the index) is 2 when the array gets accessed.

I have tried using global arrays and also static global arrays, both of which result in the same error.

Is there something wrong with my IDE and compiler? I am using MinGW with Eclipse CDT, running with std/c++11 option enabled.

Here is the whole source file, in case any other part of the program is causing problems:

#include <iostream> #include <queue> #include <vector> #include <unordered_set> #include <utility> #include <algorithm> #include <cstring> using namespace std; vector<unordered_set<int>> adj; static bool *visited; pair<int, int> dfs(int node) { if (visited[node]) return make_pair(0, node); pair<int, int> best = make_pair(0, node); for (int neigh : adj[node]) { pair<int, int> alt = dfs(node); alt.second++; best = max(best, alt); } return best; } int main(int argc, char** argv) { int n, m, def; cin >> n ; cin >> m; bool *pho=new bool[n]; memset(pho, 0, sizeof(bool) * n); int *degrees=new int[n]; memset(degrees, 0, sizeof(int) * n); cout << "n="<<n<<", m="<<m<<endl; for (int i = 0; i < m; i++) { int d=2; cout << "i=" << i << ", d="<<d<< endl; pho[d] = true; } for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; adj[a].insert(b); adj[b].insert(a); degrees[a]++; degrees[b]++; } queue<int> next; for (int i = 0; i < n; i++) { if (degrees[i] == 0) { next.push(i); } } while (!next.empty()) { int node = next.front(); next.pop(); if (pho[node]) continue; for (int neigh : adj[node]) { adj[node].erase(neigh); adj[neigh].erase(node); degrees[node]--; degrees[neigh]--; if (degrees[neigh] == 1) next.push(neigh); } } visited=new bool[n]; memset(visited, 0, sizeof(bool) * n); pair<int, int> pivot = dfs(def); memset(visited, 0, sizeof(bool) * n); pair<int, int> end = dfs(pivot.second); int dist = end.first; //number of edges she only has to walk once int tree = n - 1; //number of edges in tree int otherdist = tree - dist; //number of edges she has to walk twice int total = dist + otherdist * 2; cout << total << endl; return 0; }

最满意答案

这些行是错误的:

adj[a].insert(b); adj[b].insert(a);

您需要使用a和b作为键创建unordered_map实例,然后分别插入b和a作为值。 如果您需要键值对,则不需要有向量集。

These lines are wrong :

adj[a].insert(b); adj[b].insert(a);

You need to create unordered_map instance with a and b as keys, then respectively insert b and a as value. You don't need to have a vector of sets if you need key-value pairs.

更多推荐

本文发布于:2023-08-04 20:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422305.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   索引   错误   Segmentation   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!