我写了一个程序,使用结构和动态内存分配。我想得到帮助,了解为什么如果我按4(这意味着退出菜单),我得到的错误:检测到堆腐 eclipse :在0x00845EE8的正常块(#76)之后。它被链接到函数deletelist()
。我真的不明白为什么会发生这种情况,以及如何解决这个问题。
这就是代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PRO_OP 1
#define CON_OP 2
#define PRINT_OP 3
#define EXIT_OP 4
#define STR_LEN 50
#define MAX_LIST_LENGTH 10
typedef struct reasonList
{
char* listName;
char* reasons[MAX_LIST_LENGTH];
int numReasons;
} reasonList;
void initList(reasonList* list, char* name);
void addReason(reasonList* list);
void printList(reasonList list);
int menu(void);
void myFgets(char str[], int n);
void deleteList(reasonList* list);
int main(void)
{
char dillema[STR_LEN] = { 0 };
int op = 0;
reasonList proList;
initList(&proList, "PRO");
reasonList conList;
initList(&conList, "CON");
printf("What is your dillema?\n");
myFgets(dillema, STR_LEN);
while (op != EXIT_OP)
{
op = menu();
switch (op)
{
case(PRO_OP):
addReason(&proList);
break;
case(CON_OP):
addReason(&conList);
break;
case(PRINT_OP):
printf("Your dillema:\n");
printf("%s\n\n", dillema);
printList(proList);
printList(conList);
break;
case(EXIT_OP):
deleteList(&proList);
deleteList(&conList);
break;
}
}
printf("Good luck!\n");
getchar();
return 0;
}
/*
Function will initialize a reason list
input: the list to init, and its name
output: none
*/
void initList(reasonList* list, char* listName)
{
list->listName = (char*) malloc(sizeof(char));
strcpy(list->listName, listName);//equal to PRO or CON
for (int i = 0; i < MAX_LIST_LENGTH; i++)
{
list->reasons[i] = (char*)malloc(sizeof(char) * STR_LEN);
if (list->reasons[i] == NULL)
{
printf("no memmory left\n");
exit(1);
}
strcpy(list->reasons[i], "");
}
list->numReasons = 0;
}
/*
Function will add a reason to the list
input: the list to add to and its name
output: none
*/
//
void addReason(reasonList* list)
{
char* newReason = (char*)malloc(sizeof(char) * STR_LEN);
printf("Enter a reason to add to the list %s:\n", list->listName);
myFgets(newReason, STR_LEN);
if (list->numReasons >= MAX_LIST_LENGTH)//if no memory left
{
//free(newReason);
return;
}
free(list->reasons[list->numReasons]);
list->reasons[list->numReasons] = newReason;
list->numReasons++;
list->reasons[list->numReasons] = NULL;
}
/*
Function will print a list of reasons
input: the list
output: none
*/
void printList(reasonList list)
{
printf("list %s\n--------\n", list.listName);
for (int i = 0; i < list.numReasons; i++)
{
printf("%s", list.reasons[i]);
printf("\n");
}
printf("\n");
}
/*
Function shows menu and returns user's choice
input: none
output: user's choice
*/
int menu(void)
{
int op = 0;
printf("Choose option:\n");
printf("%d - Add PRO reason\n", PRO_OP);
printf("%d - Add CON reason\n", CON_OP);
printf("%d - Print reasons\n", PRINT_OP);
printf("%d - Exit\n", EXIT_OP);
scanf("%d", &op);
while (op < PRO_OP || op > EXIT_OP)
{
printf("Invalid option. Try again: ");
scanf("%d", &op);
}
getchar(); // clean buffer
return op;
}
/*
Function will delete a list
input: the list to delete
output: none
*/
void deleteList(reasonList* list)
{
for (int i = 0; i < list->numReasons; i++)
{
free(list->reasons[i]);
list->reasons[i] = NULL; // set pointer to NULL
}
free(list->listName);
list->listName = NULL; // set pointer to NULL
}
/*
Function will perform the fgets command and also remove the newline
that might be at the end of the string - a known issue with fgets.
input: the buffer to read into, the number of chars to read
*/
void myFgets(char str[], int n)
{
fgets(str, n, stdin);
str[strcspn(str, "\n")] = 0;
}
1条答案
按热度按时间kwvwclae1#
我无法重现segfault,但valgrind抱怨
initList()
:malloc(sizeof(char))
分配1个字节(\0
用于空字符串),而您需要strlen(listName) + 1
。无关,为了消除内存泄漏,请注意在
initList()
中为MAX_LIST_LENGTH
reasons
分配空间,但在deleteList()
中只释放list->numReasons
。建议您将其留给addReasons()
来分配空间:如果你想删除(和重用)
reasons[i]
,那么在initList()
中,你想初始化list->reasons[i] = NULL
,我建议(为了对称)创建一个deleteReason()
函数,然后在deleteList()
中使用。另一种有效的设计(因为您的
reasons
是固定大小的)是将所有reasons
分配到initList()
中(就像您所做的那样)。在addReaon()
中重用分配的字符串(没有malloc()
或free()
)。在deleteList()
免费的所有MAX_LIST_LENGTH
的原因。最好检查
malloc()
的返回值。不要从
malloc()
强制转换void *
。更喜欢传递一个与
sizeof
类型相反的变量。在上面的例子中,使用sizeof *list->listName
或sizeof *listName; that said
size(char)is defined as
1代替
sizeof(char)`,所以我总是省略它。