结构内的免费链接列表

发布于 2025-02-01 02:31:51 字数 1593 浏览 2 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅语花开 2025-02-08 02:31:51

struct ordi中,logicielsinstalles应该是指针,而不是未安装软件的计算机的结构,并避免使用无效的free on & suntinateur-> logicielsinstalles

此外,您的函数formaterrictionderur在释放列表后不会重置指针logicielSinstalles。相反,它设置了sutinateUr = null,它没有效果,因为sutinateur只是本地参数值。

这可能会导致稍后访问无效或双重免费。

这是一个修改版本:

typedef struct lst_logi {
    log *logiciel;
    struct lst_logi *next;
    struct lst_logi *prev;
} lst_logi;

typedef struct ordi {
    int numeroOrdi;
    char *nomOrdi;
    int CapaciteDisque;
    lst_logi *LogicielsInstalles;
} ordi;

void FormaterOrdinateur(ordi *ordinateur) {
    lst_logi *node = ordinateur->LogicielsInstalles;

    while (node) {
        lst_logi *next = node->next;
        // free the log structure pointed to by node->logiciel
        free(node->logiciel);
        // free the node structure
        free(node);
        node = next;
    }
    ordinateur->LogicielsInstalles = NULL;
}

In the struct ordi, LogicielsInstalles should be a pointer, not a structure to account for computers with no installed software, and avoid an invalid free on &ordinateur->LogicielsInstalles.

Furthermore, your function FormaterOrdinateur does not reset the pointer LogicielsInstalles after freeing the list. It instead sets ordinateur = NULL, which has no effect as ordinateur is just a local argument value.

This may cause invalid access later or a double free.

Here is a modified version:

typedef struct lst_logi {
    log *logiciel;
    struct lst_logi *next;
    struct lst_logi *prev;
} lst_logi;

typedef struct ordi {
    int numeroOrdi;
    char *nomOrdi;
    int CapaciteDisque;
    lst_logi *LogicielsInstalles;
} ordi;

void FormaterOrdinateur(ordi *ordinateur) {
    lst_logi *node = ordinateur->LogicielsInstalles;

    while (node) {
        lst_logi *next = node->next;
        // free the log structure pointed to by node->logiciel
        free(node->logiciel);
        // free the node structure
        free(node);
        node = next;
    }
    ordinateur->LogicielsInstalles = NULL;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文