Warm tip: This article is reproduced from serverfault.com, please click

Why does pointer assignment cause variable assignment to not always stick?

发布于 2020-11-28 07:06:11

Pointer assignment of index is being consistently inconsistent within addData(..). I expect memory address is moving around as underlying array increases in size.

Behavior: I assign to variable A, then assign B = A*0.2, then assign y = sig(B), finally B = y. Sometimes on the next loop B == y || B == A*0.2. It is perfectly consistent across multiple executions.

I made a simpler and more full version of the code.

package main

import(
    "fmt"
    "math"
)

func main(){
    //Structure setup
    l := lots{}; l.addData(2); l.addData(2); l.addData(2); l.addData(2)
    
    l.val[0].y[0] = 0.20700021
    l.val[0].y[1] = 0.30003001
    l.propagate()
}

type data struct{
    y []float64
}

type pair struct {
    one *data
    two *data
}

// lots is the biggest part of the structure
// the problem seems to occure when this is introduced
type lots struct{
    val []data
    join []pair
}

// addData appends a data struct and a pair struct to
// the corresponding parts of lots struct
func (l *lots)addData(size int){
    l.val = append(l.val, data{make([]float64, size)})
    
    // should be skipped first call only
    if(len(l.join) < len(l.val)-1){
        fmt.Println("len of l.val: ", len(l.val)) 
        l.join = append(l.join, pair{})
        l.join[len(l.join)-1].one = &l.val[len(l.val)-2]
        l.join[len(l.join)-1].two = &l.val[len(l.val)-1]
    }
}

// propagate 
func (l *lots)propagate(){
    for _, v := range l.join{
        v.travel()
    }
}

// travel modifies values going from p.one -> p.two
func (p *pair) travel(){
    fmt.Println("p.one.y: ", p.one.y)
    p.mathy()
    fmt.Println("p.two.y: ", p.two.y)
    
    p.two.y = sigmoid(p.two.y)
    fmt.Println("p.two.y: ", p.two.y)
}

func (p *pair) mathy(){
    for i := range p.one.y {
        p.two.y[i] = p.one.y[i] * p.one.y[i]
    }
}

// sigmoid seems to be causing some problems.
// Works fine on it's own though
func sigmoid(x []float64)(y []float64){
    y = make([]float64, len(x))
    for i := range x{
        y[i] = 1./(1.+math.Exp(-x[i]))
    }
    return
}

I expect the #'s of p.two.y: [#'s] to equal the following lines #'s of p.one.y: [#'s] all the time. The output I am getting is not consistently equal. Sometimes the p.one.y: [#'s] #'s are equal to the p.two.y: [#'s] from the line preceding line; that value was over written and then that value came back.

p.one.y:  [0.20700021 0.30003001]
p.two.y:  [0.04284908694004409 0.0900180069006001]/////// bad
p.two.y:  [0.5107106330188076 0.5224893174114301]      // overwritten
p.one.y:  [0.04284908694004409 0.0900180069006001]/////// reappeared
p.two.y:  [0.0018360442515954571 0.008103241566356488]
p.two.y:  [0.5004590109339528 0.5020257993066767]//// overwritten
p.one.y:  [0.5004590109339528 0.5020257993066767]//// good
p.two.y:  [0.25045922162499035 0.25202990316950763]
p.two.y:  [0.5622895277500193 0.5626760660176802]

I have tried to reduce function nesting, and it worked when I put everything into the Propagate() function and assigned directly to p.two.y[i] with the sigmoid function. (below)

// propagate 
func (l *lots)propagate(){
    for _, p := range l.join{
        fmt.Println("p.one.y: ", p.one.y)
        
        for i := range p.one.y {
            p.two.y[i] = p.one.y[i] * p.one.y[i]
        }
        fmt.Println("p.two.y: ", p.two.y)
        
        // using this extra variable causes the problem of inconsistent assignment
        //y := make([]float64, len(p.two.y))
        for i := range p.two.y{
            //y[i] = 1./(1.+math.Exp(-p.two.y[i]))
            p.two.y[i] = 1./(1.+math.Exp(-p.two.y[i]))
        }
        //p.two.y = y
        
        fmt.Println("p.two.y: ", p.two.y)
    }
}

This version provides good data, but takes away so much of the specialization I like.

p.one.y:  [0.20700021 0.30003001]
p.two.y:  [0.04284908694004409 0.0900180069006001]
p.two.y:  [0.5107106330188076 0.5224893174114301]////
p.one.y:  [0.5107106330188076 0.5224893174114301]//// Good
p.two.y:  [0.2608253506784712 0.27299508680906215]
p.two.y:  [0.564839170446528 0.5678280461350629]////
p.one.y:  [0.564839170446528 0.5678280461350629]//// Good
p.two.y:  [0.3190432884707219 0.3224286899775631]
p.two.y:  [0.5790910765397528 0.5799160282084651]
Questioner
Cody Harkinz
Viewed
0
Cody Harkinz 2020-11-29 12:22:52

The problem is with the pointer assignments as you build the slice you are trying to reference. The addresses keep changing.

func main(){
    var lump []int
    
    // A loop to build a slice of `int`'s from 0 size to 8 size
    // and print each index address
    for i:= 0; i < 8; i++{
        lump = append(lump, int(i))
        fmt.Printf("addr of lump[%v]: %p\n",i, &lump[i])
    }
    
    fmt.Println()
    
    // A loop to look at the addresses of each index
    for i := range lump{
        fmt.Printf("addr of lump[%v]: %p\n",i, &lump[i])
    }
}

Check out the addresses not being created in sequential memory locations.

//while building the slice
// notice the addresses making big jumps
addr of lump[0]: 0xc00000a0c8
addr of lump[1]: 0xc00000a0f8
addr of lump[2]: 0xc00000e3b0
addr of lump[3]: 0xc00000e3b8
addr of lump[4]: 0xc00000c2e0
addr of lump[5]: 0xc00000c2e8
addr of lump[6]: 0xc00000c2f0
addr of lump[7]: 0xc00000c2f8

//after building the slice
// notice all address being sequential
addr of lump[0]: 0xc00000c2c0
addr of lump[1]: 0xc00000c2c8
addr of lump[2]: 0xc00000c2d0
addr of lump[3]: 0xc00000c2d8
addr of lump[4]: 0xc00000c2e0
addr of lump[5]: 0xc00000c2e8
addr of lump[6]: 0xc00000c2f0
addr of lump[7]: 0xc00000c2f8

You could move to C/C++ where you could handle all the memory adjustments as the array increases in size. Or build one slice then the other.