i believe my code may not be allocating memory properly this code is for the construction of a hash table for a school project
typedef struct {
char * word;
char * defn;
} entry;
typedef struct {
int table_maxsize;
int table_cursize;
int table_rehash;
int table_delta_capacity;
entry** data;
int* flag;
} hashTable;
typedef hashTable * Dictionary;
this is the create function that i believe is not working correctly
Dictionary create(int initial_capacity, int delta_capacity){
/* returns NULL on failure
* Side Effects: allocates storage.*/
Dictionary table = NULL;
table = malloc(sizeof(hashTable));
//int i = 0;
table->data = malloc( sizeof( entry* ) * initial_capacity);
table->table_maxsize = initial_capacity;
table->table_cursize = 0;
table->table_delta_capacity = delta_capacity;
table->table_rehash = ((initial_capacity * 0.6) + 1);
// printf("inti = %dn delt = %dn", table->table_maxsize , table->table_delta_capacity );
int i;
for( i = 0; i < initial_capacity; i++ ) {
table->data[i] = NULL;
}
table->flag = (int*) calloc(initial_capacity, sizeof(int));
return table;
}
here is the program it crashes at
void insertEntry(Dictionary table, char * index, char * value){
entry* word;
int flag, hashval;
if((table->table_cursize) == (table->table_rehash))
tableRehash(table);
table->table_cursize += 1;
hashval = hash(index, (table->table_maxsize));
while(hashval <= (table->table_maxsize)){
word = table->data[hashval];
flag = table->flag[hashval];
if (flag == 0){
printf("word = %s deff = %sn", index, value);
//crashes here where word->word is at address 0x0
printf("word = %s deff = %s entry = %sn", index, value, word->word);
if (strcmp((word->word), index)){
printf("word = %s deff = %sn", index, value);
word->defn = value;
(table->flag[hashval]) = 1;
return;
}
printf("1n");
}
else{
word->word = malloc(sizeof( char*));
word->word = index;
word->defn = malloc(sizeof( char*));
word->defn = value;
table->flag[hashval] = 1;
return;
}
if (hashval == (table->table_maxsize))
hashval = -1;
hashval++;
}
return;
}
Aucun commentaire:
Enregistrer un commentaire