0

I've tried applying advice from other threads regarding the EXC_BAD_ACCESS message, but with no success. The note appears next to 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;
}

Any insight? Memory allocation doesn't seem to be the issue, but I'm probably not seeing something.

surf-n-cherf
  • 63
  • 1
  • 7

1 Answers1

0

The pointers e.g. Child.Wolf are local to the function, they have no meaning outside your Create_Child function yet you assign those addresses to Child.Object_Moved and return a copy of it.

You should allocate Child on the heap instead

Node* Create_Child(Node Parent_Node, int item) {

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

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

Also it is good to sanity check all arguments to your function before using them. e.g. item in range?, Parent_Node valid info?

AndersK
  • 35,813
  • 6
  • 60
  • 86