어떻게 이런 일이 생기죠? 뭘 확인해야 할까요?
재귀 함수 visit(board)는 구조체를 받고, 함수 안에서는 incoming이라는 이름으로 씁니다.
nextBoard에는 left와 right라는 슬라이스가 두 개 있습니다. 각 슬라이스에는 label1, label2, bool을 멤버로 갖는 작은 구조체 cell이 들어 있습니다.
visit(incoming) 안의 코드는 이렇습니다:
nextBoard := advance(incoming)
savedInput := incoming
visit(nextBoard)
함수 안에서 visit을 호출하는 곳은 이것뿐입니다.
여기까지가 배경이고, 문제는 이렇습니다. 재귀 깊이 27에서 visit이 호출되어 12단계를 더 내려간다고 합시다. 그런 다음 깊이 27로 돌아옵니다. 이때 incoming이 호출 전 내용과 달라져 있습니다. 이상하게 savedInput도 incoming과 정확히 똑같이 바뀌어 있습니다. nextBoard는 호출 전 그대로입니다.
제가 알고 싶은 건 어떻게 이럴 수 있냐는 겁니다. 이 변수 3개는 모두 함수 범위에 있고 패키지 범위가 아닙니다.
incoming의 변화를 더 설명하면, left와 right 슬라이스의 len은 둘 다 7입니다. right 슬라이스가 돌아올 때 원래의 7번째 원소와 달라져 있습니다?
깊이 27에서 39 사이 어딘가에서 right의 일곱 번째 원소가 left의 일곱 번째 원소로 대체되고 불리언이 뒤집히며, left의 7번째 원소는 삭제되는 것은 압니다. advance(incoming)이라는 함수가 그 변경을 합니다.
정확히 말하면, 깊이 27로 돌아온 incoming의 left는 그대로인데 right는 앞에서 설명한 대로 바뀝니다. 즉 불리언이 뒤집힌 right[6]=left[6]입니다.
슬라이스 안의 슬라이스가 여전히 원본과 공유하는 메모리를 가리키는 게 아닐까 의심됩니다. incoming과 savedInput이 똑같이 바뀌는 이유로는 그것밖에 생각나지 않습니다. 그런데 호출 뒤에 incoming이 왜 조금이라도 달라지는 거죠?
시간 내서 읽어주셔서 감사합니다.
특별히 확인할 게 있나요? savedInput:=incoming이라고 썼을 때의 효과를 제가 잘못 이해하나요? :=도 써보고 savedInput을 먼저 선언한 뒤 =도 써봤습니다.
영어에서 번역·원문 보기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.