FIL*_*IaS 2 c hashtable list segmentation-fault chaining
尝试使用链接列表实现哈希表来解决冲突问题我正在面对我的代码初始化哈希表的一些问题.我遇到了分段错误.试着看看问题究竟在哪里我使用了valgrind.使用此工具,我收到警告:
"地址0x8没有堆叠,malloc'd或(最近)免费"
我几乎每次尝试"编辑"哈希表.例如,对于大小,插入,删除等我一次又一次地看了我的代码,但我找不到什么是错的.我以为我有malloc'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;
}
Run Code Online (Sandbox Code Playgroud)
更多说明:使用以下代码搜索哈希表中是否已存在数字.我从valgrind得到这个:
无效读取大小8 == 3773 ==在0x40110E:lookup(360)== 3773 ==地址0x8未堆叠,malloc'd或(最近)免费
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;
}
Run Code Online (Sandbox Code Playgroud)
事实上,如果我打电话来查看表格的大小,我也会得到一个细分,这让我更加担心.打电话给:
unsigned int size = Array[pos].TableHead->size;
Run Code Online (Sandbox Code Playgroud)
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.
Run Code Online (Sandbox Code Playgroud)
读这个我的第一个想法,我的号码没有一个空终止符.所以,我重新初始化它,并在其最后一个索引上添加了null.不幸的是,你看到的问题仍然存在.在第一次运行(查找函数)时,它将数字与列表进行比较; s数字为空.有分割.但我徘徊为什么.它不能只返回NULL吗?
谢谢.
blankTable->table[i]=malloc(sizeof(List));
blankTable->table[i]=NULL;
Run Code Online (Sandbox Code Playgroud)
您为List项分配内存,然后将其设置为NULL(0x0).
| 归档时间: |
|
| 查看次数: |
6340 次 |
| 最近记录: |