Reputation: 17
first off thanks for taking the time to read through this and opening it up. I am basically done with this code and I believe everything is nearly perfect except I am getting a segmentation fault. It is a homework assignment, but I've done all of the work, but I know there's just something mixed up in one of my Free()'s and I believe it is in my Delete() function.
#define TABLE_SIZE 310987 // size of the hash table
#define true 1
#define false 0
static int hash(BOOK *b);
/* Deletes a book with key (b -> isbn) from the table.
If the book is found, it is deleted from the table
and true is returned; If no book with the given isbn
exists, False is returned. *comps holds the number
of comparisons done for this deletion.
*/
void insert(NODE *table[], BOOK x, int *collisionCount){
int i = hash(&x);
NODE *temp;
temp = (NODE *) malloc(sizeof(NODE));
assert(temp != NULL);
temp -> element = x;
temp -> next = table[i];
if(table[i] != NULL) {
*collisionCount += 1;
}
table[i] = temp;
}
boolean delete (NODE *table[], BOOK *b, int *comps){
NODE *current, *previous;
int i = hash(b);
*comps = 0;
current = table[i];
while(current != NULL) {
*comps += 1;
if(strcmp(current -> element.isbn, b -> isbn) == 0) {
if(current == table[i]) {
table[i] = current -> next;
free(current -> element.title);
free(current -> element.author);
free(current -> element.publisher);
free(current);
return true;
}
else {
previous -> next = table[i] -> next;
free(current -> element.title);
free(current -> element.author);
free(current -> element.publisher);
free(current);
}
}
previous = current;
current = current -> next;
}
return false;
}
/* initializes the hash table to an empty table */
void initialize(NODE *table[]){
int i;
for(i = 0; i < TABLE_SIZE; i++) {
table[i] = NULL;
}
}
/* prints one BOOK object to the ouptut file */
void printToFile(const BOOK *b, FILE *fpout) {
fprintf(fpout, "ISBN: %s", b -> isbn);
fprintf(fpout, "ISBN: %s", b -> title);
fprintf(fpout, "ISBN: %s", b -> author);
fprintf(fpout, "ISBN: %d", b -> year);
fprintf(fpout, "ISBN: %s", b -> publisher);
}
/* frees all the memory allocated on the heap */
void freeMemory(NODE * table[]){
NODE *temp;
int i;
for(i = 0; i <= TABLE_SIZE; i++) {
while(table[i] != NULL) {
temp = table[i] -> next;
free(table[i] -> element.title);
free(table[i] -> element.author);
free(table[i] -> element.publisher);
free(table[i]);
table[i] = temp;
}
}
}
I ran it through terminal's gdb debugger and I got this:
gdb OpenHashing
(no debugging symbols found)...done.
(gdb) run OpenHashing
Starting program: OpenHashing
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a9e59c in free ()
(gdb) backtrace
0 0x00007ffff7a9e59c in free ()
1 0x0000000000401889 in freeMemory ()
2 0x00000000004012d4 in main ()
I believe this means that it is in the free() since the two numbers match up, but where in the free() not sure how to read this can anyone help? Point me to a good down loadable debugger? Terminal has pointed me in the right direction, but I am unsure exactly what line is throwing me off since I have so many free()'s.
Thank you appreciate any help given.
Upvotes: 0
Views: 68
Reputation: 883
Try replacing:
for(i = 0; i <= TABLE_SIZE; i++) {
with:
for(i = 0; i < TABLE_SIZE; i++) {
Upvotes: 2