【发布时间】:2015-11-22 04:22:10
【问题描述】:
我收到了这个奇怪的错误。只发生在我需要让它在那个操作系统上工作的 linux 上,但在 windows 上工作得很好。
仅在 inFile.close(); 上发生 已使用大量 cout 语句对此进行了测试。
这是出现段错误的函数。 (Element300 是一个包含终止符在内的 81 个字符的 c 样式字符串。另外,正在读取的文件是 JAVA 关键字,所以没有超过 80 个字符。)
函数如下:
void HT300::fill300()
{
Element300 readLine = "\0";
Element300 tempLine = "\0";
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] = '\0';
hashedValue = hash300(tempLine);
tempIndex = hashedValue;
while(strcmp(theTable[tempIndex].reserved,tempLine) != 0 && strcmp(theTable[tempIndex].reserved, "\0") != 0)
{
tempProbed++;
tempIndex += 3;
if(tempIndex > MAX_TABLE)
{
tempIndex = 0;
}
}
if(strcmp(theTable[tempIndex].reserved,"\0") == 0)
{
strcpy(theTable[tempIndex].reserved,tempLine);
theTable[tempIndex].probed = tempProbed;
theTable[tempIndex].hashed = hashedValue;
}
tempIndex = 0;
tempProbed = 0;
tempLineIndex = 0;
readLineIndex = 0;
strcpy(tempLine, "\0");
}
}
inFile.close();
return;
}
WINDOWS 上的 inFile 错误:
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
【问题讨论】:
-
我还没有阅读您的代码,但您可能在数组之外编写代码并破坏了
infile的数据结构,或者破坏了ifstream inFile对象本身。使用调试器,在inFile的值上设置观察点。如果它在不应该触及它的代码行发生变化(例如,在写入数组的行),那么您已经找到了错误。 (或者至少是后果。实际的错误可能更早,并且违反了后来代码所做的假设。) -
此问题是否与系统相关?就像我在原始问题中所说的那样:适用于 Windows 10、Eclipse IDE,但不适用于 Linux。 @PeterCordes
-
close语句不一定会出现问题。它可能发生在您的内存引用超出分配区域的其他地方。Element300是什么?数据是否被正确读取? -
@unxnut
Element300是一个 c 风格的字符串,最多 80 个字符。就像我说的,我的程序中的这个功能适用于 Windows,但不适用于 Linux。 -
看起来你运行了调试器,但确保你有 Eclipse 构建调试负载(右键单击项目。选择 Build Configuration->Set Active->Debug)