提问人:nini 提问时间:12/22/2022 最后编辑:nini 更新时间:12/22/2022 访问量:20
free() 函数总是崩溃
free() function always crash
问:
所以,我所有的工作都很好,但只有免费功能被禁用 我很确定问题出在我使用 malloc 的方式上
// This is the head
typedef struct Contact {
char*firstName;
char* lastName;
char* phoneNum;
struct Contact* next
} Contact;
这就是我分配和创建新联系人的方式 只是我如何尝试使用 malloc 进行分配的示例
Contact* createNewCont(char* fName, char* lName, char* phoNum)
{
Contact* newCont;
newCont = malloc(sizeof (newCont));
//allocate and check all the string in the contact
newCont->firstName = malloc(strlen(fName));
printf ("%d",strlen(fName));
if(newCont->firstName == NULL)
return NULL;
strcpy(newCont->firstName,fName);
}
问题发生在这里 我尝试释放特定的联系人
int freeParam(Contact** head,char* fName, char* lName)
{
Contact* temp = *head;
int isExist = FALSE;
int compVal1, compVal2;
if (temp == NULL)
{
printf("null");
return FALSE;
}
compVal1 = strcmp((*head)->firstName,fName);
compVal2 = strcmp((*head)->lastName,lName);
if (compVal1 == 0 && compVal2 == 0)
{
if (temp->next == NULL)
{
Contact* toRemvoe = *head;
*head = NULL;
return TRUE;
}
Contact* toRemvoe = *head;
(*head) = (*head)->next;
free(toRemvoe->firstName);
free(toRemvoe->lastName);
free(toRemvoe->phoneNum);
free(toRemvoe);
toRemvoe = NULL;
return TRUE;
}
我看到了一些答案,但不太明白
我尝试了几种不同的方法,使用 malloc 进行分配 但我得到了同样的结果
答: 暂无答案
评论
free