首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用单链表分割故障

用单链表分割故障
EN

Stack Overflow用户
提问于 2013-02-19 10:15:33
回答 1查看 156关注 0票数 0

好吧,我有个学校项目,我有个很大的问题。我已经花费了3-4个小时的调试时间,我不知道为什么会这样。

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

我认为这个算法是可行的,问题在于保存哈希表。在小测试中,程序工作正常,然后我进行了110个文件测试,它只是“分段错误”。

在对错误进行分段之前,我所知道的是:

  • 它在第9档出现错误
  • 它在哈希表中还不存在的单词上出错
  • 在搜索匹配时(在列表的末尾,在添加新值之前),它在传递所有列表后出错。

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

另外,标题:http://pastebin.com/H0m7WjrG

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

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

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

EN

回答 1

Stack Overflow用户

发布于 2013-02-19 12:36:18

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

我用您的代码作为一种快速的方法来隔离需要更仔细观察的代码。我发现只有第一个文件'date.in‘正在被读取。然后,我发现对put_doc的第二个调用没有返回。

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

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

我一会儿再好好看一看,我真的很喜欢刚才的电视!

编辑:

好吧,我得说这是件有趣的事。)我将为将来的程序使用该代码,该程序将搜索文件列表以获取某些文本。我的电视在午夜结束,我发现你的歌很难读,所以根据你给出的要求,我决定重新实现它。我删除了地图和散列函数,发现它们对于我理解的最终任务来说是不必要的。很可能(实际上有希望),这段代码在某种程度上不适合于提出的问题--它更像是一种不同方法的例子,希望它是一个代码的例子,其变量的命名更有意义。

我发现这些变量的名字确实妨碍了一个清晰的理解。这似乎也比需要的复杂一些。如果你有什么问题,我很乐意回答。

代码语言:javascript
复制
#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;
    }
}

输出:

代码语言:javascript
复制
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.
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14954956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档