malloc 不适用于列表的第二个元素
malloc doesn't work for second element of list
该函数应该创建一个结构链表,我在调试器中看到它为第一个元素分配内存,但是在为第二个元素分配内存时程序崩溃了。
struct Tipologia{
int mq;
int prezzo;
int n;
char descrizione[100];
};
struct Nodo{
struct Tipologia t;
struct Nodo *next;
};
typedef struct Nodo* NODO;
struct Tipologia creaTipologia(){
struct Tipologia t;
printf("MQ?\n");
scanf("%d", &(t.mq));
printf("PREZZO?\n");
scanf("%d", &(t.prezzo));
printf("DISPONIBILI?\n");
scanf("%d", &(t.n));
printf("DESCRIZIONE?\n");
int c;
while ( (c = getc(stdin)) != EOF && c != '\n');
fgets((t.descrizione), 100, stdin);
return t;
}
NODO costruisciPalazzo(){
int continua;
NODO h= NULL;
printf("VUOI COSTRUIRE UN APPARTAMENTO? 1 SI, 0 NO\n");
scanf("%d", &continua);
if(continua){
NODO n= malloc(sizeof(NODO));
h= n;
n->t= creaTipologia();
n->next= NULL;
printf("VUOI COSTRUIRE UN APPARTAMENTO? 1 SI, 0 NO\n");
scanf("%d", &continua);
while(continua){
NODO nodo= malloc(sizeof(NODO));
n->next= nodo;
n= nodo;
n->t= creaTipologia();
printf("VUOI COSTRUIRE UN APPARTAMENTO? 1 SI, 0 NO\n");
scanf("%d", &continua);
}
n->next= NULL;
}
return h;
}
我一直在按照教授的指示进行操作,但它一直在崩溃,但没有给出任何错误来解释实际发生的情况。它似乎适用于我的同学,我们无法弄清楚问题是什么。请帮忙
简单地通过执行 malloc(sizeof(struct Nodo)) 而不是 malloc(sizeof(NODO)) 解决了这个问题,因为对于 NODO 它将只分配指针,而对于 struct Nodo 它分配整个元素
问题是您使用的 typedef
隐藏了 NODO
是指针这一事实。这很糟糕,因为它会造成令人困惑的情况,您期望类型具有特定大小并使用特定语法,但实际上这是完全不同的东西。
例如,你必须这样做:
h= n;
n->t= creaTipologia();
n->next= NULL;
令人困惑的是 h
和 n
都没有明确声明为指针,但你必须使用箭头符号。
我的建议是您要么完全删除 typedef
并在代码中使用 struct nodo
,或者至少删除 typedef
中的指针。对其他结构做同样的事情以保持一致性:
typedef struct {
int mq;
int prezzo;
int n;
char descrizione[100];
} TIPOLOGIA;
typedef struct Nodo {
TIPOLOGIA t;
struct Nodo *next;
} NODO;
您还可以使用对象作为其大小的参考来简化您的 malloc
。例如:
NODO *h = malloc(sizeof *h);
这样就避免了调用malloc
时需要指定h
的类型。重复使用也更好。
该函数应该创建一个结构链表,我在调试器中看到它为第一个元素分配内存,但是在为第二个元素分配内存时程序崩溃了。
struct Tipologia{
int mq;
int prezzo;
int n;
char descrizione[100];
};
struct Nodo{
struct Tipologia t;
struct Nodo *next;
};
typedef struct Nodo* NODO;
struct Tipologia creaTipologia(){
struct Tipologia t;
printf("MQ?\n");
scanf("%d", &(t.mq));
printf("PREZZO?\n");
scanf("%d", &(t.prezzo));
printf("DISPONIBILI?\n");
scanf("%d", &(t.n));
printf("DESCRIZIONE?\n");
int c;
while ( (c = getc(stdin)) != EOF && c != '\n');
fgets((t.descrizione), 100, stdin);
return t;
}
NODO costruisciPalazzo(){
int continua;
NODO h= NULL;
printf("VUOI COSTRUIRE UN APPARTAMENTO? 1 SI, 0 NO\n");
scanf("%d", &continua);
if(continua){
NODO n= malloc(sizeof(NODO));
h= n;
n->t= creaTipologia();
n->next= NULL;
printf("VUOI COSTRUIRE UN APPARTAMENTO? 1 SI, 0 NO\n");
scanf("%d", &continua);
while(continua){
NODO nodo= malloc(sizeof(NODO));
n->next= nodo;
n= nodo;
n->t= creaTipologia();
printf("VUOI COSTRUIRE UN APPARTAMENTO? 1 SI, 0 NO\n");
scanf("%d", &continua);
}
n->next= NULL;
}
return h;
}
我一直在按照教授的指示进行操作,但它一直在崩溃,但没有给出任何错误来解释实际发生的情况。它似乎适用于我的同学,我们无法弄清楚问题是什么。请帮忙
简单地通过执行 malloc(sizeof(struct Nodo)) 而不是 malloc(sizeof(NODO)) 解决了这个问题,因为对于 NODO 它将只分配指针,而对于 struct Nodo 它分配整个元素
问题是您使用的 typedef
隐藏了 NODO
是指针这一事实。这很糟糕,因为它会造成令人困惑的情况,您期望类型具有特定大小并使用特定语法,但实际上这是完全不同的东西。
例如,你必须这样做:
h= n;
n->t= creaTipologia();
n->next= NULL;
令人困惑的是 h
和 n
都没有明确声明为指针,但你必须使用箭头符号。
我的建议是您要么完全删除 typedef
并在代码中使用 struct nodo
,或者至少删除 typedef
中的指针。对其他结构做同样的事情以保持一致性:
typedef struct {
int mq;
int prezzo;
int n;
char descrizione[100];
} TIPOLOGIA;
typedef struct Nodo {
TIPOLOGIA t;
struct Nodo *next;
} NODO;
您还可以使用对象作为其大小的参考来简化您的 malloc
。例如:
NODO *h = malloc(sizeof *h);
这样就避免了调用malloc
时需要指定h
的类型。重复使用也更好。