g++的分段错误
Segmentation Fault with G++
我在程序的一部分中得到分段错误。我尝试了几种不同的方法来让它工作,但都没有成功。我用gdb调试它,得到:
程序收到信号SIGSEGV,分段错误。0x000000363c49d56e in
std::basic_string <char, std::char_traits <char>, std::allocator <char> >::assign(std::basic_string <char>, std::char_traits <char>,std::allocator <char> > const&) ()
from/usr/lib64/libstdc++.so.6
我不确定调试消息是什么意思,当我搜索时,我没有发现任何有用的东西。有人能告诉我为什么我得到这个错误,以及如何解决它?
下面是程序中出现错误的部分。
std::string name, gift, input, token, compare; std::string giant[50], separated[20], individuals[20], items[20]; int size, z = 0, x = 0, r =0;
cout << "****************Opening file to read list.****************" << endl << endl;
ifstream infile;
infile.open("input.txt");
while(!(infile.eof()))
{
for(size = 0; size < 20; size++)
{
getline(infile, giant[size]);
}
for(z = 0; z < 20; z++) //I believe this loop is the problem.
{
std::istringstream identity(giant[z]);
while(getline(identity, token, '"'))
{
separated[x] = token;
x++;
}
}
}
你很明显会超越separated
。
使用向量,而不是数组,和/或assert
为您在算法期间期望的容器大小。然后,您将能够更快地看到逻辑错误。
x
值不断增大。当x
达到20时,你需要做点什么
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 为什么我使用 std::copy() 收到运行时错误(分段错误)?
- 为什么我的代码包含错误分段错误(核心转储)?
- 为什么会出现此错误?- 分段错误(核心转储)
- 意外的运行时错误(分段错误)
- C++将文本文件读入数组,运行错误:分段错误
- mmap 错误:分段错误/指针无效错误
- 在我的代码中找不到错误....分段错误
- 错误:分段故障(核心转储)阵列
- 用string.h将错误分段到队列中
- c++字符串流转换错误:分段错误
- 为什么我的代码显示错误分段错误(核心转储)