G
Grant Austin
I'm implementing a hash table. I really think I'm doing
everything acceptably but I'm still getting Seg Faults whenever
I try and search the table and the symbol has not already
been added to the table. I can figure out how to 'stop' on time.
The pointer to the last node never goes NULL as far as I can tell.
The address that the hash_table entry's next pointer is
pointing to is causing the fault.(I think)
I've cut down the code to try and give a good description but it's
still fairly lengthy to allow clarity.
Any ideas much appreciated.
Regards,
Grant
Code Follows;
main.c
#include <stdio.h>
#include "hash_table.h"
int main(void){
unsigned int
hidx;
hashList *hash_head[22];
hash_insert(hash_head,1,-1,"symbol2");
hash_insert(hash_head,1,-1,"symbol1");
hidx = hash("symbol3",22);
hash_search(hash_head[hidx],"symbol3")
hidx = hash("symbol2",22);
hash_search(hash_head[hidx],"symbol2")
return 0;
}
g_hash_table.h
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
struct hashNode{
struct hashNode * next;
lcList * lc_head;
int defined; /*boolean flag non-zero is defined*/
unsigned int lc;
char *symbol;
};
typedef struct hashNode hashList;
int hash(char * s, int T){
int
h = 0;
int
a = 127;
for (; *s != 0; s++)
h = (a * h + *s) % T;
return h;
}
hashList * hash_search(hashList *hash_cur, char * symbol){
if(hash_cur == NULL) return hash_cur;
while(hash_cur){
if(strcmp(hash_cur->symbol,symbol) == 0){
printf("symbol %s found in %d\n",symbol,hash_cur);
return hash_cur;
}
else {
printf("debug: %u %u\n",hash_cur,hash_cur->next);
hash_cur = hash_cur->next;
}
}
printf("end hash search for %s : hash_cur %d\n",symbol,hash_cur);
return hash_cur;
}
int hash_insert(hashList * hash_head[], int defined, unsigned int lc,
char * symbol){
unsigned int hidx;
hashList * hash_cur = malloc(sizeof *hash_cur);
hashList * hash_test = NULL;
if(hash_cur == NULL) return 0;
hidx = hash(symbol,22);
if((hash_cur->symbol = malloc(strlen(symbol)+1)) == NULL){
free(hash_cur);
return -1;
}
strcpy(hash_cur->symbol,symbol);
hash_cur->defined = defined;
hash_cur->lc = lc;
hash_cur->lc_head = NULL;
hash_cur->next = hash_head[hidx];
hash_head[hidx] = hash_cur;
printf("inserted\n");
return 1;
}
unsigned int isDefined(hashList * hash_head[], char * symbol){
/* returns 1 if symbol is defined, 0 else */
unsigned int hidx;
hashList * hash_test = malloc(sizeof *hash_test);
hidx = hash(symbol,22);
if ((hash_test = hash_search(hash_head[hidx], symbol)) != NULL){
return hash_test->defined;
}
return 0;
}
unsigned int symDefine(hashList * hash_head[], char * symbol,unsigned int lc){
unsigned int hidx;
hashList *hash_test = malloc(sizeof *hash_test);
hidx = hash(symbol,22);
if ((hash_test = hash_search(hash_head[hidx], symbol)) != NULL){
hash_test->defined = 1;
hash_test->lc = lc;
return 1;
}
return 0;
}
void hashList_free(hashList **hash_cur){
hashList *tmp;
for( ; *hash_cur; *hash_cur = tmp){
tmp = (*hash_cur)->next;
free((*hash_cur)->symbol);
free(*hash_cur);
}
return;
}
everything acceptably but I'm still getting Seg Faults whenever
I try and search the table and the symbol has not already
been added to the table. I can figure out how to 'stop' on time.
The pointer to the last node never goes NULL as far as I can tell.
The address that the hash_table entry's next pointer is
pointing to is causing the fault.(I think)
I've cut down the code to try and give a good description but it's
still fairly lengthy to allow clarity.
Any ideas much appreciated.
Regards,
Grant
Code Follows;
main.c
#include <stdio.h>
#include "hash_table.h"
int main(void){
unsigned int
hidx;
hashList *hash_head[22];
hash_insert(hash_head,1,-1,"symbol2");
hash_insert(hash_head,1,-1,"symbol1");
hidx = hash("symbol3",22);
hash_search(hash_head[hidx],"symbol3")
hidx = hash("symbol2",22);
hash_search(hash_head[hidx],"symbol2")
return 0;
}
g_hash_table.h
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
struct hashNode{
struct hashNode * next;
lcList * lc_head;
int defined; /*boolean flag non-zero is defined*/
unsigned int lc;
char *symbol;
};
typedef struct hashNode hashList;
int hash(char * s, int T){
int
h = 0;
int
a = 127;
for (; *s != 0; s++)
h = (a * h + *s) % T;
return h;
}
hashList * hash_search(hashList *hash_cur, char * symbol){
if(hash_cur == NULL) return hash_cur;
while(hash_cur){
if(strcmp(hash_cur->symbol,symbol) == 0){
printf("symbol %s found in %d\n",symbol,hash_cur);
return hash_cur;
}
else {
printf("debug: %u %u\n",hash_cur,hash_cur->next);
hash_cur = hash_cur->next;
}
}
printf("end hash search for %s : hash_cur %d\n",symbol,hash_cur);
return hash_cur;
}
int hash_insert(hashList * hash_head[], int defined, unsigned int lc,
char * symbol){
unsigned int hidx;
hashList * hash_cur = malloc(sizeof *hash_cur);
hashList * hash_test = NULL;
if(hash_cur == NULL) return 0;
hidx = hash(symbol,22);
if((hash_cur->symbol = malloc(strlen(symbol)+1)) == NULL){
free(hash_cur);
return -1;
}
strcpy(hash_cur->symbol,symbol);
hash_cur->defined = defined;
hash_cur->lc = lc;
hash_cur->lc_head = NULL;
hash_cur->next = hash_head[hidx];
hash_head[hidx] = hash_cur;
printf("inserted\n");
return 1;
}
unsigned int isDefined(hashList * hash_head[], char * symbol){
/* returns 1 if symbol is defined, 0 else */
unsigned int hidx;
hashList * hash_test = malloc(sizeof *hash_test);
hidx = hash(symbol,22);
if ((hash_test = hash_search(hash_head[hidx], symbol)) != NULL){
return hash_test->defined;
}
return 0;
}
unsigned int symDefine(hashList * hash_head[], char * symbol,unsigned int lc){
unsigned int hidx;
hashList *hash_test = malloc(sizeof *hash_test);
hidx = hash(symbol,22);
if ((hash_test = hash_search(hash_head[hidx], symbol)) != NULL){
hash_test->defined = 1;
hash_test->lc = lc;
return 1;
}
return 0;
}
void hashList_free(hashList **hash_cur){
hashList *tmp;
for( ; *hash_cur; *hash_cur = tmp){
tmp = (*hash_cur)->next;
free((*hash_cur)->symbol);
free(*hash_cur);
}
return;
}