C++ 在空闲列表期间,有一个段错误

C++ 在空闲列表期间,有一个段错误,c++,c,linked-list,free,C++,C,Linked List,Free,两个功能,一个是创建链接列表,另一个是释放链接列表。 如果Create函数返回指向head节点的双指针,使用此节点释放链接列表,将遇到段错误。但是,如果更改Create函数以返回指向head节点的指针,然后释放列表,这就可以了 谁能给我解释一下?以下是出现段错误的代码: #include <stdio.h> #include <stdlib.h> typedef struct ListNode{ int m_nValue; ListNode* m_pNe

两个功能,一个是创建链接列表,另一个是释放链接列表。 如果Create函数返回指向head节点的双指针,使用此节点释放链接列表,将遇到段错误。但是,如果更改Create函数以返回指向head节点的指针,然后释放列表,这就可以了

谁能给我解释一下?以下是出现段错误的代码:

#include <stdio.h>
#include <stdlib.h>

typedef struct ListNode{
    int m_nValue;
    ListNode* m_pNext;
}ListNode;

ListNode** CreateList(int data[], int length){
   if(length<=0 || data == NULL)
       return NULL;
   ListNode *pHead = (ListNode*)malloc(sizeof(ListNode));
   ListNode *pNode = pHead;
   pNode->m_pNext = NULL;
   pNode->m_nValue = data[0];
   int i=1;
   for(; i<length; i++){
       ListNode *temp = (ListNode*)malloc(sizeof(ListNode));
       temp->m_nValue = data[i];
       temp->m_pNext = NULL;
       pNode->m_pNext = temp;
       pNode = temp;
   }
   return &pHead;
}

void FreeList(ListNode **pHead){
    ListNode *pNode;
    while(pHead!=NULL && *pHead!=NULL){
        pNode = *pHead;
        *pHead = pNode->m_pNext; // here will encounter an error;
        free(pNode);
    }
    pHead = NULL;
}

int main(){
    int data[] = {1,2,3,4,5};
    ListNode **pHead = CreateList(data, sizeof(data)/sizeof(int));
    FreeList(pHead);
}
#包括
#包括
类型定义结构列表节点{
国际货币价值;
ListNode*m_pNext;
}列表节点;
ListNode**CreateList(整数数据[],整数长度){
如果(lengthm_pNext=NULL;
pNode->m_nValue=数据[0];
int i=1;
对于(;im_nValue=数据[i];
temp->m_pNext=NULL;
pNode->m_pNext=temp;
pNode=温度;
}
返回&pHead;
}
无效自由列表(列表节点**pHead){
ListNode*pNode;
while(pHead!=NULL&&*pHead!=NULL){
pNode=*pHead;
*pHead=pNode->m_pNext;//此处将遇到错误;
自由(pNode);
}
pHead=NULL;
}
int main(){
int data[]={1,2,3,4,5};
ListNode**pHead=CreateList(数据,sizeof(数据)/sizeof(int));
自由列表(pHead);
}
但是如果我将CreateList的返回类型更改为ListNode*CreateList(…),这将很好地工作

ListNode* CreateList(int data[], int length){
     if(length<=0 || data == NULL)
          return NULL;
   ListNode *pHead = (ListNode*)malloc(sizeof(ListNode));
   ListNode *pNode = pHead;
   pNode->m_pNext = NULL;
   pNode->m_nValue = data[0];
   int i=1;
   for(; i<length; i++){
       ListNode *temp = (ListNode*)malloc(sizeof(ListNode));
       temp->m_nValue = data[i];
       temp->m_pNext = NULL;
       pNode->m_pNext = temp;
       pNode = temp;
   }
   return pHead;
}
int main(){
    int data[] = {1,2,3,4,5};
    ListNode *pHead = CreateList(data, sizeof(data)/sizeof(int));
    FreeList(&pHead);
}
ListNode*CreateList(int-data[],int-length){
如果(lengthm_pNext=NULL;
pNode->m_nValue=数据[0];
int i=1;
对于(;im_nValue=数据[i];
temp->m_pNext=NULL;
pNode->m_pNext=temp;
pNode=温度;
}
返回pHead;
}
int main(){
int data[]={1,2,3,4,5};
ListNode*pHead=CreateList(数据,sizeof(数据)/sizeof(int));
自由列表(&pHead);
}

ListNode**CreateList(int data[],int length)
方法中,返回指向局部变量的指针,当函数返回时,该指针显然无效

也就是说,在
CreateList
函数中声明一个指针变量
ListNode*pHead
,并返回变量
pHead
的地址。指针变量
pHead
存储在堆栈中,当CreateList函数返回堆栈时,用于存储
pHead
的非预期内存被释放
pHead
指向的内存在堆上仍然可用