我在使用 destroy_tree 函数删除 C++ 中的树时遇到问题。编译器说*** glibc detected *** <path>: free(): invalid pointer: 0x00007f37590d3778 ***
。这是我的destroy_tree
功能:
void Tree::destroy_tree(node** parent) {
if ( (*parent) != NULL ) {
for (int i=0; i<NUM_ELEMS; i++) {
destroy_tree( &((*parent)->leaves[i]) );
}
if ( (*parent)->zone_id != NULL ) {
delete (*parent)->zone_id;
(*parent)->zone_id = NULL;
}
delete (*parent);
(*parent) = NULL;
}
这是结构:
struct node {
int* zone_id;
node* leaves [NUM_ELEMS];
node() : leaves() {} //setting all leaves pointers to NULL
};
指针初始化(在其他函数中):
(*parent)->leaves[value]->zone_id = new int;
*((*parent)->leaves[value]->zone_id) = zone_id;
我只是看不出它失败的地方......