单链表的分段错误

时间:2013-02-19 10:15:33

标签: c list crash malloc

好的,所以我有这个学校项目,我有一个非常大的问题。我花了3-4个小时进行调试,但我无法找出它发生的原因。

程序执行此操作:它从文件“input.in”读取以下内容:N(< 500),然后是N个文件名,M(< 500)然后M“查询”。 查询是这样的一行:“John& Papa | Dan”,它将返回文件索引,其中包含“John和Papa”或“Dan”。

我认为该算法有效,问题在于保存哈希表。在小测试中,程序运行正常,然后我有一个110文件测试,它只是“分段错误”。

到目前为止我所知道的分段错误:

  • 第9个文件出错
  • 它在哈希表中尚不存在的单词出错
  • 在搜索匹配项后通过所有列表时出错(在列表末尾,就在添加新值之前)

以下是代码:http://pastebin.com/fd4c1f6w

此外,标题为:http://pastebin.com/H0m7WjrG

以下是调试信息:http://pastebin.com/gvvyjePZ

输入文件:http://www.sendspace.com/file/48etji

拜托,我真的需要解决这个问题,我真的很失望。

1 个答案:

答案 0 :(得分:2)

我的评论有点长,所以我会在这里发布。回到图形调试器之前,我们使用了一种称为“墓碑调试”的技术。基本上,您只需在代码中添加一些printf语句,以确定哪个是程序执行的最后位置。 printf("(%d) %s\n", __LINE__, __FILE__);对此非常有用,顺便说一句。

我用你的代码做了这个,作为隔离需要更密切关注的代码的快速方法。我发现只有第一个文件'date.in'正在被阅读。然后我确定第二次调用put_doc没有返回。

然后我意识到你已经发布了Alocare_Mapare和Realocare_Mapare的更新代码。 :脸掌:

当我使用新函数更新代码时,代码继续读取所有输入文件。然后我发现它在“for(i=0;i<nrTokeni;i++)”循环中崩溃了。

我会稍微好一点,我现在真的很喜欢电视! :P

修改

那么,我必须说这很有趣。 :) 我会将代码用于将来搜索某些文本文件列表的程序。 我的电视在午夜结束,我发现你的颂歌难以阅读,所以根据你提出的要求的描述,我决定重新实施它。我取消了地图和哈希函数,发现它们对于我理解的最终任务是不必要的。可能(实际上有希望)代码有点不适合提出的问题 - 它更像是一个不同方法的例子,并且希望一个代码示例,其中的变量具有更有意义的名称。

我发现变量名称确实妨碍了清晰的理解。它似乎也比需要复杂一点。如果您有任何问题,我很乐意回答。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node_t
{
    char *data;
    node_t *next;
};

node_t *makeNewNode(char *newData)
{
    node_t *tmp;
    tmp = (node_t *)malloc(sizeof(node_t));
    tmp->data = strdup(newData);
    tmp->next = NULL;
    return tmp;
}

void addNode(node_t **listHead, char *newData)
{
    node_t *theNewNode = makeNewNode(newData);

    if (*listHead == NULL)
        *listHead = theNewNode;

    else
    {
        node_t *curNode = *listHead;
        while (curNode->next != NULL)
            curNode = curNode->next;
        curNode->next = theNewNode;
    }
}

void printList(node_t *nodeList)
{
    node_t *curNode = nodeList;
    while (curNode != NULL)
    {
        printf("%s\n", curNode->data);
        curNode = curNode->next;
    }
}

void readMainInputFile(char *filename, node_t **filesToProcessOut, node_t **searchTermsOut)
{
    int numFiles, numSearchTerms;
    int curFileNum, curSearchTerm;
    const int maxLineLength = 1024;
    char lineBuffer[maxLineLength+1], *endlStrippedFileName, *endlStrippedSearchTerms;

    FILE *fileHandle = fopen(filename, "rt");

    fscanf(fileHandle, "%d\n", &numFiles);
    for (curFileNum=0; curFileNum<numFiles; curFileNum++)
    {
        fgets(lineBuffer, maxLineLength, fileHandle);
        endlStrippedFileName = strtok(lineBuffer, "\r\n");
        addNode(filesToProcessOut, endlStrippedFileName);
    }

    fscanf(fileHandle, "%d\n", &numSearchTerms);
    for (curSearchTerm=0; curSearchTerm<numSearchTerms; curSearchTerm++)
    {
        fgets(lineBuffer, maxLineLength, fileHandle);
        endlStrippedSearchTerms = strtok(lineBuffer, "\r\n");
        addNode(searchTermsOut, endlStrippedSearchTerms);
    }
    fclose(fileHandle);
    printf("Read %d files, %d search terms\n", numFiles, numSearchTerms);
}

int fileLen(FILE *fp)
{
    int result, curPos;
    curPos = ftell(fp);
    fseek(fp, 0, SEEK_END);
    result = ftell(fp);
    fseek(fp, curPos, SEEK_SET);
    return result;
}

