结构内的免费链接列表
我有一个type
type ordi
typedef struct ordi
{
int numeroOrdi;
char *nomOrdi;
int CapaciteDisque;
lst_logi LogicielsInstalles;
} ordi;
此surtinateur
具有双重链接列表logicielsinstalles
type lst_logi
/code>
typedef struct lst_logi
{
log *logiciel;
struct lst_logi *next;
struct lst_logi *prev;
} lst_logi;
我制作了一个函数formaterrictionsur
删除所有“ logiciels”(法语中的软件)
换句话说,链接列表logicielsinstalles
。
void FormaterOridinateur(ordi *ordinateur)
{
lst_logi *head = &ordinateur->LogicielsInstalles;
lst_logi *tmp;
// printf("curr: %s\n", head->logiciel->designation);
// printf("curr->next: %s\n", (head->next)->logiciel->designation);
// these two line to make sure the node and node->next exit
while (head)
{
tmp = head;
head = head->next;
free(tmp);
}
ordinateur = NULL;
}
但是这个功能给了我SEG故障
带有GDB的更多详细信息:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7e244c9 in arena_for_chunk (ptr=0x7fffffffe051) at ./malloc/arena.c:156
156 ./malloc/arena.c: No such file or directory.
当我搜索Arena_for_chunk
错误时,我发现它意味着内存不存在或无法释放
我敢肯定,我正在尝试自由的节点,所以它只是拒绝释放。
任何解释。我该如何修复它。
提前致谢 ;)
I have an ordinateur
of type ordi
typedef struct ordi
{
int numeroOrdi;
char *nomOrdi;
int CapaciteDisque;
lst_logi LogicielsInstalles;
} ordi;
this ordinateur
has a doubly linked list LogicielsInstalles
of type lst_logi
typedef struct lst_logi
{
log *logiciel;
struct lst_logi *next;
struct lst_logi *prev;
} lst_logi;
I made a function FormaterOrdinateur
to delete all "Logiciels"(Software in french)
In other word free the linked list LogicielsInstalles
.
void FormaterOridinateur(ordi *ordinateur)
{
lst_logi *head = &ordinateur->LogicielsInstalles;
lst_logi *tmp;
// printf("curr: %s\n", head->logiciel->designation);
// printf("curr->next: %s\n", (head->next)->logiciel->designation);
// these two line to make sure the node and node->next exit
while (head)
{
tmp = head;
head = head->next;
free(tmp);
}
ordinateur = NULL;
}
but this function gives me seg fault
more details with gdb:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7e244c9 in arena_for_chunk (ptr=0x7fffffffe051) at ./malloc/arena.c:156
156 ./malloc/arena.c: No such file or directory.
when I searched about arena_for_chunk
error I found out that it means memory doesn't exist or can't be freed
I'm sure the node I'm trying to free exists so it just refuses to be freed.
any explanation for that. And how can I fix it.
Thanks in advance ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
struct ordi
中,logicielsinstalles
应该是指针,而不是未安装软件的计算机的结构,并避免使用无效的free
on& suntinateur-> logicielsinstalles
。此外,您的函数
formaterrictionderur
在释放列表后不会重置指针logicielSinstalles
。相反,它设置了sutinateUr = null
,它没有效果,因为sutinateur
只是本地参数值。这可能会导致稍后访问无效或双重免费。
这是一个修改版本:
In the
struct ordi
,LogicielsInstalles
should be a pointer, not a structure to account for computers with no installed software, and avoid an invalidfree
on&ordinateur->LogicielsInstalles
.Furthermore, your function
FormaterOrdinateur
does not reset the pointerLogicielsInstalles
after freeing the list. It instead setsordinateur = NULL
, which has no effect asordinateur
is just a local argument value.This may cause invalid access later or a double free.
Here is a modified version: