c++ - G++ 的段错误

标签 c++ string gdb g++

我的程序的一部分出现段错误。我尝试了几种不同的方法来让它工作,但都没有成功。我用 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/

相关文章:

c++ - 获取 vector c++中的字符串计数

c++ - QtCreator 调试器只显示汇编程序

c++ - 快速 GDB 问题;如果多个内存位置显示为,这意味着什么?

c++ - Linux 不尊重 SCHED_FIFO 优先级? (正常或 GDB 执行)

c++ - Windows I/O 驱动程序中的主动等待

c++ - 错误 : cannot convert 'const wchar_t [13]' to 'LPCSTR {aka const char*}' in assignment

C++继承,如何解决这个问题?

c++ - 使用 fstream 加载二进制文件

mysql - 在 Sql 查询中设置多个参数值

PHP 从字符串中删除域名