C - 无法释放分配的结构

发布于 2025-01-09 04:20:23 字数 1800 浏览 0 评论 0原文

我正在尝试用 C 创建一个简单的家谱程序,但是我遇到了一个问题,以下结构的实例拒绝释放,并且我遇到内存泄漏,

typedef struct Person {
    struct person* *parents[2];
    struct person* partner;
    struct person* *children[32];
} person;

包含它的随附结构是

typedef struct Tree {
    person* top;
    person* current;
} tree;

树结构释放的很好,但是当我尝试释放 person 结构的 malloc 内存时,我得到了内存泄漏,我很确定表明该内存。

void newPerson(tree *t){
    person *p = malloc(sizeof(person));
...

这是在同一函数内分配内存的函数

...
if (t->current){
t->top = p; 
t->current = p; 
...

,两个指针都设置为指向 p。

int main(){
    tree *t = malloc(sizeof(tree));
    t->current = NULL;
    newPerson(t);
    free(t->current);
    free(t);
    return 0;
}

这是主函数中创建和释放树变量的代码。从我尝试解决此问题的各种方法来看,发生了以下情况。

  1. 如果我将 free(p) 放在创建 Person 的同一函数中,则一切正常,没有错误,也没有内存泄漏
  2. 如果我尝试释放 t->current,我会收到泄漏清理错误,告诉我直接泄漏Person 的确切分配位置
  3. 如果我尝试释放 t->top,则会收到未知地址错误的 SEGV。

现在我知道问题出在 t 的某个地方,但我对问题实际上是什么有最微弱的线索,要么我对 malloc 和 free 的知识已经退化到我做错了什么而我看不到它,或者有其他事情正在发生。

编辑:代表

#include <stdio.h>
#include <stdlib.h>

typedef struct Branch {
    struct branch* otherbranch;
} branch;

typedef struct Tree {
    branch* root;
    branch* current;
} tree

void newBranch(tree *t){
    branch *b = malloc(sizeof(branch));
    b->otherbranch = NULL;
    if (t->current){
        t->root = b; 
        t->current = b; 
    }
    //free(b); //case 1 where freeing works
}

int main(){
    tree *t = malloc(sizeof(tree));
    t->current = NULL;
    newBranch(t);
    free(t->root); //case 3 where segv occurs
    //free(t->current); //case 2 where memory leak occurs
    free(t);
    return 0;
}

I am attempting to create a simple family tree program in C, however I am encountering an issue where an instance of the following struct refuses to be free and i am getting a memory leak

typedef struct Person {
    struct person* *parents[2];
    struct person* partner;
    struct person* *children[32];
} person;

the accompanying struct which encompasses it is this

typedef struct Tree {
    person* top;
    person* current;
} tree;

the Tree struct frees just fine, however when I attempt to free the malloced memory of the person struct, I get a memory leak I am pretty sure indicates that memory.

void newPerson(tree *t){
    person *p = malloc(sizeof(person));
...

this is the function where the memory is malloced

...
if (t->current){
t->top = p; 
t->current = p; 
...

within the same function, both pointers are set to point to p.

int main(){
    tree *t = malloc(sizeof(tree));
    t->current = NULL;
    newPerson(t);
    free(t->current);
    free(t);
    return 0;
}

And this is the code within the main function where the tree variable is created, and where it is freed. From the various ways ive been trying to fix this, the following happened.

  1. If I put free(p) within the same function the Person is created, everything works fine and there are no errors nor memory leaks
  2. If I try to free t->current, I get a leak sanitiser error telling me of a direct leak exactly where the Person is allocated
  3. If i try to free t->top, I get a SEGV on unknown address error.

Now I know the issue is somewhere with t, but I have the faintest of clue as to what the issue actually is, either my knowledge of malloc and free has degraded to the point where im doing something wrong and i can't see it, or something else is going on.

Edit: Reprex

#include <stdio.h>
#include <stdlib.h>

typedef struct Branch {
    struct branch* otherbranch;
} branch;

typedef struct Tree {
    branch* root;
    branch* current;
} tree

void newBranch(tree *t){
    branch *b = malloc(sizeof(branch));
    b->otherbranch = NULL;
    if (t->current){
        t->root = b; 
        t->current = b; 
    }
    //free(b); //case 1 where freeing works
}

int main(){
    tree *t = malloc(sizeof(tree));
    t->current = NULL;
    newBranch(t);
    free(t->root); //case 3 where segv occurs
    //free(t->current); //case 2 where memory leak occurs
    free(t);
    return 0;
}

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

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

发布评论

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

评论(1

北音执念 2025-01-16 04:20:23

当您输入此代码时,

void newBranch(tree *t){
    branch *b = malloc(sizeof(branch));
    b->otherbranch = NULL;
    if (t->current){
        t->root = p; 
        t->current = p; 
    }
    //free(b); //case 1 where freeing works
}

t->current为空,因此您永远不会设置t->root或t->current。

然后返回main并执行free(t->root),t->root此时无效。您可能想将 t->root 初始化为 NULL,也许 t->current 也这样,

我还假设您的意思是 t->root = b 而不是 = p 因为没有 pi 可以看到

when you enter this code

void newBranch(tree *t){
    branch *b = malloc(sizeof(branch));
    b->otherbranch = NULL;
    if (t->current){
        t->root = p; 
        t->current = p; 
    }
    //free(b); //case 1 where freeing works
}

t->current is null, so you never set t->root or t->current.

you then return to main and do free(t->root), t->root is invalid at this point. You probably want to initialize t->root to NULL and maybe t->current too

i also assume you mean t->root = b not = p since there is no p i can see

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