C OpenHashing分段错误
首先感谢您花时间通读并打开它。我基本上完成了这段代码,我相信一切都是近乎完美的,除了我得到了一个分割错误。这是一个家庭作业,但是我已经完成了所有的工作,但是我知道在我的一个Free()中有一些东西混在一起了,我相信它在我的Delete()函数中C OpenHashing分段错误,c,memory,hash,segmentation-fault,free,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
#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;
}
}
}
我相信这意味着它在free()中,因为这两个数字是匹配的,但是在free()中的什么地方不知道如何读这个,有人能帮忙吗?告诉我一个好的可下载调试器?终端为我指明了正确的方向,但我不确定到底是哪条线把我甩了,因为我有这么多空闲()
感谢您提供的帮助。尝试更换:
for(i = 0; i <= TABLE_SIZE; i++) {
for(i=0;i向表中添加项的代码在哪里?此代码中有很多free()但没有malloc()s、 添加我的插入功能抱歉,我不想让屏幕过载,也不认为编辑时需要一秒钟。不要像编辑时那样销毁内容。我们不会因为你得到了答案就删除内容。如果你不想让你的同学看到你的答案(或你的教授),那么一开始就不要在这里发帖。你会得到答案的,接受我的朋友!哇!我为此用头撞了两个小时!谢谢=)这是一个(很难?)记住array[N]
从0
到N-1
。。
for(i = 0; i <= TABLE_SIZE; i++) {
for(i = 0; i < TABLE_SIZE; i++) {