简体   繁体   English

结构上malloc的C用法

[英]C usage of malloc on struct

I'm trying to use malloc on a struct called image. 我正在尝试在名为image的结构上使用malloc。 The function is: 该函数是:

void image_init(struct image* img, int w, int h) {
    img = malloc(sizeof(struct image));
    (*img).w = w;
    (*img).h = h;
}

And the function to free the image again is: 再次释放图像的功能是:

void image_destroy(struct image* img) {
    free(img);
}

But whenever I try to free the malloc-ed data I'm getting the error that the adress I'm trying to free was not malloc-ed before. 但是,每当我尝试释放由malloc分配的数据时,我都会得到一个错误,即我尝试释放的地址之前没有进行malloc分配。

The struct of image is: 图像的结构为:

struct image {
    int w,h;
    int* data;
}

I call the functions with: 我用以下函数调用这些函数:

struct image img;
image_init(&img,100,100);
image_destroy(&img);

First here 首先在这里

void image_init(struct image* img, int w, int h) {
    img = malloc(sizeof(struct image));
    (*img).w = w;
    (*img).h = h;
}

img is a copy of the original pointer passed to it, hence the code on first line inside function has no effect, since it makes the copy point somewhere - not the original object. img是传递给它的原始指针的副本 ,因此函数内第一行的代码无效,因为它使复制点指向某个位置-而不是原始对象。

This: 这个:

struct image img;
image_init(&img,100,100);
image_destroy(&img);

doesn't make sense either (assuming you expected img to point somewhere after call to init ). 也没有任何意义(假设您希望img在调用init之后指向某处)。 img isn't a pointer, how you expect it to point somewhere? img不是指针,您如何期望它指向某个地方?


One way you could solve this could be 解决这个问题的一种方法可能是

struct image *img = image_init(100,100);

where 哪里

struct image* image_init(int w, int h) {
    img = malloc(sizeof(struct image));
    (*img).data = NULL;
    (*img).w = w;
    (*img).h = h;
    return img;
}

Don't forget to call free on the pointer returned by above function - you will need to free data pointer separately too, in case you allocate it too. 不要忘了打电话给free对上述函数返回的指针-你需要免费data指针分开过,在你分配太案件。


NOTE : My best guess (if you also can't change prototype) is you want something like this: 注意 :我最好的猜测(如果您也不能更改原型)是您想要这样的东西:

void image_init(struct image* img, int w, int h) {
    img->data = malloc(sizeof(int) * w * h);
    img->w = w;
    img->h = h;
}

destroy: 破坏:

void image_destroy(struct image* img) {
    free(img->data);
    free(img);
}

In main 在主要

struct image* img = malloc(sizeof(struct image));
image_init(img, 100, 100);
image_destroy(img);

PS. PS。 Or if you want the usage of those functions remain as you had in main go with the answer of Johnny Mopp. 或者,如果您希望保留这些功能的用法,就像您对Johnny Mopp的回答一样。

I think your assignment is not to allocate the img but the data ptr: 我认为您的任务不是分配img而是分配data ptr:

void image_init(struct image* img, int w, int h) {
    img->data = malloc(sizeof(int) * w * h);// Check for failure...
    img->w = w;
    img->h = h;
}
void image_destroy(struct image* img) {
    free(img->data);
}
struct image img;
image_init(&img,100,100);
image_destroy(&img);

allocates an image struct on your stack. 在堆栈上分配图像结构。 Why are you trying to malloc one? 您为什么要尝试分配一个? If you really want one on the heap then do 如果您真的想要一个堆,那就去做

    struct image * image_init( int w, int h) {
        struct image *img = malloc(sizeof(struct image));
        img->w = w;
        img->h = h;
        return img;
    }
...  
    struct image *img = image_init(100,100);

Note the more idiomatic '->' use 注意更多惯用的“->”用法

if you want the one on the stack then do 如果您想将其放在堆栈上,请执行

void image_init(struct image* img, int w, int h) {
    img->w = w;
    img->h = h;
}

您需要将malloc强制转换为struct,因为malloc的返回值是void类型。

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

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