c++ 分段错误(核心转储)错误
c++ Segmentation fault (core dumped) error
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
string swapLastName(const string full_name, const string new_last_name)
{
string firstname;
string newname;
istringstream StrStream(full_name);
// seperate first name from full name
StrStream >> firstname;
// combines first name with new last name
newname=firstname +' '+ new_last_name;
// outputs new name
cout << "Your new name: " << newname << endl;
}
int main()
{
string full_name;
string new_last_name;
//input full name
cout << "Type your full name: ";
//getline to get entire full name
getline(cin, full_name);
//input new last name
cout << "Enter your new last name: ";
getline(cin, new_last_name);
swapLastName(full_name, new_last_name);
return 0;
}
对 c++ 有点陌生,需要一些帮助来解释为什么我不断收到分段错误(核心转储)错误。一切都可以正常工作,我想要它,但在它运行后我得到分段错误(核心转储)
你不从swapLastName
返回任何内容,但你给它一个返回类型string
。当控件到达函数的末尾时,它没有返回string
,因此它最终会得到一个string
大小的垃圾内存块。string
析构函数在临时字段被销毁时运行,并且由于所有内部字段都未初始化且毫无意义,因此它可能会在尝试释放某个随机地址时出现段错误。
相关文章:
- C++映射分割错误(核心转储)
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 在c++中键入向量中的所有值后,得到分段错误(核心转储)
- 分段错误(核心转储)但无法弄清楚
- 分段错误(核心转储) - 使用 SavedModel 的 Tensorflow C++ API 进行推断
- C++运行时错误与快速排序算法抛出堆栈转储错误
- C++指针无法在函数外部传递值和分段错误(核心转储)错误
- 合并排序:分段错误核心转储
- C++:分段错误(核心转储)
- 分段错误(内核转储) C++面向对象编程
- 从 pybind11 构建导入模块时出现"分段错误(核心转储)"
- qt C++命令行代码中的分段错误(核心转储)
- 从文件中读取的数据,但发生核心转储错误
- C++ atof - 分段错误(核心转储)
- 为什么我的代码包含错误分段错误(核心转储)?
- 运行编译的 a.out 后出现分段错误(核心转储)错误
- 为什么我的代码给我一个分段/核心转储错误?
- 有关矩阵的代码错误导致分段错误(内核转储)
- 双重释放或损坏(输出)和中止(核心转储)错误
- 运行我的代码时出现分段错误(核心转储)问题