c - OpenHashing 分段故障

标签 c memory hash segmentation-fault free

首先感谢您花时间阅读并打开它。我基本上完成了这段代码,我相信一切都近乎完美,除了我遇到了段错误。这是一项家庭作业,但我已经完成了所有的工作,但我知道我的一个 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()。

感谢您提供的任何帮助。

最佳答案

尝试替换:

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

与:

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

关于c - OpenHashing 分段故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16068971/

相关文章:

ruby - 如何根据现有键名复制 Ruby 哈希?

ruby - 在 Ruby 散列中使用 fixnums 作为键好吗?

java - 我可以读取链接 key 表吗?

c - 来自curl_easy_perform的管道数据

c - POSIX TIMER- 有多个计时器

无法读写(内存映射)硬件寄存器

c - 如何在回调函数中从文件加载 GdkPixbuf?

c++ - 在 RAM 中保留数据,同时允许其他进程在 linux C++ 中访问它

C++ 字符分配

hash - HMAC + SHA256 jwt secret 长度