我在Codeblocks上的树程序中使用此函数它显示Pop函数中的分段错误,我释放了Tree节点。分段错误就像程序接收信号SIGSEGV分段,我理解这个错误是由于{ {1}}没有得到返回1(只有0)的值。似乎Pop功能有错误,我需要这方面的帮助,我被困在这里很多天plz帮帮我。
//树类型节点的堆栈实现
isEmptyStack()
这是为了保留订单打印级别订单树....从左到右,从下到上,
typedef struct TreeStructure
{
int data;
struct TreeStructure *left;
struct TreeStructure *right;
}Tree;
typedef struct SListNode
{
struct TreeStructure *data;
struct ListNode *next;
}SList;
typedef struct StackList
{
struct ListNode *Node;
}Stack;
Stack *CreationStack()
{
return NULL;
}
int isEmptyStack(Stack *top)
{
return top==NULL;
}
void Push(Stack **top,Tree *data)
{
SList *new,*tmp;
new=malloc(sizeof *new); // Modification here according to comments
new->data=data;
new->next=*top;
*top=new;
}
Tree *Pop(Stack **top)
{
Tree *data;
SList *tmp;
if(isEmptyStack(*top))
{
printf("Underflow") ;return NULL;
}
else
{
tmp=*top;
*top=tmp->next;
data=tmp->data;
if(tmp) // using do not let occur case of the dangling pointer
free(tmp); // Showing fault here only on Debugging
return data;
}
}
正如我已经检查过其他功能正常工作,每当我尝试增加我的代码时,从那里开始出现问题,PLZ不要混淆其他功能
答案 0 :(得分:1)
请在发布之前仔细检查您的代码。这是我第一眼就看到的东西,很可能是其他东西,因为你根本没有足够的注意来使事情变得正确。
您的职能Push
:
tmp
malloc
typedef
ed指针答案 1 :(得分:0)
似乎data
是pop函数内的悬空指针。释放tmp时,还可以释放数据指向的TreeStructure。