在关闭文件时获取Segfault



我收到了这个奇怪的错误。只在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语句设置为>=而不是>来解决此问题。

最新更新