在关闭文件时获取Segfault
Getting a Segfault on Close of a file
我收到了这个奇怪的错误。只在linux上发生,我需要让它在那个操作系统上运行,但在windows上运行得很好。
仅发生在inFile.close();
上。我们用大量的cout
语句测试了这一点。
这是它在seg出错的函数。(Element300是一个c风格的字符串,有81个字符,包括终止符。此外,正在读取的文件是JAVA关键字,所以没有超过80个字符。)
以下是功能:
void HT300::fill300()
{
Element300 readLine = " ";
Element300 tempLine = " ";
int hashedValue;
int tempIndex = 0;
int tempProbed = 0;
int tempLineIndex = 0;
int readLineIndex = 0;
ifstream inFile;
inFile.open("RWJAVA.DAT");
if(!inFile)
{
cerr << "File not found: RWJAVA.DAT" << endl;
}
else
{
while(!inFile.getline(readLine,MAX_SIZE,'n').eof())
{
while(readLine[readLineIndex] == ' ')
{
readLineIndex++;
}
while(isalnum(readLine[readLineIndex]))
{
tempLine[tempLineIndex] = readLine[readLineIndex];
tempLineIndex++;
readLineIndex++;
}
tempLine[tempLineIndex] = ' ';
hashedValue = hash300(tempLine);
tempIndex = hashedValue;
while(strcmp(theTable[tempIndex].reserved,tempLine) != 0 && strcmp(theTable[tempIndex].reserved, " ") != 0)
{
tempProbed++;
tempIndex += 3;
if(tempIndex > MAX_TABLE)
{
tempIndex = 0;
}
}
if(strcmp(theTable[tempIndex].reserved," ") == 0)
{
strcpy(theTable[tempIndex].reserved,tempLine);
theTable[tempIndex].probed = tempProbed;
theTable[tempIndex].hashed = hashedValue;
}
tempIndex = 0;
tempProbed = 0;
tempLineIndex = 0;
readLineIndex = 0;
strcpy(tempLine, " ");
}
}
inFile.close();
return;
}
inWINDOWS上的文件错误:
Multiple errors reported.
1) Failed to execute MI command:
-var-create - * inFile
Error message from debugger back end:
-var-create: unable to create variable object
2) Failed to execute MI command:
-var-create - * inFile
Error message from debugger back end:
-var-create: unable to create variable object
3) Failed to execute MI command:
-data-evaluate-expression inFile
Error message from debugger back end:
No symbol "inFile" in current context.
4) Failed to execute MI command:
-var-create - * inFile
Error message from debugger back end:
-var-create: unable to create variable object
5) Unable to create variable object
循环:
while(strcmp(theTable[tempIndex].reserved,tempLine) != 0 && strcmp(theTable[tempIndex].reserved, " ") != 0)
{
tempProbed++;
tempIndex += 3;
if(tempIndex > MAX_TABLE)
{
tempIndex = 0;
}
}
不知怎么就越界了。我通过更改选中的条件并将if语句设置为>=
而不是>
来解决此问题。
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 使用指针从C++中的数组中获取最大值
- 如何获取std::result_of函数的返回类型
- 如何在openssl-ecc中获取十六进制格式的私钥
- 使用Unreal C++获取VR耳机的世界位置/方向
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- C++中带有List类的迭代器Segfault
- 从C字符串中获取奇怪的字符串长度
- 为什么我的for循环不能正确获取argv
- 从python中调用C++函数并获取返回值
- 如何获取一个数字的前3位
- 从引用的Vector获取SEGFAULT
- 在数据流处理程序中获取segfault
- 获取用户存储segfault错误
- 在关闭文件时获取Segfault
- 通过多个函数传递指针并获取segfault
- 尝试从命令行获取输入时出现char**argv[]segfault
- c++:初始化指针队列时获取segfault