0

首先感谢您花时间阅读并打开它。我基本上完成了这段代码,我相信一切都近乎完美,除了我遇到了分段错误。这是一项家庭作业,但我已经完成了所有的工作,但我知道我的一个 Free() 中有一些东西混在一起,我相信它在我的 Delete() 函数中。

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

}

我通过终端的 gdb 调试器运行它,我得到了这个:

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 ()

我相信这意味着它在 free() 中,因为这两个数字匹配,但是在 free() 中的哪里不知道如何阅读,有人可以帮忙吗?指点我一个好的可下载调试器?终端机为我指明了正确的方向,但我不确定究竟是哪条线路让我失望,因为我有这么多 free()。

感谢您提供的任何帮助。

4

1 回答 1

2

尝试更换:

for(i = 0; i <= TABLE_SIZE; i++) {

和:

for(i = 0; i < TABLE_SIZE; i++) {
于 2013-04-17T19:55:51.680 回答