gpt4 book ai didi

c - 无法将第二个元素插入链表 |指向函数的指针被损坏

转载 作者:行者123 更新时间:2023-11-30 15:49:27 25 4
gpt4 key购买 nike

下面是相关代码:

typedef struct Node_t {
ListElement data;
struct Node_t* next;
} Node;

struct List_t {
Node* head;
Node* tail;
Node* current;
int size;
CopyListElement copyF;
FreeListElement freeF;
};
<小时/>
static ListResult initializeNode(List list, ListElement element, Node* newNode){
printf("\nEntered initializeNode\n");
if ((list == NULL) || (element == NULL)) return LIST_NULL_ARGUMENT;
newNode = malloc(sizeof(Node));
if (newNode == NULL) return LIST_OUT_OF_MEMORY;
printf("\nWithin initializeNode, before copyF\n");
ListElement newElement = list->copyF(element);
printf("\nWithin initializeNode, after copyF\n");
if (newElement == NULL) return LIST_OUT_OF_MEMORY;
newNode->data = newElement;
printf("\nLast line within initializeNode\n");
return LIST_SUCCESS;
}
<小时/>
List listCreate(CopyListElement copyElement, FreeListElement freeElement){

//Check if there is a NULL argument.
if ((copyElement == NULL) || (freeElement == NULL)) return NULL;

//Check wether there is enough memory.
List newList = malloc(sizeof(List));
if (newList == NULL) return NULL;
//Initialize an empty List.
newList->head = NULL;
newList->tail = NULL;
newList->size = 0;
newList->current = NULL;
newList->copyF = copyElement;
newList->freeF = freeElement;

return newList;
}
<小时/>
ListResult listInsertFirst(List list, ListElement element){
printf("\nEntered listInsertFirst\n");
Node* newNode;
ListResult result = initializeNode(list, element, newNode);
printf("\n Node was initialized\n");
if (result != LIST_SUCCESS) {
return result;
}

printf("\nEntering logistic works within listInsertFirst\n");
//Finish logistic work within the Node.
newNode->next = list->head;
list->head = newNode;
list->size++;
printf("\nElement was inserted successfully\n");

printf("\nCheck list->CopyF within listInsertFirst\n");
list->copyF(element);
printf("\nCheck list->CopyF within listInsertFirst: PASSED\n");

return LIST_SUCCESS;
}
<小时/>

在主函数中我正在尝试:

List list = listCreate(&copyInt, &freeInt);

ListResult result;
int el=2;
//ListElement e1;
//ListElement e2;

result = listInsertFirst(list,&el);
printf("\nresult = %d\n", result);

result = listInsertFirst(list,&el);
printf("\nresult = %d\n", result);
<小时/>

After compiling and running I get:

Entered listInsertFirst

Entered initializeNode

Within initializeNode, before copyF

Within initializeNode, after copyF

Last line within initializeNode

Node was initialized

Entering logistic works within listInsertFirst

Element was inserted successfully

Check list->CopyF within listInsertFirst Segmentation fault: 11

由于某种原因,[指向函数] list->copyF 的指针被损坏了[我认为]。

最佳答案

根据标签,我假设这是 C 代码,而不是 C++。鉴于您混合了数据定义和实际代码语句,我不希望它在 C 中工作,我不能 100% 确定它是真正的 C,在这种情况下,我可能对下面的错误是错误的。

首先,initializeNode() 的接口(interface)并不符合您的预期。您可能想要:

static ListResult initializeNode(List list, ListElement element, Node** newNodep)
{
Node *newNode = malloc(sizeof(Node));
if (newNode == NULL) return LIST_OUT_OF_MEMORY;
ListElement newElement = list->copyF(element);
if (newElement == NULL) return LIST_OUT_OF_MEMORY;
newNode->data = newElement;
*newNodep = newNode;
return LIST_SUCCESS;
}

这样您创建的节点就会被传回。

我不知道 CopyInt() 的作用,但如果它确实是函数遇到总线错误,那么 initializeNode() 的错误就不是您的问题。但是,在报告崩溃之前您可能看不到所有 printf 的输出。

如果 CopyInt() 执行我所期望的操作,它会执行以下操作:

ListElement CopyInt(int *val)
{
ListElement *e = malloc(sizeof(ListElement));
if (e)
e->val = *val;
return e;
}

这里出现第二次总线错误的唯一方法是,如果您弄乱了库函数 malloc() 维护的数据结构。不幸的是,对于这个理论,我没有看到任何比内存泄漏更糟糕的事情。

关于c - 无法将第二个元素插入链表 |指向函数的指针被损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16269637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com