C++에서 STL 트리의 BFS 코드를 연습하고 있었고 디버그가 불가능한 런타임 오류가 하나 있습니다. printout() function
을 호출하지 않으면 정상적으로 작동합니다. 내가 STL에 새로운 오전으로 도와주세요 ..[오류] 포인터가 해제되었습니다
#include<iostream>
#include<malloc.h> //on llvm we don't need this
#include<list>
using namespace std;
typedef struct Node{
int val;
struct Node* left;
struct Node* right;
}node;
void push(node** root,int val)
{
if(!(*root))
{
node* temp=(node*)malloc(sizeof(node));
temp->val=val;
temp->right=temp->left=NULL;
*root=temp;
}
else if(val<(*root)->val)
push(&((*root)->left),val);
else
push(&((*root)->right),val);
}
void printout(node* head)
{
node* temp;
temp=head;
list<node*>qu;
//using bfs here
while(temp!=NULL)
{
cout<<temp->val<<endl;
if(temp->left!=NULL)
qu.push_back(temp->left);
if(temp->right!=NULL)
qu.push_back(temp->right);
temp=qu.front();
qu.pop_front();
//free(temp);
}
}
int main()
{
node* root=NULL;
push(&root,3);
push(&root,4);
push(&root,1);
push(&root,10);
push(&root,2);
printout(root);
}
하면이 corrent 출력하지만 당신은 qu
이 비어있는 경우 확인하지 않고 각각의 반복에서 qu.front()
를 호출 실행 시간
3
1
4
2
10
a.out(613) malloc: *** error for object 0x7fff55ed8bc8: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
표준 C++을 작성하려면 ''을' ' –