尝试使用链表实现哈希表来解决冲突问题我在初始化哈希表的代码中遇到了一些问题。我得到一个分段错误。试图找出问题出在哪里,我使用了 valgrind。使用此工具,我收到警告:
“地址 0x8 未堆叠、malloc 或(最近)释放”
几乎每次我尝试“编辑”哈希表。例如尺寸,插入某物,删除等。我一遍又一遍地查看我的代码,但我找不到问题所在。我以为我已经正确地 malloc'd 和 stack'd 一切。但是有了这个消息,显然某事出了问题。对此有什么想法吗?
我的代码:
//hash table structure
typedef struct HashTable
{
int size; //size of table with connections
struct List **table; //table elements
}HashTable;
typedef struct List
{
char* number;
struct List *next;
}List;
struct HashTable *initHashTable(int size)
{
struct HashTable *blankTable=(struct HashTable *)malloc(sizeof(struct HashTable));
if (size<1)
{
return NULL;
}
if ((blankTable=malloc(sizeof(HashTable)))==NULL)
{
return NULL;
}
if ( (blankTable->table=malloc(size*sizeof(List))) == NULL)
{
return NULL;
}
int i;
for (i=0; i<size; i++) //initializes hash table
{
blankTable->table[i]=malloc(sizeof(List));
blankTable->table[i]=NULL; //Valgrind :: Invalid write of size 8
}
blankTable->size = size;
//printf("blankTable size:%d\n",blankTable->size);
return blankTable;
}
更多注意事项:使用以下代码搜索哈希表中是否已存在数字。我从 valgrind 得到这个:
在 0x40110E 处读取大小为 8 ==3773== 的无效:lookup(360) ==3773== 地址 0x8 未堆栈、malloc 或(最近)释放
struct List *lookup(HashTable *hashtable,char *number)
{
struct List *list= (struct List *) malloc (sizeof(struct List )); ;
unsigned int hashval= hash(number);
if ( (hashtable->table[hashval])!=NULL)
{
for( list=hashtable->table[hashval]; list!=NULL; list=list->next)
{ if(strcmp(number,list->number)==0) //SEGMENTATION!
{
return list;
}
}
}
return NULL;
}
事实上,如果我打电话查看桌子的大小,我也会得到一个分段,这让我更加担心。调用这个:
unsigned int size = Array[pos].TableHead->size;
Array[pos].TableHead 是指向 hashTable 结构的指针。
编辑:
运行 valgring 我得到这个报告:
Invalid write of size 8
==8724== at 0x4016D2: initHashTable (hash.c:524)
==8724== by 0x4019CE: main (hash.c:792)
==8724== Address 0x5199180 is 8 bytes after a block of size 8 alloc'd
==8724== at 0x4C25153: malloc (vg_replace_malloc.c:195)
==8724== by 0x4016B6: initHashTable (hash.c:522)
==8724== by 0x4019CE: main (hash.c:792)
==8724== Use of uninitialised value of size 8
==8724== at 0x4C264C4: strcmp (mc_replace_strmem.c:412)
==8724== by 0x4017A0: lookup (hash.c:551)
==8724== by 0x401820: add(hash.c:566)
==8724== by 0x401AAB: main (hash.c:817)
==8724==
==8724== Invalid read of size 1
==8724== at 0x4C264C4: strcmp (mc_replace_strmem.c:412)
==8724== by 0x4017A0: lookup (hash.c:551)
==8724== by 0x401820: add (hash.c:566)
==8724== by 0x401AAB: main (hash.c:817)
==8724== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==8724==
==8724==
==8724== Process terminating with default action of signal 11 (SIGSEGV)
==8724== Access not within mapped region at address 0x0
==8724== at 0x4C264C4: strcmp (mc_replace_strmem.c:412)
==8724== by 0x4017A0: lookup (hash.c:551)
==8724== by 0x401820: add (hash.c:566)
==8724== by 0x401AAB: main (hash.c:817)
==8724== If you believe this happened as a result of a stack
==8724== overflow in your program's main thread (unlikely but
==8724== possible), you can try to increase the size of the
==8724== main thread stack using the --main-stacksize= flag.
==8724== The main thread stack size used in this run was 8388608.
读到这篇文章,我首先想到我的号码没有空终止符。所以,我重新初始化它,并在它的最后一个索引上添加了null。不幸的是,您看到的问题仍然存在。在第一次运行(查找函数)时,它将数字与列表的数字进行比较,该数字为空。有细分。但我在徘徊为什么。它不能只返回 NULL 吗?
谢谢你。