我有以下结构,我使用来创建哈希表指纹以下文件编写代码有什么问题?
typedef struct fpinfo
{
unsigned long chunk_offset;
unsigned long chunk_length;
unsigned char fing_print[33];
}fpinfo;
/* * 下面定义在哈希表中的一个条目。 */
typedef struct Hash_Entry
{
struct Hash_Entry *next; /* Link entries within same bucket. */
unsigned namehash; /* hash value of key */
struct fpinfo fp;
} Hash_Entry;
typedef struct Hash_Table
{
struct Hash_Entry **bucketPtr; /* Buckets in the table */
int numBuckets;
int buck_entry_count[64];//number of entries in each bucket
int size; /* Actual size of array. */
int numEntries; /* Number of entries in the table. */
int mask; /* Used to select bits for hashing. */
} Hash_Table;
我使用
int Hash_CreateEntry(Hash_Table *t, struct Hash_Entry he)
{
Hash_Entry *e;
const char *p;
int keylen;
struct Hash_Entry **hp;
unsigned long h = 0, g,i=0;
while (i<5)
{
h = (h) + he.fp.fing_print[i]++;
g = h & 0xF0000000;
h ^= g >> 24;
h &= ~g;
i++;
}
p =(const char*) he.fp.fing_print;
for (e = t->bucketPtr[h & t->mask]; e != NULL; e = e->next)
{
if (e->namehash == h && strcmp((const char *)(e->fp).fing_print, p) == 0)
{
printf("\n%d \t%s",(e->fp).chunk_length,(e->fp).fing_print);
return (1);
}
}
if (t->numEntries >= rebuildLimit * t->size)
WriteHTtoFile(t);
e = (Hash_Entry *)malloc(sizeof(*e) /*+ keylen*/);
hp = &t->bucketPtr[h & t->mask];
e->next = *hp;
*hp = e;
e->namehash = h;
strcpy((char *)(e->fp).fing_print, p);
t->numEntries++;
t->buck_entry_count[h & t->mask]++;
return (0);
}
代码中,我以前写的HT到文件插入指纹到它是
static void WriteHTtoFile(Hash_Table *t)
{
Hash_Entry *e, *next = NULL, **hp, **xp;
int i=0, mask;
Hash_Entry **oldhp;
int oldsize;
FILE *htfile=fopen("htfile.txt","a");
system("cls");
for (hp = t->bucketPtr;t->bucketPtr!=NULL;hp=t->bucketPtr++)
{
for (e = *hp;e ->next!= NULL;e = e->next)
fprintf(htfile,"\n%d \t%s",(e->fp).chunk_length,(e->fp).fing_print);
}
fclose(htfile);
}
我的问题是(有)
1 - 它表示“访问冲突读取地址0xfdfdfe09”。写了很多次之后(写了6401个指纹)。它表示错误的行是文件写入函数中的fprintf()。
2-写入的指纹和写入前的指纹完全不匹配。实际上,编译器中的指纹十六进制表示(我正在使用VC2010)以及程序读取的指纹是不同的。
3-所有条目的chunck_length值是3452816845升
'T-> buck_entry_count [H&T->掩模] ++;'我不能看到T->掩模的任意位置初始化。考虑到“t-> buck_entry_count []”是固定大小(64)t->掩码应该至多为0x3f,这条线至少是可疑的。附加提示:为索引使用无符号类型,这样可以避免负面索引和错误,并且在大多数情况下,程序将以更快的速度失败,并且损坏更少。 – wildplasser 2012-02-25 12:24:52