我的程序的一部分出现段错误。我尝试了几种不同的方法来让它工作,但都没有成功。我用 gdb 调试它并得到:
Program received signal SIGSEGV, Segmentation fault. 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
。
使用 vector 而不是数组,和/或断言
用于您在算法中期望的容器大小。然后您将能够更快地发现逻辑错误。
关于c++ - G++ 的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22668819/