C - 删除 n 元树节点
C - deleting n-ary tree nodes
我用 C 实现了一个 m,n,k-game 与 AI。游戏运行良好,但当我必须释放决策树时,它总是抛出 "Access violation reading location" 异常。
这是决策树结构的实现:
typedef struct decision_tree_s {
unsigned short **board;
status_t status;
struct decision_tree_s *brother;
struct decision_tree_s *children;
} decision_tree_t;
这是 delete_tree
函数的实现:
void delete_tree(decision_tree_t **tree) {
decision_tree_t *tmp;
if (*tree != NULL) {
delete_tree((*tree)->children);
delete_tree((*tree)->brother);
free(*tree);
*tree = NULL;
}
}
您正在破坏 children
成员两次:第一次在 for
循环中,第二次在循环之后。
您可能想像这样编写 for 循环:
for (tmp = tree->brother; tmp != NULL; tmp = tmp->brother) {
delete_tree(tmp);
}
我用 C 实现了一个 m,n,k-game 与 AI。游戏运行良好,但当我必须释放决策树时,它总是抛出 "Access violation reading location" 异常。
这是决策树结构的实现:
typedef struct decision_tree_s {
unsigned short **board;
status_t status;
struct decision_tree_s *brother;
struct decision_tree_s *children;
} decision_tree_t;
这是 delete_tree
函数的实现:
void delete_tree(decision_tree_t **tree) {
decision_tree_t *tmp;
if (*tree != NULL) {
delete_tree((*tree)->children);
delete_tree((*tree)->brother);
free(*tree);
*tree = NULL;
}
}
您正在破坏 children
成员两次:第一次在 for
循环中,第二次在循环之后。
您可能想像这样编写 for 循环:
for (tmp = tree->brother; tmp != NULL; tmp = tmp->brother) {
delete_tree(tmp);
}