除非以前已分配(并释放),否则可以将功能的结构指针引用

发布于 2025-01-30 16:55:41 字数 824 浏览 2 评论 0原文

我应该对代码进行长时间的解释,但是解释已经在下面的代码中,所以我想我的问题是:如何使它起作用而无需杂乱然后释放它?还是基本上,在这种情况下写这篇文章的正确方法是什么?

#include <stdio.h>
#include <malloc.h>

struct d {
    int f;
};

struct d* rr() {
    struct d* p = malloc(sizeof (struct d*));
    p->f = 33;
    return p;
}

void rr2(struct d* p) {
    p = malloc(sizeof (struct d*));
    p->f = 22;
}



int main()
{
    //works..
    struct d* g;
    g = malloc(sizeof (struct d));
    g->f = 45;
    printf("[%i]", g->f);
    
    //works..
    g = rr();
    printf("[%i]", g->f);
    

    //below, both are same, except in this first case, g is allocated then freed..
 
    //works..
    free(g);
    rr2(g);
    printf("[%i]", g->f);
    
    //doesn't work..
    struct d *q;
    rr2(q);
    printf("[%i]", q->f);



    return 0;
}

I supposed to write a long explanation of the code but the explanation is already in the code below so I guess my question is: How do I get it to work without having to malloc then freeing it? or basically what is the correct way to write this in a situation like this?

#include <stdio.h>
#include <malloc.h>

struct d {
    int f;
};

struct d* rr() {
    struct d* p = malloc(sizeof (struct d*));
    p->f = 33;
    return p;
}

void rr2(struct d* p) {
    p = malloc(sizeof (struct d*));
    p->f = 22;
}



int main()
{
    //works..
    struct d* g;
    g = malloc(sizeof (struct d));
    g->f = 45;
    printf("[%i]", g->f);
    
    //works..
    g = rr();
    printf("[%i]", g->f);
    

    //below, both are same, except in this first case, g is allocated then freed..
 
    //works..
    free(g);
    rr2(g);
    printf("[%i]", g->f);
    
    //doesn't work..
    struct d *q;
    rr2(q);
    printf("[%i]", q->f);



    return 0;
}

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

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

发布评论

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

评论(1

不即不离 2025-02-06 16:55:41

对于两个功能中的初学者

struct d* rr() {
    struct d* p = malloc(sizeof (struct d*));
    p->f = 33;
    return p;
}

void rr2(struct d* p) {
    p = malloc(sizeof (struct d*));
    p->f = 22;
}

都有错别字。看来您的意思

    struct d* p = malloc(sizeof (struct d));
                                 ^^^^^^^^

    p = malloc(sizeof (struct d));
                       ^^^^^^^^^

    struct d* p = malloc(sizeof ( *p ));
                                 ^^^^^

    p = malloc(sizeof ( *p) );
                       ^^^^^

此功能有关

void rr2(struct d* p) {
    p = malloc(sizeof (struct d*));
    p->f = 22;
}

,然后在此调用中,

struct d *q;
rr2(q);

指针q通过值传递给该函数。因此,该功能处理指针Q的副本。更改函数中的副本不会反映原始指针Q。它保持不变。

要使代码正常工作,您必须通过参考将指针传递(通过指向其指针间接传递)。在这种情况下,该函数将看起来像

void rr2(struct d **p) {
    *p = malloc(sizeof (struct d ));
    ( *p )->f = 22;
}

和称为

rr2( &q );

此代码段

free(g);
rr2(g);
printf("[%i]", g->f);

,然后将其调用不确定的行为,因为在此语句中,

printf("[%i]", g->f);

可以访问已经释放的内存。

For starters in the both functions

struct d* rr() {
    struct d* p = malloc(sizeof (struct d*));
    p->f = 33;
    return p;
}

and

void rr2(struct d* p) {
    p = malloc(sizeof (struct d*));
    p->f = 22;
}

there is a typo. It seems you mean

    struct d* p = malloc(sizeof (struct d));
                                 ^^^^^^^^

and

    p = malloc(sizeof (struct d));
                       ^^^^^^^^^

or

    struct d* p = malloc(sizeof ( *p ));
                                 ^^^^^

and

    p = malloc(sizeof ( *p) );
                       ^^^^^

As for this function

void rr2(struct d* p) {
    p = malloc(sizeof (struct d*));
    p->f = 22;
}

then in this call

struct d *q;
rr2(q);

the pointer q is passed to the function by value. So the function deals with a copy of the pointer q. Changing the copy within the function does not reflect on the original pointer q. It stays unchanged.

To make the code working you have to pass the pointer by reference (indirectly through a pointer to it). In this case the function will look like

void rr2(struct d **p) {
    *p = malloc(sizeof (struct d ));
    ( *p )->f = 22;
}

and be called like

rr2( &q );

As for this code snippet

free(g);
rr2(g);
printf("[%i]", g->f);

then it just invokes undefined behavior because in this statement

printf("[%i]", g->f);

there is an access to already freed memory.

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