gpt4 book ai didi

c - 自由(): invalid next size (fast) - Freeing a struct of string array in c

转载 作者:行者123 更新时间:2023-11-30 16:02:26 25 4
gpt4 key购买 nike

您好,我正在尝试释放我创建的链接列表,但遇到了一些真正的麻烦。

我的系统是Ubuntu 10.10,我使用gcc编译器。我必须使用 makefile 和单独的源文件编译。这是makefile的头部

OBJS    = main.o Buffer.o ExtMerge.o MySet.o
SOURCE = main.c Buffer.c ExtMerge.c MySet.c
HEADER = MyHeader.h Buffer.h ExtMerge.h MySet.h
OUT = myjoin
CC = gcc
FLAGS = -g -c

该程序是外部排序程序的一部分。它从文件中读取大量记录,将它们插入到已排序的列表中,并将它们写入部分文件中。到目前为止,一切都按预期进行。然后另一个文件被打开。此时,明智的做法是释放内存,以便新列表(包含来自 file2 的数据)不会消耗额外的内存。

让我向您展示我的代码:列表的节点:

struct record{

char **field;
struct record *next;
};

此函数将标记数组(从文件中读取)插入到有序列表中:cols_no 是数组中字符串的数量,column 是感兴趣的字符串。下面的 malloc 东西工作得非常完美

struct record *OrderedInsertRec(struct record *buf, char **arr, int column, int cols_no)
{ struct record *aux=NULL;
int i;

if ( buf==NULL ) {
buf = (struct record *) malloc (sizeof(struct record)+1);
buf->field=(char **)calloc(cols_no, sizeof(char*));
for (i = 0; i <= cols_no; ++i){
buf->field[i] = (char*)malloc(((strlen(arr[i])+1))*sizeof(char*));
strcpy(buf->field[i],arr[i]);
}

buf->next = NULL;
return(buf);
}
else if ( strverscmp (buf->field[column], arr[column]) < 0 ) {
buf->next = OrderedInsertRec (buf->next, arr, column, cols_no);
return(buf);
}
else {
aux = (struct record *) malloc (sizeof(struct record)+1);
aux->field=(char **)calloc(cols_no, sizeof(char*));
for (i = 0; i <= cols_no; ++i){
aux->field[i] = malloc(strlen(arr[i])+1);
strcpy(aux->field[i],arr[i]);
}

aux->next = NULL;
aux->next = buf;
return(aux);
}

}

这给我带来了麻烦:

struct record * DeleteBuffer(struct record * buf, int cols_no)
{
int i;

struct record * tmp;

while(buf != NULL)
{

tmp = buf;
buf = buf->next;
}
free(tmp);
}

}

这可以正常工作,但什么也不做。正如我从系统监视器中看到的那样,只释放了少量内存(我想是因为我只释放了在 OrderedInsert 中分配的指针,而不是 field[] 数组)

然后我尝试了这个:

struct record * DeleteBuffer(struct record * buf, int cols_no)
{
int i;


struct record * tmp;
while(buf != NULL)
{

tmp = buf;
buf = buf->next;
for (i = 0; i <= cols_no; ++i){

free(tmp->field[i]);
}

free(tmp);
}
}
}

这就是我得到的:{

*** glibc detected *** ./myjoin: double free or corruption (out): 0x088b3010 ***
======= Backtrace: =========
/lib/libc.so.6(+0x6c501)[0xb766e501]
/lib/libc.so.6(+0x6dd70)[0xb766fd70]
/lib/libc.so.6(cfree+0x6d)[0xb7672e5d]
./myjoin[0x8048bee]
./myjoin[0x80495b4]
./myjoin[0x8048a10]
/lib/libc.so.6(__libc_start_main+0xe7)[0xb7618ce7]
./myjoin[0x8048791]
======= Memory map: ========
08048000-0804b000 r-xp 00000000 fb:03 2999566 /home/giorgos/Desktop/test11/myjoin
0804b000-0804c000 r--p 00002000 fb:03 2999566 /home/giorgos/Desktop/test11/myjoin
0804c000-0804d000 rw-p 00003000 fb:03 2999566 /home/giorgos/Desktop/test11/myjoin
088a0000-088c1000 rw-p 00000000 00:00 0 [heap]
b74d3000-b74ed000 r-xp 00000000 fb:03 3940432 /lib/libgcc_s.so.1
b74ed000-b74ee000 r--p 00019000 fb:03 3940432 /lib/libgcc_s.so.1
b74ee000-b74ef000 rw-p 0001a000 fb:03 3940432 /lib/libgcc_s.so.1
b7500000-b7521000 rw-p 00000000 00:00 0
b7521000-b7600000 ---p 00000000 00:00 0
b7601000-b7602000 rw-p 00000000 00:00 0
b7602000-b7759000 r-xp 00000000 fb:03 3940764 /lib/libc-2.12.1.so
b7759000-b775b000 r--p 00157000 fb:03 3940764 /lib/libc-2.12.1.so
b775b000-b775c000 rw-p 00159000 fb:03 3940764 /lib/libc-2.12.1.so
b775c000-b775f000 rw-p 00000000 00:00 0
b776d000-b7772000 rw-p 00000000 00:00 0
b7772000-b7773000 r-xp 00000000 00:00 0 [vdso]
b7773000-b778f000 r-xp 00000000 fb:03 3940761 /lib/ld-2.12.1.so
b778f000-b7790000 r--p 0001b000 fb:03 3940761 /lib/ld-2.12.1.so
b7790000-b7791000 rw-p 0001c000 fb:03 3940761 /lib/ld-2.12.1.so
bfb1a000-bfb3b000 rw-p 00000000 00:00 0 [stack]
Aborted

}

我该怎么办。是我的malloc有问题吗?预先感谢您!

最佳答案

我发现三个问题。你malloc/calloc:

      buf = (struct record *) malloc (sizeof(struct record)+1);
buf->field=(char **)calloc(cols_no, sizeof(char*));

为什么sizeof(struct record)+1+1 是做什么用的?

calloc() cols_no个大小为sizeof(char*)的项目,但是你的两个for循环从0开始... cols,它应该从 0...cols-1 开始。

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

应该是:

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

您应该分配 (strlen(arr[i])+1)*sizeof(char) - 使用 sizeof(char*) 可能是 4-8 倍太多了,假设 sizeof(char) == 1 和 sizeof(char*) == 4 或 8,具体取决于 32 位或 64 位指针。

      buf->field[i] = (char*)malloc(((strlen(arr[i])+1))*sizeof(char*));

应该是:

      buf->field[i] = (char*)malloc(((strlen(arr[i])+1))*sizeof(char));

关于c - 自由(): invalid next size (fast) - Freeing a struct of string array in c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5341323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com