John
John

Reputation: 804

What is wrong with the following file writing code?

i have the following structure that i use to create a hash table for fingerprints

typedef struct fpinfo
{ 
   unsigned long chunk_offset;
   unsigned long chunk_length;
   unsigned char fing_print[33];
}fpinfo;

/* * The following defines one entry in the hash table. */

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;

I insert fingerprints into it using

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);
}

The Code I used to write the HT to file is

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);
}

my problem is (are)

1-it says "Access violation reading location 0xfdfdfe09." after writting a considerable number of times (it wrote 6401 fingerprints). It indicates the faulty line to be the fprintf() in the file writing function.

2- the fingerprints it writes and what i have before writing does not match at all. Actually the hex representation of the fingerprints in the compiler(i am using VC2010) and the one i have which is read by the program are different.

3- the values for chunck_length of all the entries are 3452816845l

Upvotes: 0

Views: 174

Answers (2)

Bartosz Moczulski
Bartosz Moczulski

Reputation: 1239

I guess the loop in WriteHTtoFile should look more like this:

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

Upvotes: 1

zvrba
zvrba

Reputation: 24574

You have more problems than that; this code is hopelessly botched

  • WriteHTToFile modifies the original hashtable, so you end up with memory leak at least
  • You use %d format to print out fing_print; it's not at all clear what fing_print is/should be (binary string; ascii string).

Get a good book on C, and get some practice with a debugger.

Upvotes: 0

Related Questions