我正在编写一个程序来创建一个用于创建列表的结构,但是我遇到了双重自由或损坏错误。我知道链接列表是一个更好的实现,但我想知道我在这里做错了什么。代码在我的IDE中完美运行,但显示了 在Ubuntu的终端上使用GCC运行时出现上述错误。
/* Implements an employee structure having name and company name
and can take new employees, delete last employee and display entire list of employees */
#include <stdio.h>
#include <stdlib.h>
// defining structure employee
typedef struct emp
{
char name[20];
char company[20];
}emp;
int p = 0, size = 0;
void create(emp *);
void del(emp *);
void display(emp *);
int main(void)
{
emp *buffer = malloc(sizeof(emp));
if (buffer == NULL)
{
printf("Error");
exit(1);
}
int n = 0;
do
{
printf("The options are:\n");
printf("1. Add employee\n");
printf("2. Delete employee\n");
printf("3. Display employee\n");
printf("4. Exit\n");
printf("Enter from 1 to 4:");
scanf("%d",&n);
if (n == 1)
{
create(buffer);
}
else if (n == 2)
{
del(buffer);
}
else if (n == 3)
{
display(buffer);
}
}while(n != 4);
free(buffer);
return 0;
}
void create(emp *buffer)
{
size++;
if (p != 0)
{
buffer = realloc(buffer, size * sizeof(emp));
if (buffer == NULL)
{
printf("Error");
exit(1);
}
}
printf("Enter:");
scanf("%s%s", (buffer + size - 1)->name, (buffer + size - 1)->company);
p++;
}
void del(emp *buffer)
{
// deletes only last node
size--;
buffer = realloc(buffer, size * sizeof(emp));
if (buffer == NULL)
{
printf("Error");
exit(1);
}
}
void display(emp *buffer)
{
int k = 0;
for (k = 1; k <= size; k++)
{
printf("Name:%s\n",(buffer + k - 1)->name);
printf("Company:%s\n",(buffer + k - 1)->company);
}
}
更新: 我跑了valgrind并得到以下内容,但无法理解我在哪里错误地重新分配。
==3322== Invalid free() / delete / delete[] / realloc()
==3322== at 0x4C2FD5F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3322== by 0x4008C9: create (in /home/akshay/Data/practice/a.out)
==3322== by 0x40081C: main (in /home/akshay/Data/practice/a.out)
==3322== Address 0x5203040 is 0 bytes inside a block of size 40 free'd
==3322== at 0x4C2FD5F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3322== by 0x4008C9: create (in /home/akshay/Data/practice/a.out)
==3322== by 0x40081C: main (in /home/akshay/Data/practice/a.out)
==3322== Block was alloc'd at
==3322== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3322== by 0x400786: main (in /home/akshay/Data/practice/a.out)
==3322==
Error==3322==
==3322== HEAP SUMMARY:
==3322== in use at exit: 80 bytes in 1 blocks
==3322== total heap usage: 5 allocs, 4 frees, 2,288 bytes allocated
==3322==
==3322== LEAK SUMMARY:
==3322== definitely lost: 80 bytes in 1 blocks
==3322== indirectly lost: 0 bytes in 0 blocks
==3322== possibly lost: 0 bytes in 0 blocks
==3322== still reachable: 0 bytes in 0 blocks
==3322== suppressed: 0 bytes in 0 blocks
==3322== Rerun with --leak-check=full to see details of leaked memory
==3322==
==3322== For counts of detected and suppressed errors, rerun with: -v
==3322== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
答案 0 :(得分:2)
您正在通过值将缓冲区传递给create
和del
,因此您所做的任何更改都不会被反映出来。因此free
获得buffer
的旧值。你有3个选择 -
O1:使缓冲区全局化,就像你对大小所做的那样,并且不要将它作为参数传递。容易但不建议。
O2:从buffer
和del
返回create
的新值。然后在调用时执行
buffer = del(buffer);
O3:将指针传递给缓冲区而不是缓冲区并在其中进行修改。您的原型将更改为
void create(emp ** buffer);
您必须在*buffer
内的每个位置使用create
(del
相同)。
此外,通话将更改为
create(&buffer);
这有点复杂但是最正确的&#34;之一。
希望能帮助您理解这个问题。