2012-02-25 89 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, /*...*/);   
}