2

I have a struct like this:

struct node {
    int state[9];
};

typedef struct node Node;

In the beginning of my code, I initialize it with some state. Then, I generate possible sucessors to this state, based on the ZERO location, for example:

case(7):
    printf("Before generating sucessor 2:\n");
    for(i=0;i<9;i++) printf("%d,", n->state[i])
    suc2->state[7] = n->state[6];
    suc2->state[6] = 0;
    printf("After generating sucessor 2:\n");
    for(i=0;i<9;i++) printf("%d,", n->state[i])
 break;

The case above is in a function that generate possible sucessors states. And "n" is the node I receive: expand(Node* n);

My problem is: the n state is different after I generate sucessor 2. And I cant understand why, since i'm only changing successor 2 state.

What could be causing this?

1
  • 4
    perhaps n and suc2 point to the same struct Commented Sep 13, 2014 at 12:51

1 Answer 1

3

If when you modify suc2->state[6] the value of n->state[6] changes to the same value, then n points to the same node as suc2.

This is possible if your data structure has a link back to the node from its first successor, so traversing two levels in actually brings you back to where you have started.

If you know that your algorithm is such that this is not supposed to happen, add an assertion to your code:

assert(suc2 != n);

This will stop the execution of your program when this problem happens because of a bug in the algorithm.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, you are right, they are pointing to same thing. Now I see that my problem is: sucessor2 is choose as n in the next algorithm phase, so when I'm creating my next sucessors, I'm altering my n (that became sucessor2 in the anterior phase). So, I guess I can solve it allocating my sucessors in my expand function.... well, thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.