c 中的动态分配不起作用
Dynamic Alocation in c not working
每次我尝试 运行 情况 1 时,我都会退出 (1)(如果分配中存在一些错误)。我不知道为什么,我能得到一些帮助吗?
#include <stdio.h>
#include <stdlib.h>
typedef struct hospede{ //guest
int numreg; //register num
char nome[80];
int acompanhante; //n of ppl
int dias; //time to stay
estado tabela;
}hospede;
typedef struct quarto{ //room
int num; //num do quarto
char categoria; //[S] ou [F]
char status; //[L] ou [O]
estado stats;
}quarto;
void alocaHospede(hospede **hosp, int tam);
void alocaQuartos(quarto **quar, int tam);
void geraQuartos(quarto *quar);
void checkIn(hospede *hosp, int tam);
int main()
{
hospede *hpd = NULL;
quarto *qrt = NULL;
quarto quart;
int qtd = 0;
char op;
int x;
qrt = &quart;
geraQuartos(qrt);
do{
printf("\n1-CheckIn\n>");
scanf("%i", &x);
fflush(stdin);
switch(x){
case 1:
alocaHospede(&hpd, qtd+1);
checkIn(hpd, qtd);
}
printf("\nDeseja continuar? \n");
scanf("%c", &op);
fflush(stdin);
}while(op!='n' && op!='N');
return 0;
}
void checkIn(hospede *hosp, int tam){
printf("\nwork\n");
}//checkIn
void alocaHospede(hospede **hosp, int tam){
*hosp = (hospede*)realloc(*hosp, tam*sizeof(hospede));
if(*hosp == NULL)
exit(1);
}//aloca hospede
void alocaQuartos(quarto **quar, int tam){
if((*quar = (quarto *) realloc(*quar, tam * sizeof(quarto)))== NULL)
exit(1);
}//alocaQuartos
void geraQuartos(quarto *quar){
int i;
for(i=0;i<15;i++,quar++){
(*quar).num = i+1;
}
}//geraQuartos
OBS:
我删除了一些尚未使用的结构和联合来缩短代码,我也会对房间进行分配,我认为这是同样的问题。
geraQuartos(qrt) 导致堆栈溢出,因为 quart 只有 1 个条目长度,但您在函数内写入第 1 ~ 15 个条目。
堆栈溢出时可能会发生任何奇怪的事情。
geraQuartos
中的 for
循环将其参数视为指向包含 15 个 quartos
结构的数组的指针。但它只是一个指向一个结构的指针,所以它写在对象的边界之外。这导致了未定义的行为,在本例中它覆盖了 hpd
,所以它不是 NULL
。这会导致 realloc
失败,因为第一个参数不是 NULL
或之前由 malloc
.
返回的指针
将 quart
的声明更改为:
quarto quart[15];
然后做:
geraQuartos(quart);
每次我尝试 运行 情况 1 时,我都会退出 (1)(如果分配中存在一些错误)。我不知道为什么,我能得到一些帮助吗?
#include <stdio.h>
#include <stdlib.h>
typedef struct hospede{ //guest
int numreg; //register num
char nome[80];
int acompanhante; //n of ppl
int dias; //time to stay
estado tabela;
}hospede;
typedef struct quarto{ //room
int num; //num do quarto
char categoria; //[S] ou [F]
char status; //[L] ou [O]
estado stats;
}quarto;
void alocaHospede(hospede **hosp, int tam);
void alocaQuartos(quarto **quar, int tam);
void geraQuartos(quarto *quar);
void checkIn(hospede *hosp, int tam);
int main()
{
hospede *hpd = NULL;
quarto *qrt = NULL;
quarto quart;
int qtd = 0;
char op;
int x;
qrt = &quart;
geraQuartos(qrt);
do{
printf("\n1-CheckIn\n>");
scanf("%i", &x);
fflush(stdin);
switch(x){
case 1:
alocaHospede(&hpd, qtd+1);
checkIn(hpd, qtd);
}
printf("\nDeseja continuar? \n");
scanf("%c", &op);
fflush(stdin);
}while(op!='n' && op!='N');
return 0;
}
void checkIn(hospede *hosp, int tam){
printf("\nwork\n");
}//checkIn
void alocaHospede(hospede **hosp, int tam){
*hosp = (hospede*)realloc(*hosp, tam*sizeof(hospede));
if(*hosp == NULL)
exit(1);
}//aloca hospede
void alocaQuartos(quarto **quar, int tam){
if((*quar = (quarto *) realloc(*quar, tam * sizeof(quarto)))== NULL)
exit(1);
}//alocaQuartos
void geraQuartos(quarto *quar){
int i;
for(i=0;i<15;i++,quar++){
(*quar).num = i+1;
}
}//geraQuartos
OBS:
我删除了一些尚未使用的结构和联合来缩短代码,我也会对房间进行分配,我认为这是同样的问题。
geraQuartos(qrt) 导致堆栈溢出,因为 quart 只有 1 个条目长度,但您在函数内写入第 1 ~ 15 个条目。 堆栈溢出时可能会发生任何奇怪的事情。
geraQuartos
中的 for
循环将其参数视为指向包含 15 个 quartos
结构的数组的指针。但它只是一个指向一个结构的指针,所以它写在对象的边界之外。这导致了未定义的行为,在本例中它覆盖了 hpd
,所以它不是 NULL
。这会导致 realloc
失败,因为第一个参数不是 NULL
或之前由 malloc
.
将 quart
的声明更改为:
quarto quart[15];
然后做:
geraQuartos(quart);