该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
struct Stu{
char name[20];
int age;
int no;
};
struct Test{
struct Stu *st;
char *buf;
char *buf2;
};
struct Test *malloc_test()
{
struct Test *test= (struct Test*)malloc(sizeof(struct Test));
if(test == NULL){
return NULL;
}
test->st = (struct Stu*)malloc(sizeof(struct Stu));
if(test->st == NULL){
free(test);
test= NULL;
return NULL;
}
test->buf = (char *)malloc(20 * sizeof(char));
if(test->buf == NULL){
free(test->st);
test->st = NULL;
free(test);
test= NULL;
return NULL;
}
test->buf2 = (char *)malloc(10 * sizeof(char));
if(test->buf2 == NULL){
free(test->buf);
free(test->st);
test->buf = NULL;
test->st = NULL;
free(test);
test= NULL;
return NULL;
}
return test;
}
以上是我想的第一种方法 先申请后释放 可是这样 如果以后在结构体中添加一个或删除一个成员时 对代码的改动比较大 请问狼友们有啥子好的办法