C++ 中的分段错误(核心转储) 找不到错误
Segmentation fault (core dumped) in c++ Can't find error
我似乎找不到错误的来源。该代码定义了一个名为boolfunc的结构。我定义了一个函数来添加两个boolfunc。我得到了错误:分割错误(核心转储)。我知道这意味着什么,但错误的根源在哪里。
#include<iostream>
#include<vector>
using namespace std;
class boolfunc {
public:
int num_var;
int num_terms;
vector<vector<int> > func;
boolfunc addfunc(boolfunc f1, boolfunc f2);
};
boolfunc addfunc(boolfunc f1, boolfunc f2)
{
boolfunc f3;
f3.func.resize(0);
f3.num_terms = f1.num_terms + f2.num_terms;
int var_dif = f1.num_var - f2.num_var;
if(var_dif >= 0)
{
f3.num_var=f1.num_var;
f3.func.resize(f1.num_var);
f2.num_var=f1.num_var;
f2.func.resize(f1.num_var);
for(int i=0; i<f2.num_terms; ++i)
{
for(int j=0; j<var_dif; ++j)
{
f2.func[i].push_back(-1);
}
}
}
else
{
f3.num_var=f2.num_var;
f3.func.resize(f2.num_var);
f1.num_var=f2.num_var;
f1.func.resize(f2.num_var);
for(int i=0; i<f1.num_terms; ++i)
{
for(int j=0; j<(-1)*(var_dif); ++j)
{
f1.func[i].push_back(-1);
}
}
}
for(int i=0; i<f1.num_terms; ++i)
{
f3.func[i]=f1.func[i];
}
for(int j=0; j<f2.num_terms;++j)
{
f3.func[j+f1.num_terms]=f2.func[j];
}
return f3;
}
int main()
{
boolfunc ef1, ef2, ef3;
ef1.func.resize(0);
ef2.func.resize(0);
ef3.func.resize(0);
cout<< "Input the number of variables of function 1" <<endl;
cin>> ef1.num_var;
cout<< "Input the number of variables of function 2" <<endl;
cin>> ef2.num_var;
cout<< "Input the number of terms of function 1" <<endl;
cin>> ef1.num_terms;
cout<< "Input the number of terms of function 2" <<endl;
cin>> ef2.num_terms;
ef1.func.resize(ef1.num_terms);
for(int i=0; i<ef1.num_terms; ++i)
{
ef1.func[i].resize(ef1.num_var);
for(int j=0; j<ef1.num_var; ++j)
{
cout<<"For function 1, Input the term "<<i+1<<"'s variable "<<j+1<<"'s value:";
cin>>ef1.func[i][j];
}
}
ef2.func.resize(ef2.num_terms);
for(int i=0; i<ef2.num_terms; ++i)
{
ef2.func[i].resize(ef2.num_var);
for(int j=0; j<ef2.num_var; ++j)
{
cout<<"For function 2, Input the term "<<i+1<<"'s variable "<<j+1<<"'s value:";
cin>>ef2.func[i][j];
}
}
ef3 = addfunc(ef1, ef2);
for(int i=0; i<ef3.num_terms; ++i)
{
for(int j=0; j<ef3.num_var; ++j)
{
cout<<ef3.func[i][j]<<' ';
if(j==ef3.num_var -1) cout<<endl;
}
}
return 0;
}
如何处理:
构建并运行
g++ -Wall -Wextra -O0 -ggdb main.cpp
gdb --args ./a.out
使用示例矢量(0,0,1,0),您的程序在第49行失败:
f3.func[i]=f1.func[i];
错误:
Input the number of variables of function 1
0
Input the number of variables of function 2
0
Input the number of terms of function 1
1
Input the number of terms of function 2
0
Program received signal SIGSEGV, Segmentation fault.
0x0000000000402930 in std::vector<int, std::allocator<int> >::capacity (this=0x0) at /usr/include/c++/4.6/bits/stl_vector.h:652
652 - this->_M_impl._M_start); }
(gdb) bt
#0 0x0000000000402930 in std::vector<int, std::allocator<int> >::capacity (this=0x0) at /usr/include/c++/4.6/bits/stl_vector.h:652
#1 0x0000000000401bf3 in std::vector<int, std::allocator<int> >::operator= (this=0x0, __x=...) at /usr/include/c++/4.6/bits/vector.tcc:164
#2 0x0000000000400fc3 in addfunc (f1=..., f2=...) at main.cpp:49
#3 0x0000000000401558 in main () at main.cpp:92
分析
如果您查看程序结束和标准库开始的边界,我们会立即看到错误:
std::vector<int, std::allocator<int> >::operator= (this=0x0, __x=...) at /usr/include/c++/4.6/bits/vector.tcc:164
正如您所看到的,NULL
作为this
参数传递给std::vector<..>::operator=
,这意味着您的f3.func
向量的大小错误。
最简单的一个:使用valgrind。
像这样:http://codepad.org/uSfdSK5d
更好的提问形式可能提供了更详细的答案。
相关文章:
- C++映射分割错误(核心转储)
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 分段错误(核心转储)但无法弄清楚
- 分段错误(核心转储) - 使用 SavedModel 的 Tensorflow C++ API 进行推断
- C++运行时错误与快速排序算法抛出堆栈转储错误
- C++指针无法在函数外部传递值和分段错误(核心转储)错误
- 合并排序:分段错误核心转储
- C++:分段错误(核心转储)
- 分段错误(内核转储) C++面向对象编程
- 从 pybind11 构建导入模块时出现"分段错误(核心转储)"
- qt C++命令行代码中的分段错误(核心转储)
- 从文件中读取的数据,但发生核心转储错误
- C++ atof - 分段错误(核心转储)
- 为什么我的代码包含错误分段错误(核心转储)?
- 运行编译的 a.out 后出现分段错误(核心转储)错误
- 为什么我的代码给我一个分段/核心转储错误?
- 有关矩阵的代码错误导致分段错误(内核转储)
- 双重释放或损坏(输出)和中止(核心转储)错误
- 运行我的代码时出现分段错误(核心转储)问题