Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
while循环中的分段故障 我有一个程序,从C++中输入2个文件。然后确定第二个输入文件是拓扑排序?但是如果我在while循环语句中使用list.empty,它会给我一个分段错误,但是for循环不会给我任何错误;但是,for循环只循环一次,因为我可能需要执行两次 #include <list> #include <iostream> #include <vector> using namespace std ; list<unsigned> output; list<unsigned> & testSort ( istream & idata , istream & sdata ) { unsigned n,x1,x2; vector< list<unsigned> > successor(n); vector<unsigned> count(n,0); vector<bool> marks(n,false); idata >>n; for(int i=0;i<n;i++) { idata>>x1>>x2; count[x2]++; successor[x1].push_back(x2); if(idata.eof()) break; } for(int i=0;i<n;i++) { sdata>>x1; if(count[x1]==0) { marks[x1]=true; //for(int j=0;j<successor[x1].size();++j) { while(!successor[x1].empty()) { count[successor[x1].front()]--; successor[x1].pop_front(); } } else { for(int i=0;i<n;i++) { if(marks[i]==false) output.push_back(i); } break; } } return output; }_C++ - Fatal编程技术网

while循环中的分段故障 我有一个程序,从C++中输入2个文件。然后确定第二个输入文件是拓扑排序?但是如果我在while循环语句中使用list.empty,它会给我一个分段错误,但是for循环不会给我任何错误;但是,for循环只循环一次,因为我可能需要执行两次 #include <list> #include <iostream> #include <vector> using namespace std ; list<unsigned> output; list<unsigned> & testSort ( istream & idata , istream & sdata ) { unsigned n,x1,x2; vector< list<unsigned> > successor(n); vector<unsigned> count(n,0); vector<bool> marks(n,false); idata >>n; for(int i=0;i<n;i++) { idata>>x1>>x2; count[x2]++; successor[x1].push_back(x2); if(idata.eof()) break; } for(int i=0;i<n;i++) { sdata>>x1; if(count[x1]==0) { marks[x1]=true; //for(int j=0;j<successor[x1].size();++j) { while(!successor[x1].empty()) { count[successor[x1].front()]--; successor[x1].pop_front(); } } else { for(int i=0;i<n;i++) { if(marks[i]==false) output.push_back(i); } break; } } return output; }

while循环中的分段故障 我有一个程序,从C++中输入2个文件。然后确定第二个输入文件是拓扑排序?但是如果我在while循环语句中使用list.empty,它会给我一个分段错误,但是for循环不会给我任何错误;但是,for循环只循环一次,因为我可能需要执行两次 #include <list> #include <iostream> #include <vector> using namespace std ; list<unsigned> output; list<unsigned> & testSort ( istream & idata , istream & sdata ) { unsigned n,x1,x2; vector< list<unsigned> > successor(n); vector<unsigned> count(n,0); vector<bool> marks(n,false); idata >>n; for(int i=0;i<n;i++) { idata>>x1>>x2; count[x2]++; successor[x1].push_back(x2); if(idata.eof()) break; } for(int i=0;i<n;i++) { sdata>>x1; if(count[x1]==0) { marks[x1]=true; //for(int j=0;j<successor[x1].size();++j) { while(!successor[x1].empty()) { count[successor[x1].front()]--; successor[x1].pop_front(); } } else { for(int i=0;i<n;i++) { if(marks[i]==false) output.push_back(i); } break; } } return output; },c++,C++,读入x1和x2的值是多少?如果值大于n,则对向量元素的访问无效:计数[x2],标记[x1]和后续[x1]表示无效元素 使用at函数代替下标符号[],该函数执行边界检查以捕获第一个无效访问 unsigned n,x1,x2; vector< list<unsigned> > successor(n); 这是一个明显的错误-您正在使用n来指定继任者的大小,但n尚未初始化,因此它包含垃圾,计数和标记也是如此,因为您已经将它们的大小指定为n。换句话说,在这一点上,我们不知道继任

读入x1和x2的值是多少?如果值大于n,则对向量元素的访问无效:计数[x2],标记[x1]和后续[x1]表示无效元素

使用at函数代替下标符号[],该函数执行边界检查以捕获第一个无效访问

unsigned n,x1,x2;
vector< list<unsigned> > successor(n);
这是一个明显的错误-您正在使用n来指定继任者的大小,但n尚未初始化,因此它包含垃圾,计数和标记也是如此,因为您已经将它们的大小指定为n。换句话说,在这一点上,我们不知道继任者的规模

你有两个选择。你可以移动你的idata>>n;在定义后继项、计数和标记之前,或者可以定义它们而不指定大小,然后在从idata中读取n后使用resize指定它们的大小


除了指出我很少发现它是一个最佳选择之外,我不再重复我通常对std::list的咆哮。

当出现分段错误时,你应该做的第一件事是在调试器中运行你的程序。这不仅可以帮助您精确定位崩溃的确切位置,还可以让您检查变量以了解可能导致崩溃的原因。