简体   繁体   English

C:初始化结构并将其分配给 function 参数中的指针

[英]C: Initializing a struct and assigning it to pointer in function parameter

Because of an assignment at Uni, I face the weird problem of having to initialize a struct to a pointer provided as a function parameter.由于在 Uni 的分配,我面临着一个奇怪的问题,即必须将结构初始化为作为 function 参数提供的指针。 Everything appears fine inside of that init function, but once I try to access the objects's values inside a different function, they just appear to be empty.在该 init function 中,一切看起来都很好,但是一旦我尝试访问不同 function 中的对象值,它们似乎就是空的。

As I'm horrible at explaining things, I got a small example with actual and expected output:由于我很难解释事情,我得到了一个实际和预期 output 的小例子:

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

struct foo {
    int bar;
};

void init_foo(struct foo* f) {
    f = malloc(sizeof(struct foo));
    f->bar = 5;
    printf("bar0: %d\n", f->bar);
}

void print_foo(struct foo* f) {
    printf("bar1: %d\n", f->bar);
}

int main() {
    struct foo f;
    init_foo(&f);
    print_foo(&f);
}

Acutal output:实际 output:

bar0: 5
bar1: 0

Expected output:预期 output:

bar0: 5
bar1: 5

I haven't dealt with C all that much before, so if someone could provide an explanation and/or a solution to this, I'd be very thankful.我之前没有处理过 C 这么多,所以如果有人可以对此提供解释和/或解决方案,我将非常感激。

In main() , f is an allocated version of the foo struct.main()中, ffoo结构的分配版本。 You passed a pointer to that allocated structure to init_foo() .您将指向该分配结构的指针传递给init_foo()

There is no need for f to be malloc() 'ed inside of init_foo() .不需要finit_foo()内部进行malloc()编辑。 f already points to an allocated structure. f已经指向一个分配的结构。

I think if you just remove the malloc() , it should work.我认为如果您只是删除malloc() ,它应该可以工作。

You need to get rid of the malloc() , it doesn't belong since main() already allocated memory for its local f variable.您需要摆脱malloc() ,它不属于,因为main()已经为其局部f变量分配了 memory 。 init_foo() just needs to populate the existing memory. init_foo()只需要填充现有的 memory。 By calling malloc() , you are changing the foo* pointer inside of init_foo() to point at different memory, ignoring the original memory that was passed in.通过调用malloc() ,您将更改init_foo()内部的foo*指针以指向不同的 memory,忽略传入的原始 memory。

Try this instead:试试这个:

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

struct foo {
    int bar;
};

void init_foo(struct foo* f) {
    f->bar = 5;
    printf("bar0: %d\n", f->bar);
}

void print_foo(struct foo* f) {
    printf("bar1: %d\n", f->bar);
}

int main() {
    struct foo f;
    init_foo(&f);
    print_foo(&f);
}

On the other hand, if you want init_foo() to allocate memory for the struct, then do this instead:另一方面,如果您希望init_foo()为结构分配 memory,请改为执行以下操作:

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

struct foo {
    int bar;
};

void init_foo(struct foo** f) {
    *f = malloc(sizeof(struct foo));
    if (f) {
        (*f)->bar = 5;
        printf("bar0: %d\n", (*f)->bar);
    }
}

void free_foo(struct foo* f) {
    free(f);
}

void print_foo(struct foo* f) {
    printf("bar1: %d\n", f->bar);
}

int main() {
    struct foo *f;
    init_foo(&f);
    if (f) {
        print_foo(f);
        free_foo(f);
    }
}

Or this:或这个:

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

struct foo {
    int bar;
};

struct foo* init_foo() {
    struct foo *f = malloc(sizeof(struct foo));
    if (f) {
        f->bar = 5;
        printf("bar0: %d\n", f->bar);
    }
    return f;
}

void free_foo(struct foo* f) {
    free(f);
}

void print_foo(struct foo* f) {
    printf("bar1: %d\n", f->bar);
}

int main() {
    struct foo *f = init_foo(&f);
    if (f) {
        print_foo(f);
        free_foo(f);
    }
}

Don't malloc inside init_foo ;不要mallocinit_foo space was already allocated by the caller.调用者已经分配了空间。 So you're filling up the space you malloc ed, but not the space pointed to by the pointer passed to the function.因此,您正在填充malloc编辑的空间,而不是传递给 function 的指针指向的空间。

You are eclipsing struct foo* f.您正在使 struct foo* f 黯然失色。

You have a stack based structure struct foo f defined in main.您在 main 中定义了一个基于堆栈的结构 struct foo f。 You pass a pointer to it, to the init_foo function.您将指向它的指针传递给 init_foo function。 However you then immediately replace that structure pointer with an allocated structure, and proceed to fill that structure in. The original struct foo* f which was passed into the init_foo function was therefore not modified.但是,您随后立即用分配的结构替换该结构指针,并继续填充该结构。传递给 init_foo function 的原始 struct foo* f 因此没有被修改。

You then leak the memory by exiting the init_foo function and then you print the uninitilized structure.然后,您通过退出 init_foo function 来泄漏 memory,然后打印未初始化的结构。

You do not need the memory allocation, remove it all.您不需要 memory 分配,将其全部删除。 Simply initialize directly into the struct foo* f you pass into the init_foo function.只需直接初始化到结构 foo* f 中,然后传入 init_foo function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM