C++ SIGSEGV 循环中的分段错误
我有以下代码,最终导致分段错误。
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 循环时,我不再收到此错误。但我不知道这个循环有什么问题。
感谢您的支持
I have the following code which ends up in a segmentation fault.
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();
}
When i remove the while-loop, i don't get this error anymore. But i don't know what's wrong with this loop.
Thx for your support
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
while 可能应该读
The while should probably read
这:
应该是:
This :
should be:
我不确定它是否与您收到的错误有关,但您似乎在第 7 行的
i++
上遇到了问题。即使i ,它也可能会增加 i == fileLines.size() - 1
,因此下一行的fileLines[i].find("message")
将访问不存在的项目。I'm not sure if it has anything to do with the error you're getting, but you seem to have a problem with the
i++
on line 7. It may increase i even wheni == fileLines.size() - 1
, sofileLines[i].find("message")
on the next line would access a non-existing item.