【问题标题】:What is wrong with the following file writing code?以下文件编写代码有什么问题?
【发布时间】:2012-02-25 09:02:11
【问题描述】:

我使用以下结构为指纹创建哈希表

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的值为3452816845l

【问题讨论】:

  • t-&gt;buck_entry_count[h &amp; t-&gt;mask]++; 我看不到 t->mask 在任何地方初始化。鉴于 "t->buck_entry_count[]" 是固定大小 (64) t->mask 最多应该是 0x3f,这条线至少是值得怀疑的。附加提示:为您的索引使用无符号类型,这将避免负索引和错误,并且在大多数情况下,程序会更快地失败,并且损坏更少。

标签: c file linked-list access-violation


【解决方案1】:

我猜WriteHTtoFile 中的循环应该更像这样:

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

【讨论】:

    【解决方案2】:

    你的问题不止于此;这段代码被彻底搞砸了

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

    获得一本关于 C 的好书,并使用调试器进行一些练习。

    【讨论】:

    • 请看编辑,这是一个拼写错误,finger_print 是一个 ascii 字符串;但是你能告诉我 WriteHTToFile 在哪里修改表格吗?
    • 抱歉,我没有太多编程经验。你能告诉我如何更正它,这样它就不会修改原始数据吗?
    猜你喜欢
    • 1970-01-01
    • 2012-07-13
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多