Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PThreads和Malloc导致无法访问内存_C_Multithreading_Pthreads_Malloc - Fatal编程技术网

PThreads和Malloc导致无法访问内存

PThreads和Malloc导致无法访问内存,c,multithreading,pthreads,malloc,C,Multithreading,Pthreads,Malloc,我有一个哈希表,它利用线程插入元素。每个散列桶都是互斥锁的,线程可以在该桶中添加/遍历散列链。每次调用insert_word函数时,该函数都会生成一个线程并返回。然后线程负责搜索放置数据的位置,并在必要时分配内存 该表的目的是统计文本文档中单词的出现次数。该实现适用于较小的文件大小(~5kb),但当使用较大的文件(25kb)进行测试时,程序会出错 使用GDB,它显示程序在以下行出现故障: if(!strcmp(next->word, word)) 打印next显示它有一个地址(有时为0x

我有一个哈希表,它利用线程插入元素。每个散列桶都是互斥锁的,线程可以在该桶中添加/遍历散列链。每次调用insert_word函数时,该函数都会生成一个线程并返回。然后线程负责搜索放置数据的位置,并在必要时分配内存

该表的目的是统计文本文档中单词的出现次数。该实现适用于较小的文件大小(~5kb),但当使用较大的文件(25kb)进行测试时,程序会出错

使用GDB,它显示程序在以下行出现故障:

if(!strcmp(next->word, word))
打印next显示它有一个地址(有时为0x20,有时为0x530000007365,有时为0x64,有时为0x74656e),GDB表示无法访问next->word

此位置应在以下位置进行标记:

next = (struct hashEntry *)malloc(sizeof(struct hashEntry));
curr->next = next;
next->word = strdup(word);
next->count = 1;
似乎内存已经分配,数据在该地址被访问和利用,那么为什么突然无法访问它呢

编辑:应指定结构的内容

struct hashEntry {
    char *word;
    int count;
    struct hashEntry *next;
}

您没有初始化
next->next
,因此链表的最后一个元素在
next
中包含一些随机值。您应该隐式地将其设置为
NULL
,并检查
curr->next
是否为
NULL
,以检测链接列表的结尾。哇,这是我花了2小时调试的最简单的修复方法。谢谢@伊塞德夫:为什么不回答这个问题?