我收到了这个奇怪的错误。只发生在Linux上,我需要让它在该操作系统上工作,但在Windows上工作得非常好。
仅在inFile.close();
上发生。已使用大量cout
语句对此进行了测试。
这是它遇到seg故障的函数。 (Element300是一个c样式字符串,有81个字符,包括终结符。读入的文件也是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
答案 0 :(得分:1)
循环:
while(strcmp(theTable[tempIndex].reserved,tempLine) != 0 && strcmp(theTable[tempIndex].reserved, "\0") != 0)
{
tempProbed++;
tempIndex += 3;
if(tempIndex > MAX_TABLE)
{
tempIndex = 0;
}
}
不知何故超出界限。我通过更改选中的条件并将if语句设置为>=
而不是>
来解决此问题。