分配最近释放的内存

发布于 2024-11-24 18:08:27 字数 1663 浏览 6 评论 0原文

我有一个结构,用于构建链接列表,如下所示;

struct my_struct{
    char a[16];
    struct my_struct *next;
}

我通过以下函数释放该链接列表;

void free_my_list(struct my_struct* recv) {

     if (recv->next != NULL)
         free_my_list(recv->next);

     free(recv);
     recv = NULL;
}

在我的程序中,我一遍又一遍地使用结构_my_list,但每次都会释放并分配它,如下所示:

struct my_struct *_my_list;

free_my_list(_my_list);
_my_list = (my_list *) malloc(sizeof(my_list));
_my_list->next = NULL;

每次填充列表时,我都会打印字符数组,然后通过上面的代码重置_my_struct。 上面的代码在 Ubuntu pc 上运行良好,但在 Cent OS 上正确打印第一个列表(第一个 malloc _my_struct 之后)后,以下列表将打印为损坏的数据。

当我在整个程序执行期间不释放和分配内存时,它在 Cent OS 中也可以正常工作,但我应该在 printf() 调用之间重置列表 _my_list

_my_list 通过以下函数填充和打印;

/*prints every item in my_list*/
void print_my_list(struct my_struct *recv, FILE *fd) {

   my_list *tmp;
   tmp = recv;

   while (tmp != NULL) {
       if (fwrite(tmp->a, 1, strlen(tmp->a), fd) == -1) {
               pritnf("error\n");
        }
       tmp = tmp->next;
   }
}

/*Add 'a' string to _my_list*/
void add_recv_to_list(struct my_struct **recv_list, char *recv) {

struct my_struct *tmp;
tmp = *recv_list;

if (*recv_list == NULL) {
    *recv_list = (struct my_struct *) malloc(sizeof(struct my_struct));
    tmp = *recv_list;

} else {

    while ((tmp->next) != NULL) {
        tmp = tmp->next;
    }
    tmp->next = (struct my_struct *) malloc(sizeof(struct my_struct));
    tmp = tmp->next;

}
strncpy(tmp->a, recv, MAX_NAME_LEN);
tmp->next = NULL;
}

可能是什么原因,有什么想法吗?

I have a struct that I use to build a linked list as below;

struct my_struct{
    char a[16];
    struct my_struct *next;
}

I free that linked list by below function;

void free_my_list(struct my_struct* recv) {

     if (recv->next != NULL)
         free_my_list(recv->next);

     free(recv);
     recv = NULL;
}

In my program, I use a struct _my_list over and over but free and malloc it every time as below:

struct my_struct *_my_list;

free_my_list(_my_list);
_my_list = (my_list *) malloc(sizeof(my_list));
_my_list->next = NULL;

Every time I fill the list, I print char arrays and then reset _my_struct by above code.
Above code works fine on Ubuntu pc, but on Cent OS after printing first list(after first malloc _my_struct) correctly, following list are printed as corrupted data.

When I don't free and malloc memory during whole program execution it works fine in Cent OS too but I should reset list _my_list between printf() calls.

_my_list is filled and printed via below functions;

/*prints every item in my_list*/
void print_my_list(struct my_struct *recv, FILE *fd) {

   my_list *tmp;
   tmp = recv;

   while (tmp != NULL) {
       if (fwrite(tmp->a, 1, strlen(tmp->a), fd) == -1) {
               pritnf("error\n");
        }
       tmp = tmp->next;
   }
}

/*Add 'a' string to _my_list*/
void add_recv_to_list(struct my_struct **recv_list, char *recv) {

struct my_struct *tmp;
tmp = *recv_list;

if (*recv_list == NULL) {
    *recv_list = (struct my_struct *) malloc(sizeof(struct my_struct));
    tmp = *recv_list;

} else {

    while ((tmp->next) != NULL) {
        tmp = tmp->next;
    }
    tmp->next = (struct my_struct *) malloc(sizeof(struct my_struct));
    tmp = tmp->next;

}
strncpy(tmp->a, recv, MAX_NAME_LEN);
tmp->next = NULL;
}

What can be the reason, any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

嘴硬脾气大 2024-12-01 18:08:27

我认为你的问题可能从这里开始:

struct my_struct *_my_list;

free_my_list(_my_list);
_my_list = (my_list *) malloc(sizeof(my_list));
_my_list->next = NULL;

当你初始化 struc: struct my_struct *_my_list; 时,你没有给它分配任何值,所以它预先保存了内存中的任何垃圾数据。当您在 free_my_list 中 free() 时,行为是未定义的(您正在释放从未 malloc()ed 的东西 - 因此结果很可能是某些东西的损坏或稍后尝试将您的声明更改为:struct my_struct *_my_list = NULL;(无论如何,将指针初始化为 NULL 始终是一个好习惯)并将 free_my_list 函数更改为:

void free_my_list(struct my_struct* recv) {
    if (recv == NULL)
         return;

     if (recv->next != NULL)
         free_my_list(recv->next);

     free(recv);
     recv = NULL;
}

I think that your problem may start here:

struct my_struct *_my_list;

free_my_list(_my_list);
_my_list = (my_list *) malloc(sizeof(my_list));
_my_list->next = NULL;

When you initialize the struc: struct my_struct *_my_list; you don't assign it any value, so it holds whatever garbage data was in memory beforehand. When you free() that in free_my_list, the behavior is undefined (you are freeing something that you never malloc()ed - so the result may very well be corruption of something or other later on. Try changing your declaration to: struct my_struct *_my_list = NULL; (always a good practice to initialize pointers to NULL, anyway) and changing your free_my_list function to:

void free_my_list(struct my_struct* recv) {
    if (recv == NULL)
         return;

     if (recv->next != NULL)
         free_my_list(recv->next);

     free(recv);
     recv = NULL;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文