struct image {
struct buff *buf;
unsigned char **data;
};
struct image *newimage(int nr,int nc) {
struct image *img;
img = (struct image *) malloc( sizeof (struct image) );
img->buf = (struct header *)malloc( sizeof(struct buff) );
img->data = (unsigned char **)malloc(sizeof(unsigned char *)*nr);
img->data[0] = (unsigned char *)malloc (nr*nc);
......
return img;
}
指针会指向一块内存,指针本身也是要占用内存的,所以先给img结构体开辟空间,这样里面的指针成员就有了内存空间。然后再给指针成员分配空间。
free的话,先把成员free后再把结构体给free掉。