【问题标题】:C++ SIGSEGV Segmentation fault in loop循环中的 C++ SIGSEGV 分段错误
【发布时间】:2011-08-21 16:00:09
【问题描述】:

我有以下代码,最终导致分段错误。

    for (int a=0; a<inputFileList.size(); a++)
    {
        fileLines = readFile(inputFileList[a].c_str());
        for (int i = 0; i < fileLines.size(); i++)
        {
            if (fileLines[i].find("text") != string::npos)
            {
                bool warnFound = false, errFound = false;
                i++;
                while (fileLines[i].find("message") == string::npos && i < fileLines.size())
                {
                    if (fileLines[i].find("error") != string::npos)
                        errFound = true;
                    else if (fileLines[i].find("warning") != string::npos)
                        warnFound = true;
                    i++;
                }
                i--;
                if (errFound)
                    errCtr++;
                else if (warnFound)
                    warnCtr++;
                else
                    okCtr++;
            }
        }
        fileLines.clear();
    }

当我删除 while 循环时,我不再收到此错误。但我不知道这个循环有什么问题。

感谢您的支持

【问题讨论】:

    标签: c++ file-io while-loop segmentation-fault


    【解决方案1】:

    我不确定这是否与您遇到的错误有关,但您似乎对第 7 行的 i++ 有问题。即使在 i == fileLines.size() - 1 时它也可能会增加 i,所以 @下一行的 987654323@ 将访问一个不存在的项目。

    【讨论】:

      【解决方案2】:

      这个:

        while (fileLines[i].find("message") == string::npos && i < fileLines.size())
      

      应该是:

        while (i < fileLines.size() && fileLines[i].find("message") == string::npos )
      

      【讨论】:

        【解决方案3】:

        while 应该是这样的

        while (i < fileLines.size() && fileLines[i].find("message") == string::npos)
        

        【讨论】:

          猜你喜欢
          • 2012-06-01
          • 2020-06-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-08-21
          • 1970-01-01
          • 2021-09-22
          • 2017-09-13
          相关资源
          最近更新 更多