【问题标题】:EXC_BAD_ACCESS creating child nodes (C)EXC_BAD_ACCESS 创建子节点 (C)
【发布时间】:2015-08-28 04:16:03
【问题描述】:

我尝试应用其他线程关于 EXC_BAD_ACCESS 消息的建议,但没有成功。注释出现在Node Create_Child (Node Parent_Node, int item) { 旁边。

typedef struct {
    int Win_Loss;
    int parent;
    int identifier;
    int Object_Moved;
    int Wolf; 
    int Goat;
    int Salad;
    int Boat;
} Node;


Node Create_Child (Node Parent_Node, int item) {
    Node Child;
    Child.Boat = (-1)*Parent_Node.Boat;
    Child.Wolf = Parent_Node.Wolf;
    Child.Goat = Parent_Node.Wolf;
    Child.Salad = Parent_Node.Salad;

    int* Child_Items[] = {&Child.Wolf, &Child.Goat, &Child.Salad, &Child.Boat};
    Child.parent = Parent_Node.identifier;
    Child_Items[item][0] *= (-1);
    Child.Object_Moved = item;
    return Child;
}

有什么见解吗?内存分配似乎不是问题,但我可能什么也没看到。

【问题讨论】:

  • item 的值是多少?是 >= 0 和
  • 是的。抱歉,我应该指定。

标签: c tree nodes minimax alpha-beta-pruning


【解决方案1】:

指针,例如Child.Wolf 是函数的本地,它们在您的 Create_Child 函数之外没有任何意义,但您将这些地址分配给 Child.Object_Moved 并返回它的副本。

您应该改为在堆上分配 Child

Node* Create_Child(Node Parent_Node, int item) {

Node* Child = malloc(sizeof(Node));
Child->Boat = -1*Parent_Node.Boat;
...

int* Child_Items[] = { &Child->Wolf, .. };

另外,最好检查一下函数的所有参数 在使用它们之前。例如item 在范围内?Parent_Node 有效信息?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    相关资源
    最近更新 更多