2012-02-25 46 views
0

我有以下结构,我使用来创建哈希表指纹以下文件编写代码有什么问题?

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升

+0

'T-> buck_entry_count [H&T->掩模] ++;'我不能看到T->掩模的任意位置初始化。考虑到“t-> buck_entry_count []”是固定大小(64)t->掩码应该至多为0x3f,这条线至少是可疑的。附加提示:为索引使用无符号类型,这样可以避免负面索引和错误,并且在大多数情况下,程序将以更快的速度失败,并且损坏更少。 – wildplasser 2012-02-25 12:24:52

回答

0

你比更多的问题;这段代码是无可救药的拙劣

  • WriteHTToFile修改原始哈希表,所以你最终的内存泄漏至少
  • 您使用%d格式打印出来fing_print;它完全不清楚fing_print是/应该是什么(二进制字符串; ascii字符串)。

获得一本关于C的好书,并通过调试器获得一些练习。

+0

请参阅编辑,这是一个错字错误,finger_print是一个ascii字符串;但是可以告诉我请WriteHTToFile修改表的位置? – John 2012-02-25 11:32:30

+0

for()'语句的“update”部分。 – zvrba 2012-02-25 11:37:02

+0

对不起,我没有太多的编程经验。你能告诉我如何纠正它,以便它不会修改原始数据? – John 2012-02-25 11:38:53

1

我想在WriteHTtoFile该循环应该看起来更是这样的:

for (i = 0; i < t->numBuckets; ++i) 
{ 
    for (e = t->bucketPtr[i]; e && e->next; e = e->next) 
     fprintf(htfile, /*...*/);   
}