[英]C- an ADT that keeps crashing
I'm trying to create an ADT , first of all, here is the basic information about it: 我正在尝试创建ADT,首先,这是有关它的基本信息:
typedef struct orange_t* Orange;
typedef enum {
JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC,
} Month;
struct orange_t {
short size;
Month expirationMonth;
char** foodCompanies;
int maxNumberOfFoodCompanies;
int sellingPrice;
};
now im trying to create a function that will "create" new orange like that: 现在,我正在尝试创建一个将“创建”新橙色的函数,如下所示:
Orange orangeCreate(short size,Month expirationMonth
,int maxNumberOfFoodCompanies,int sellingPrice)
{
Orange new_orange=(Orange)malloc(sizeof(struct orange_t));
if(new_orange==NULL)
{
return NULL;
}
if((sellingPrice<0)||(size>256||size<1)||(maxNumberOfFoodCompanies<0)||(expirationMonth>12)
||(expirationMonth<1))
{
return NULL;
}
new_orange->sellingPrice=sellingPrice;
new_orange->maxNumberOfFoodCompanies=maxNumberOfFoodCompanies;
new_orange->expirationMonth=expirationMonth;
new_orange->size=size;
for(int i=0;i<new_orange->maxNumberOfFoodCompanies;i++)
{
new_orange->foodCompanies[i]=NULL;
}
return new_orange;
}
when I tried to check the function with the simple main()
: 当我尝试使用简单的main()
检查功能时:
int main()
{
Orange orange=orangeCreate(3,JAN,10,4);
printf("the size is %d\n",orange->size);
orangeDestroy(orange);
return 0;
}
the program keeps crashing, I guess I didn't change the values in orange as I should, and they might still be NULL
. 程序不断崩溃,我想我没有按应有的方式更改橙色值,它们可能仍然是NULL
。 where did I go wrong here? 我在哪里错了?
JAN
is 0. if expirationMonth<1
you are returning NULL
from orangeCreate
. JAN
为0。如果expirationMonth<1
,则从orangeCreate
返回NULL
。
Also: you are not checking the return value of orangeCreate
. 另外:您没有检查orangeCreate
的返回值。 And you are leaking memory in orangeCreate
when you are just returning NULL
without free
ing the allocated new_orange
. 当您仅返回NULL
而不free
分配的new_orange
时,就会在orangeCreate
中泄漏内存。
The problem is that you are not allocating memory for char** foodCompanies;
问题在于您没有为char** foodCompanies;
分配内存char** foodCompanies;
While allocating memory here 在这里分配内存
Orange new_orange=malloc(sizeof(struct orange_t));
You have allocated space for the pointer variable char** foodCompanies;
您已经为指针变量char** foodCompanies;
分配了空间char** foodCompanies;
, but have not allocated space where it would point to. ,但尚未分配指向的空间。
You have to allocate some memory and store the address of it to new_orange->foodCompanies
before accessing it. 您必须分配一些内存并将其地址存储在new_orange->foodCompanies
然后再访问它。
Possible fix: 可能的解决方法:
Orange orangeCreate(short size,Month expirationMonth
,int maxNumberOfFoodCompanies,int sellingPrice)
{
Orange new_orange=malloc(sizeof(struct orange_t));
if(new_orange==NULL)
{
return NULL;
}
if((sellingPrice<0)||(size>256||size<1)||(maxNumberOfFoodCompanies<0)||(expirationMonth>12)
||(expirationMonth<1))
{
return NULL;
}
new_orange->sellingPrice=sellingPrice;
new_orange->maxNumberOfFoodCompanies=maxNumberOfFoodCompanies;
new_orange->expirationMonth=expirationMonth;
new_orange->size=size;
new_orange->foodCompanies=malloc(sizeof(char*)*new_orange->maxNumberOfFoodCompanies); /* add this line */
for(int i=0;i<new_orange->maxNumberOfFoodCompanies;i++)
{
new_orange->foodCompanies[i]=NULL;
}
return new_orange;
}
Note: it is discouraged to cast the result of malloc()
in C. 注意:不建议在C中malloc()
的结果。
c - Do I cast the result of malloc? c-我是否强制转换malloc的结果? - Stack Overflow - 堆栈溢出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.