gpt4 book ai didi

c - 将指针的结构数组作为函数参数传递?

转载 作者:太空宇宙 更新时间:2023-11-04 07:17:17 24 4
gpt4 key购买 nike

如果我直接使用结构数组的名称进行分配,但不是来自函数参数,则代码有效。否则返回内存错误。

typedef struct COORD
{
int xp;
int yp;
} coord;

coord** xy;

void allocate(coord** COORD)
{
int i;

//allocate COORD[500][460]
COORD = (coord**)malloc(sizeof(coord*)*500);
for(i=0; i<500; i++)
{
COORD[i] = (coord*)malloc(sizeof(coord)*460);
}

// freeing
for (i=0; i<500; i++) free(COORD[i]);
free(COORD);
}
//function call: allocate(xy);
//That is the code that leeds to the error

仅使用 xy 而不是 COORD 就可以了。我很想知道为什么那不起作用。

最佳答案

您在这里混淆了各种编码风格。目前尚不清楚您到底想要实现什么。根据您的任务选择一个。

临时缓冲区

您需要一个大的临时缓冲区,它应该分配在堆上并且不需要从外部看到。只需创建一个局部变量:

void do_stuff(int w, int h)
{
coord **p;
int i;

p = malloc(h * sizeof(*p));
for (i = 0; i < h; i++) p[i] = malloc(w * sizeof(**p));;

// do stuff

for (i = 0; i < h; i++) free(p[i]);
free(p);
}

分配内存以供进一步使用

您想分配您的客户端代码可以使用的存储空间。然后提供两个函数,一个分配内存,一个释放内存:

coord **create(int w, int h)
{
coord **p;
int i;

p = malloc(h * sizeof(*p));
for (i = 0; i < h; i++) p[i] = malloc(w * sizeof(**p));

return p;
}

void destroy(coord **p, int h)
{
int i;

for (i = 0; i < h; i++) free(p[i]);
free(p);
}

然后您的客户端代码可以在这些调用之间使用内存:

coord **p = create(500, 460);

// do stuff

drestroy(p, 500);

(请注意,您必须将高度传递给 destroy,这有点不幸。创建一个包装器结构来保存有关宽度和高度以及指针的信息可能会更清晰。)

为全局变量分配内存

你有一个全局指针实例。然后你的函数总是在那个指针上运行,你不需要任何关于它的进一步信息(维度除外):

coord **global = NULL;

void destroy_global(int h)
{
int i;

for (i = 0; i < h; i++) free(global[i]);
free(global);
global = NULL;
}

void create_global(int w, int h)
{
int i;

if (global != NULL) free_global();

global = alloc(h * sizeof(*global));
for (i = 0; i < h; i++) global[i] = malloc(w * sizeof(**global));
}

请注意,您应该包括 <stdlib.h>对于所有内存功能和 NULL宏。

附录 根据您的评论,您想为位图分配内存。这是上面的选项 2。

我建议创建一个对象结构。您可以将指针 v 传递给该结构作为一堆函数的句柄。您可以使用返回该句柄的函数创建对象。

下面是位图对象的粗略设计。

typedef struct Pixel Pixel;
typedef struct Bitmap Bitmap;

struct Pixel {
uint8_t r, g, b;
};

struct Bitmap {
int height;
int width;
Pixel **pixel;
};

Bitmap *bitmap_new(int w, int h)
{
Bitmap *bmp = malloc(sizeof(*bmp));
int i;

bmp->height = h;
bmp->width = w;
bmp->pixel = malloc(h * sizeof(*bmp->pixel));
for (i = 0; i < h; i++) {
bmp->pixel[i] = malloc(w * sizeof(**bmp->pixel));
}

return p;
}

void bitmap_delete(Bitmap *bmp)
{
int i;

for (i = 0; i < h; i++) free(bmp->pixel[i]);
free(bmp->pixel);
free(bmp);
}



Bitmap *bitmap_read(const char *fn)
{
Bitmap *bmp;
FILE *f = fopen(fn, "rb");

// read and allocate
return bmp;
}

void bitmap_blank(Bitmap *bmp, int r, int g, int b)
{
for (i = 0; i < bitmap->height; i++) {
for (j = 0; j < bitmap->width; j++) {
bmp->pixel[i][j].r = r;
bmp->pixel[i][j].g = g;
bmp->pixel[i][j].b = b;
}
}
}

void bitmap_mirror_x(Bitmap *bmp)
{
// do stuff
}

int bitmap_write(Bitmap *bmp, const char *fn)
{
FILE *f = fopen(fn, "rb");

// write bitmap to file
return 0;
}

设计类似于FILE *的界面: fopen给你一个句柄(或 NULL ;上面的代码中省略了错误检查)和 fread , fprintf , fseek和 family 将指向文件的指针作为参数。最后拨通fclose关闭磁盘上的文件并释放所有资源 fopen已声明。

关于c - 将指针的结构数组作为函数参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23634256/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com