【发布时间】:2012-10-30 15:17:34
【问题描述】:
我决定在这个程序中使用结构来保持它的组织性,所以我现在有一个结构链。我的问题是我是否必须 malloc 另一个结构中的结构。例如:
typedef struct OnlineS {
struct BBSIS *bbsi;
struct BBVIS *bbvi;
struct VBVIS *vbvi;
} *OnlineP;
typedef struct BBSIS{
struct FirstFitS *ff;
struct BestFitS *bf;
struct NextFitS *nf;
int itemNum;
int binNum;
int binMin;
int binMax;
int *items;
}*BBSIP;
等等,我的声明和 malloc 会是什么样子?
OnlineP on = malloc(sizeof (struct OnlineS));
on->bbsi = malloc(sizeof (struct BBSIS));
on->bbsi->bf = malloc(sizeof (struct BestFitS));
on->bbsi->nf = malloc(sizeof (struct NextFitS));
on->bbsi->ff = malloc(sizeof (struct FirstFitS));
on->bbvi = malloc(sizeof (struct BBVIS));
on->bbvi->bf = malloc(sizeof (struct BestFitS));
//ETC
【问题讨论】:
-
作为记录,您确实需要检查
malloc的返回值是否为零。不这样做会导致不可靠。