C 单链表的分段错误

C 单链表的分段错误,c,list,crash,malloc,C,List,Crash,Malloc,好吧,我有一个学校项目,我有一个相当大的问题。我已经花了3-4个小时的调试,我不知道为什么会发生这种情况 程序执行以下操作:它从文件“input.in”中读取以下内容:N(我的评论有点长,所以我将在这里发布。在图形调试器如此容易获得之前,我们使用了一种称为“墓碑调试”的技术。基本上,您只需在代码中添加一些printf语句,以确定程序执行最后到达的位置。 printf((%d)%s\n)、\uuuuuu-LINE\uuuuuu、\uuuuu-FILE\uuuuu);在这方面非常有用,顺便说一句 我

好吧,我有一个学校项目,我有一个相当大的问题。我已经花了3-4个小时的调试,我不知道为什么会发生这种情况


程序执行以下操作:它从文件“input.in”中读取以下内容:N(我的评论有点长,所以我将在这里发布。在图形调试器如此容易获得之前,我们使用了一种称为“墓碑调试”的技术。基本上,您只需在代码中添加一些printf语句,以确定程序执行最后到达的位置。
printf((%d)%s\n)、\uuuuuu-LINE\uuuuuu、\uuuuu-FILE\uuuuu);
在这方面非常有用,顺便说一句

我用你的代码这样做是为了快速隔离需要仔细查看的代码。我发现只有第一个文件“date.in”被读取。然后我发现第二次调用put_doc没有返回

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


当我用你的新函数更新代码时,代码开始读取所有的输入文件。然后我发现它在“
中崩溃了(i=0;我想没有人会阅读这么多代码并进行检查。您是否使用了调试器来检查可能出了什么问题?如果(并且只有当)我的电视节目结束后,我可能会倾向于在几个小时内看一看你还需要发布一份输入文件的副本。目前,我仅对此一点感到不安。另外,还有一件事——你的函数“int is_letter(char c)”可能会被“int isalpha(int c)”函数替换,因为“是的,很抱歉,我添加了输入文件。有人告诉我”问题是“在Map_t*Alocare_Mapare()以及在void Realocare_Mapare(Map_t*Map,int l)中,Map->bucket的新条目没有设置为NULL,因此可能包含一些随机位模式,这些位模式后来被解释为指针和解引用。“所以我改变了这一点:但我仍然存在分段错误。请参阅更新的答案。”(如果你已经收到更新通知,我深表歉意)哇,他欠你一块饼干什么的。真希望我能加倍投票支持你的工作。干杯,伙计!我感谢你的评论。只要不是基于浏览器的,我就接受它!(饼干,就是这样)因为我来这里不是为了钱,也不是为了一场pi$$ing竞赛,这都是关于学习和教学的。我很幸运地得到了这两个方面的支持。很高兴看到一位asm程序员同事在这里蹦蹦跳跳。-)s。谢谢你,哦,我的天啊:)我会立即着手解决它!Huuge谢谢。问题是我被迫用他们的标题工作(还有散列表和typedefs)。我想我真正的问题是,自从我了解了我的代码在哪里崩溃后,我该如何正确分配具有节点**成员的映射结构。我想可能是这样的:(-不确定。我在20多年的编码中从未使用过映射或散列表(嘘!),我不太熟悉它们的工作原理。正如我前面所说,整个程序中用于变量的命名约定使我很难阅读。不过,我确实注意到在函数“Realocare_Mapare”中,即重新分配内存后,继续清除其存储桶中的所有现有数据。这是有意的,还是该函数应确保新分配的内存不包含非空值?getDocs不会失败吗?
#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.