malloc给结构体分配空间的问题
struct image
{
struct header *info;
unsigned char **data;
};
struct image *newimage(int nr,int nc)
{
struct image *x;
x = (struct image *) malloc( sizeof (struct image) );
//这里已经分配过了,为什么还要分别给info和data分配?详细的解释一下。
x->info = (struct header *)malloc( sizeof(struct header) );
x->data = (unsigned char **)malloc(sizeof(unsigned char *)*nr);
//这里已经给data分配了,为什么还要给data[0]分配,如果需要时不时还要给data[i](i=1,2,3...)
//分配?再解释一下。
x->data[0] = (unsigned char *)malloc (nr*nc);
......
return x;
}