openlang

Why do values change during recursive calls?

How is this possible? What ought I check?

My recursive function visit(board) receives a struct, named incoming within the function.

nextBoard holds two slices left and right. Each slice contains small structs cell with components label1, label2 and bool.

Inside visit(incoming) I have:

nextBoard := advance(incoming)

savedInput := incoming

visit(nextBoard)

There are no other calls to visit inside it.

That is the background, now the problem: suppose at recursion depth 27 visit gets called, descending a further 12 levels. It then comes back to depth 27. Now incoming differs from its contents before that call. Oddly savedInput has changed too, in precisely the way incoming has. nextBoard still contains what it did before calling.

What I want to know is how? These 3 variables all have function scope, NOT scope at package level.

Some extra detail on incoming's changes: both left and right slices have len 7. Its right slice comes back with the 7th element changed from what it originally contained?

I do know that somewhere from depths 27 to 39, right's seventh element is replaced with left's seventh element, with its boolean reversed, and left loses its 7th element. A function named advance(incoming) performs that modification.

More precisely, when incoming comes back to depth 27, left is unchanged but right has the modification described above ie right[6]=left[6] with its boolean reversed.

My suspicion is that slices within slices still refer to memory shared with the original. It looks like the only reason incoming and savedInput would show identical modifications. Yet why would incoming differ at all after that call?

Thanks for taking the time to read this.

Is there a particular thing I should check? Am I misunderstanding the effect of writing savedInput:=incoming ? I've tried := as well as = with savedInput declared beforehand.

The conversation continues 2

Follow the reasoning
  1. Permalink

    I'm having trouble following this, could you provide some demonstration code? The code itself would show the problem more clearly than describing it in words.

  2. Permalink

    Fair point - I reconsidered the problem and posted my question again with some exmaple code. See Go - I expected arguments to be passed by value

Other sides of the same question

  1. Same sink call, a copy on one line and a move on the next

    Nim 2 decides copy or move per call site. Not per proc. Same argument, same proc. The first call copies, because a is still n…

    Replies 2Latest activity nim
  2. Delaying a cleanup block's mutable borrow until it actually runs

    What would be a way for me to write a panic-safe mecanism in Rust that behaves similarly to Java's try { ... } finally { ...…

    Replies 2Latest activity rust
  3. Inputting character arrays through pointers

    Whenever I attempt keyboard input into some char arrays, my program ends up crashing. What's a straightforward way of reading…

    Replies 2Latest activity c