2

我正在尝试释放我用 malloc 分配的内存,但 free 给出了错误:

 malloc: *** error for object 0x100100800: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap

我有一棵带节点的树

struct node {
  struct state *s;
  struct node *child;
  struct node *sibling;
};

我正在尝试使用此功能释放除一个孩子及其死者以外的所有节点:

struct node * free_children (struct node *head, struct node *keep_c) {
  struct stack_node *stack_head = init_stack();
  struct node *popped;
  push(stack_head, head);
  // to avoid checking if = keep_c for each level, do top level first
  for (struct node * s = head->child; s != 0; s = s->sibling) {
    if (s != keep_c) push(stack_head, s);
  }
  while (!stack_is_empty(stack_head)) {
    popped = pop(stack_head);
    if (popped->child != 0) push(stack_head, popped->child);
    if (popped->sibling != 0) push(stack_head, popped->sibling);
    free(popped->s);
  }
  return keep_c;
}

我无法弄清楚发生了什么,因为所有节点都是用 malloc 创建的,节点指向的所有状态也是如此。

编辑:这是分配内存的代码:

void push (struct stack_node *head, struct node *k) {
  struct stack_node * x = (struct stack_node *)
    malloc(sizeof(struct stack_node));
  x->key = k;
  x->next = head->next;
  head->next = x;
  return;
}

struct stack_node * init_stack () {
  struct stack_node * head = (struct stack_node *)
    malloc(sizeof(struct stack_node));
  head->next = 0;
  return head;
}

struct node * build_game_tree (int p1, int p2) {
  struct node *head = init_game_tree();
  struct state *state = (struct state *) malloc(sizeof(struct state));
  state->player = 0;
  state->s[0] = p1; state->s[1] = p2;
  head->s = state;
  struct stack_node *stack_head = init_stack();
  struct stack_node *upper_stack_head = init_stack();
  struct node *popped;
  bool possible_moves[9];
  push(stack_head, head);
  while(!stack_is_empty(stack_head)) {
    popped = pop(stack_head);
    if (!endgame(popped->s->s[0], popped->s->s[1])) {
      push_possible_moves(stack_head, popped);
      push(upper_stack_head, popped);
    }
    else {
      popped->child = 0;
      popped->s->score =
        score(popped->s->s[0], popped->s->s[1]);
    }
  }
  ...
  return head;
}

编辑:

struct state {
  unsigned int s[2];
  double score;
  unsigned int player;
};
4

1 回答 1

2

free_children()中,您尝试多次释放相同的内存。循环从第for一个孩子开始,遍历所有孩子的兄弟姐妹,将它们放入堆栈。while 循环还遍历每个孩子的兄弟姐妹,也将它们放入堆栈。每当一个节点出现在堆栈上时,您最终都会尝试free()它。

您需要重新考虑free_children(). 从递归实现开始可能更容易,如果有迫切需要,可以选择稍后将其转换为迭代实现。

于 2013-04-22T06:30:43.397 回答