单链表结构插入/删除多个空闲
看了这段代码太久了,我变得沮丧,失去了自己弄清楚它的任何机会:(任何人都可以告诉我我在哪里愚蠢?我只是不明白我在哪里双重释放或可能分配错误地(我必须这样做,但是是的)我不断收到 * glibc detectors * free(): invalid next size
我实际上释放的内容比我需要的多,还是我只是没有分配我需要的内容首先分配。--抱歉,缩进错误,无法让该编辑器正确缩进
我有结构:
typedef int boolean;
typedef char * String;
typedef struct {
char name[30];
long ID;
char address[40];
char city[20];
int age;
}Employee;
typedef struct node {
Employee *anEmployee;
struct node *next;
}NODE;
typedef struct {
NODE *head, *tail;
}SLL;
插入函数--SLL(单链表)
void insert(SLL *list, Employee e){
printf("insert");
NODE *temp, *current;
temp = (NODE *)malloc(sizeof(NODE));
assert(temp != NULL);
temp -> anEmployee = (Employee *)malloc(sizeof(Employee *));
assert(temp -> anEmployee != NULL);
strcpy(temp -> anEmployee -> name, e.name);
temp -> anEmployee -> ID = e.ID;
strcpy(temp -> anEmployee -> address, e.address);
strcpy(temp -> anEmployee -> city, e.city);
temp -> anEmployee -> age = e.age;
if (list -> head == NULL) { /* list is empty */
list -> head = list -> tail = temp;
return;
}
else { // list is not empty
list -> tail -> next = temp;
list -> tail = temp;
}
}
在删除函数中删除和释放内存。
boolean delete(SLL *list, String str){
printf("delete");
NODE *temp, *temp2;
if (list -> head == NULL) return FALSE; // list is empty
temp = list -> head;
while ((temp != NULL) && (strcmp(temp -> anEmployee -> name , str) != 0))
temp = temp -> next;
if (temp == NULL) return FALSE; // str is not found in the list
// now temp points to the NODE with str in it. Let us delete it
// from the list
if ( list -> head == temp) { // temp points to the first NODE
if (temp -> next == NULL) { // temp points to the only NODE
list -> head = list -> tail = NULL;
free(temp -> anEmployee);
free(temp);
return TRUE;
}
// temp points to the first NODE but it is not the only NODE
list -> head = temp -> next;
free(temp -> anEmployee);
free(temp);
return TRUE;
}
if (temp -> next == NULL) { // temp points to the last NODE
temp = list -> head;
temp2 = list -> head -> next;
while(temp2 - > next != NULL){
temp = temp->next;
temp2 = temp2 ->next;
}
list -> tail = temp ;
list -> tail -> next = NULL;
free(temp2 -> anEmployee);
free(temp2);
return TRUE;
}
// temp points to some NODE in the middle of the list
temp2 = temp -> next;
while(temp2 - > next != NULL){
temp ->anEmployee = temp2 - > anEmployee //
temp = temp->next;
temp2 = temp2 ->next;
}
temp ->anEmployee = temp2 - > anEmployee
list -> tail = temp ;
list -> tail -> next = NULL;
free(temp2 -> anEmployee);
free(temp2);
return TRUE;
}
Been looking at this code for too long and I am getting gloomy any chance of figuring it out by myself has been lost :( anyone can tell me where am I being stupid? I just don't understand where I am double freeing or possibly allocating incorrectly (which I must bee doing but yeah). I keep getting * glibc detected * free(): invalid next size
Am I actually freeing more than I need to or am I just not allocating what I need to allocate in the first place. --sorry for bad indentation can't get this editor to indent correctly
I have structures:
typedef int boolean;
typedef char * String;
typedef struct {
char name[30];
long ID;
char address[40];
char city[20];
int age;
}Employee;
typedef struct node {
Employee *anEmployee;
struct node *next;
}NODE;
typedef struct {
NODE *head, *tail;
}SLL;
insert function--SLL (Singly Linked List)
void insert(SLL *list, Employee e){
printf("insert");
NODE *temp, *current;
temp = (NODE *)malloc(sizeof(NODE));
assert(temp != NULL);
temp -> anEmployee = (Employee *)malloc(sizeof(Employee *));
assert(temp -> anEmployee != NULL);
strcpy(temp -> anEmployee -> name, e.name);
temp -> anEmployee -> ID = e.ID;
strcpy(temp -> anEmployee -> address, e.address);
strcpy(temp -> anEmployee -> city, e.city);
temp -> anEmployee -> age = e.age;
if (list -> head == NULL) { /* list is empty */
list -> head = list -> tail = temp;
return;
}
else { // list is not empty
list -> tail -> next = temp;
list -> tail = temp;
}
}
deleting and freeing memory in delete function as such
boolean delete(SLL *list, String str){
printf("delete");
NODE *temp, *temp2;
if (list -> head == NULL) return FALSE; // list is empty
temp = list -> head;
while ((temp != NULL) && (strcmp(temp -> anEmployee -> name , str) != 0))
temp = temp -> next;
if (temp == NULL) return FALSE; // str is not found in the list
// now temp points to the NODE with str in it. Let us delete it
// from the list
if ( list -> head == temp) { // temp points to the first NODE
if (temp -> next == NULL) { // temp points to the only NODE
list -> head = list -> tail = NULL;
free(temp -> anEmployee);
free(temp);
return TRUE;
}
// temp points to the first NODE but it is not the only NODE
list -> head = temp -> next;
free(temp -> anEmployee);
free(temp);
return TRUE;
}
if (temp -> next == NULL) { // temp points to the last NODE
temp = list -> head;
temp2 = list -> head -> next;
while(temp2 - > next != NULL){
temp = temp->next;
temp2 = temp2 ->next;
}
list -> tail = temp ;
list -> tail -> next = NULL;
free(temp2 -> anEmployee);
free(temp2);
return TRUE;
}
// temp points to some NODE in the middle of the list
temp2 = temp -> next;
while(temp2 - > next != NULL){
temp ->anEmployee = temp2 - > anEmployee //
temp = temp->next;
temp2 = temp2 ->next;
}
temp ->anEmployee = temp2 - > anEmployee
list -> tail = temp ;
list -> tail -> next = NULL;
free(temp2 -> anEmployee);
free(temp2);
return TRUE;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,在
insert
中,您分配的内存仅分配足够的内存来容纳
Employee
指针,而不是整个Employee代码>结构。您应该为
temp->anEmployee 分配一个大小为
sizeof(Employee)
的块。您对
free
的调用就有意义。想要释放someNode->anEmployee
和someNode
以彻底清理单个节点占用的内存。您可以按如下方式简化
delete
实现:通过跟踪查找之前的节点(如果有),您可以避免所有令人讨厌的特殊情况,并将核心列表更新减少为三个简单的条件分配。
First, in
insert
, You're allocatingwhich only allocates enough memory to hold an
Employee
pointer, not an entireEmployee
structure. You should allocate a block the size ofsizeof(Employee)
fortemp->anEmployee.
Your calls to
free
make sense insofar as you do want to freesomeNode->anEmployee
andsomeNode
to completely clean up the memory occupied by an individual node.You could simplify your
delete
implementation as follows:By tracking the node which precedes your find, if any, you can avoid all of the nasty special cases and reduce the core list update to three simple conditional assignments.
没有读完所有内容,
delete
的第一行应该做什么?我不知道
delete
应该删除什么。看来str
参数未使用。您是否想根据str
搜索特定记录,将temp
设置为指向它,然后继续执行所示的代码?Not having read all of it, what are the first lines of
delete
supposed to do?I can't tell what
delete
is supposed to be deleting. It appears that thestr
argument is unused. Do you want to search for a particular record based onstr
, settemp
to point to it, and then proceed with the code as shown?在 insert() 中,当您分配
temp->anEmployee
时,您只是为指针分配了足够的空间,而不是为整个 Employee 分配了足够的空间。这一行:
应该是:
In insert(), when you allocate
temp->anEmployee
, you're only allocating enough space for the pointer, not the full Employee.This line:
should be: