如何正确调用 free()?
How to call free() properly?
请通读全文 post,因为其中包含非常重要的小细节。
正如 C 所知,我们应该处理 malloc
失败的事件,对于这种情况,我创建了一个名为 destroyList()
的函数,其工作是获取指向 Node
的指针,并且一个一个地摧毁它。
但是我的函数没有被正确调用...
我尝试用 ptr、merged_out
和 *merged_out
调用它(最后一个是社区成员的建议)但似乎没有任何效果。
这是为什么? 函数有时会收到 NULL
、空列表或一些随机值。
有人可以帮我解决这个问题,让我了解发生了什么吗?
typedef struct node_t {
int x;
struct node_t *next;
} *Node;
void destroyList(Node ptr) {
while (ptr) {
Node toDelete = ptr;
ptr = ptr->next;
free(toDelete);
}
}
主要功能:
ErrorCode mergeSortedLists(Node list1, Node list2, Node *merged_out) {
if (!list1 || !list2) {
return EMPTY_LIST;
}
if (!isListSorted(list1) || !isListSorted(list2)) {
return UNSORTED_LIST;
}
if (!merged_out) {
return NULL_ARGUMENT;
}
Node ptr = NULL;
int total_len = getListLength(list1) + getListLength(list2);
for (int i = 0; i < total_len; i++) {
int min = getMin(&list1, &list2);
ptr = malloc(sizeof(*ptr));
*merged_out = ptr;
if (!ptr) {
destroyList(*merged_out);
*merged_out = NULL;
return MEMORY_ERROR;
}
ptr->x = min;
ptr->next = NULL;
merged_out = &ptr->next;
}
return SUCCESS;
}
函数应该这样调用:
Node merged_actual = NULL;
ErrorCode merge_status = mergeSortedLists(list1, list2, &merged_actual);
注意:getMin()
获取最小值并将具有该最小值的链表的指针前进到下一个节点。
在 if
检查之后开始。
Node ptr=NULL,last;
/* find out current tail of the list */
if (*merged_out!=NULL){
last=*merged_out;
while (last->next!=NULL){
last=last->next;
}
}
int total_len = getListLength(list1) + getListLength(list2);
for (int i = 0; i < total_len; i++)
{
int min = getMin(&list1, &list2);
ptr = malloc(sizeof(*ptr));
if (!ptr)
{
destroyList(*merged_out);
*merged_out=NULL;
return MEMORY_ERROR;
}
ptr->x = min;
ptr->next = NULL;
/* link ptr onto the list */
if (*merged_out==NULL){
/* if the list is empty, make ptr the head of the list */
*merged_out=ptr;
last=*merged_out;
}
else{
last->next = ptr;
last = ptr;
}
}
请尽量不要复制粘贴此代码块。它可能正确也可能不正确,但请尝试理解它做了什么:每次调用该函数时进行迭代,努力使 last
指向列表的最后一个元素。所以merged_out
总能指向头部
@user12986714 我丢失了我的旧帐户,并被告知不要关心 *merged_out 的初始值你能更新解决方案吗(删除第一个 while 循环,不需要 2 个指针)
请通读全文 post,因为其中包含非常重要的小细节。
正如 C 所知,我们应该处理 malloc
失败的事件,对于这种情况,我创建了一个名为 destroyList()
的函数,其工作是获取指向 Node
的指针,并且一个一个地摧毁它。
但是我的函数没有被正确调用...
我尝试用 ptr、merged_out
和 *merged_out
调用它(最后一个是社区成员的建议)但似乎没有任何效果。
这是为什么? 函数有时会收到 NULL
、空列表或一些随机值。
有人可以帮我解决这个问题,让我了解发生了什么吗?
typedef struct node_t {
int x;
struct node_t *next;
} *Node;
void destroyList(Node ptr) {
while (ptr) {
Node toDelete = ptr;
ptr = ptr->next;
free(toDelete);
}
}
主要功能:
ErrorCode mergeSortedLists(Node list1, Node list2, Node *merged_out) {
if (!list1 || !list2) {
return EMPTY_LIST;
}
if (!isListSorted(list1) || !isListSorted(list2)) {
return UNSORTED_LIST;
}
if (!merged_out) {
return NULL_ARGUMENT;
}
Node ptr = NULL;
int total_len = getListLength(list1) + getListLength(list2);
for (int i = 0; i < total_len; i++) {
int min = getMin(&list1, &list2);
ptr = malloc(sizeof(*ptr));
*merged_out = ptr;
if (!ptr) {
destroyList(*merged_out);
*merged_out = NULL;
return MEMORY_ERROR;
}
ptr->x = min;
ptr->next = NULL;
merged_out = &ptr->next;
}
return SUCCESS;
}
函数应该这样调用:
Node merged_actual = NULL;
ErrorCode merge_status = mergeSortedLists(list1, list2, &merged_actual);
注意:getMin()
获取最小值并将具有该最小值的链表的指针前进到下一个节点。
在 if
检查之后开始。
Node ptr=NULL,last;
/* find out current tail of the list */
if (*merged_out!=NULL){
last=*merged_out;
while (last->next!=NULL){
last=last->next;
}
}
int total_len = getListLength(list1) + getListLength(list2);
for (int i = 0; i < total_len; i++)
{
int min = getMin(&list1, &list2);
ptr = malloc(sizeof(*ptr));
if (!ptr)
{
destroyList(*merged_out);
*merged_out=NULL;
return MEMORY_ERROR;
}
ptr->x = min;
ptr->next = NULL;
/* link ptr onto the list */
if (*merged_out==NULL){
/* if the list is empty, make ptr the head of the list */
*merged_out=ptr;
last=*merged_out;
}
else{
last->next = ptr;
last = ptr;
}
}
请尽量不要复制粘贴此代码块。它可能正确也可能不正确,但请尝试理解它做了什么:每次调用该函数时进行迭代,努力使 last
指向列表的最后一个元素。所以merged_out
总能指向头部
@user12986714 我丢失了我的旧帐户,并被告知不要关心 *merged_out 的初始值你能更新解决方案吗(删除第一个 while 循环,不需要 2 个指针)