在树中释放动态分配的内存

时间:2013-04-22 01:27:46

标签: c memory gcc malloc free

我正在尝试释放我用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;
};

1 个答案:

答案 0 :(得分:2)

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

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