// searhes a file for any of the strings (seperated by | character) found in a single line from the inupt file.
// this is wasteful - we open load and search the file one time for each of the searchTerms.
// I.e - the InputData below would cause the file to be opened and read 4 times. Ideally, it should only be opened and read once
//  we could fix this by passing a linked list of all of the lines of search terms - I'm too lazy. :-P
//11
//doctor & having
//I & hero | life
//innocently | that | know & will & it & I & yet
//shall & turn & out & to & be

bool doesFileContainSearchTerms(char *filename, char *searchTerms)
{
    int fLen;
    bool result;
    char *buffer;
    char *searchTermsCopy = strdup(searchTerms);
    char *curToken, curSearchTerm[100];
    bool spaceAtStart, spaceAtEnd;

    // open file, get length, allocate space for length+1 bytes, zero that memory, read the file
    FILE *fileHandle = fopen(filename, "rt");
    fLen = fileLen(fileHandle);
    buffer = (char*) calloc(1, fLen+1);
    fread(buffer, fLen, 1, fileHandle);
    fclose(fileHandle);

    curToken = strtok(searchTermsCopy, "|");
    while ((curToken != NULL) && (strlen(curToken)!=0))
    {
        memset(curSearchTerm, 0, 100);

        // strip the leading/trailing spaces (and '|' char) from a search term
        // e.g
        //  "I & hero |" --> "I & hero"
        //  " life" --> "life"
        spaceAtStart = spaceAtEnd = false;
        if ((curToken[0] == ' ') || (curToken[0] == '|'))
            spaceAtStart = true;
        if (curToken[strlen(curToken)-1] == ' ')
            spaceAtEnd = true;

        if ((spaceAtStart==false) && (spaceAtEnd==false))
            strcpy(curSearchTerm, curToken);
        else if ((spaceAtStart==false) && (spaceAtEnd==true))
            strncpy(curSearchTerm, curToken, strlen(curToken)-1);
        else if ((spaceAtStart==true) && (spaceAtEnd==false))
            strncpy(curSearchTerm, curToken+1, strlen(curToken)-1);
        else if ((spaceAtStart==true) && (spaceAtEnd==true))
            strncpy(curSearchTerm, curToken+1, strlen(curToken)-2);

   //     printf("CurSearchTerm: ''%s''\n", curSearchTerm);

        // we're searching for _any_ of the text in the search term, e.g "I & hero | life"
        // if we find one of them, then set result to true and stop looking.
        result = false;
        if (strstr(buffer, curSearchTerm) != NULL)
        {
            result = true;
            break;
        }
        // didn't find one of the searchTerms yet, grab the next one
        curToken = strtok(NULL, "|");
    }

    free(buffer);
    free(searchTermsCopy);
    return result;
}

int main()
{
    node_t *inputFileList = NULL;
    node_t *searchTermList = NULL;

    readMainInputFile("input.in", &inputFileList, &searchTermList);

    node_t *curFileNameNode = inputFileList;
    while (curFileNameNode != NULL)
    {
        node_t *curSearchTermNode = searchTermList;
        while (curSearchTermNode != NULL)
        {
           // printf("Searching %s for %s\n", curFileNameNode->data, curSearchTermNode->data);
            if (doesFileContainSearchTerms(curFileNameNode->data, curSearchTermNode->data))
                printf("Search hit - file(%s), searchTerm(%s)\n", curFileNameNode->data, curSearchTermNode->data);
            curSearchTermNode = curSearchTermNode->next;
        }
        curFileNameNode = curFileNameNode->next;
    }
}

输出:

Read 110 files, 11 search terms
Search hit - file(date.in), searchTerm(I & hero | life)
Search hit - file(date.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date2.in), searchTerm(I & hero | life)
Search hit - file(date2.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date3.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date4.in), searchTerm(I & hero | life)
Search hit - file(date4.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date6.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date6.in), searchTerm(looking | fire | called & another)
Search hit - file(date7.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date8.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date9.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date10.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date10.in), searchTerm(looking | fire | called & another)
Search hit - file(date11.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date12.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date13.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date14.in), searchTerm(looking | fire | called & another)
Search hit - file(date18.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date20.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date20.in), searchTerm(looking | fire | called & another)
Search hit - file(date23.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date25.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date25.in), searchTerm(looking | fire | called & another)
Search hit - file(date28.in), searchTerm(I & hero | life)
Search hit - file(date29.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date30.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date37.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date38.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date38.in), searchTerm(looking | fire | called & another)
Search hit - file(date44.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date45.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date47.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date50.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date52.in), searchTerm(I & hero | life)
Search hit - file(date52.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date53.in), searchTerm(looking | fire | called & another)
Search hit - file(date61.in), searchTerm(looking | fire | called & another)
Search hit - file(date68.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date75.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date76.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date76.in), searchTerm(looking | fire | called & another)
Search hit - file(date77.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date78.in), searchTerm(looking | fire | called & another)
Search hit - file(date81.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date84.in), searchTerm(looking | fire | called & another)
Search hit - file(date88.in), searchTerm(looking | fire | called & another)
Search hit - file(date89.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date91.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date92.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date100.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date102.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date110.in), searchTerm(innocently | that | know & will & it & I & yet)

Process returned 0 (0x0)   execution time : 0.308 s
Press any key to continue.