为什么指针赋值导致变量赋值不总是坚持?

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

索引的指针分配在 addData(..) 中始终不一致。我预计内存地址会随着底层数组大小的增加而移动。

行为:我赋值给变量A,然后赋值B = A*0.2,然后赋值y = sig(B),最后赋值B = y。有时在下一个循环 B == y || B == A*0.2。它在多次执行中完全一致。

我做了一个更简单更完整的代码版本。

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
}

我希望 p.two.y: [#'s] 的#'s 始终等于 p.one.y: [#'s] 的以下行的#'s。我得到的输出并不总是相等的。有时 p.one.y: [#'s] # 等于前一行的 p.two.y: [#'s];该值被覆盖,然后该值又回来了。

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]

我尝试减少函数嵌套,当我将所有内容放入 Propagate() 函数并使用 sigmoid 函数直接分配给 p.two.y[i] 时,它起作用了。 (下)

// 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)
    }
}

这个版本提供了很好的数据,但带走了很多我喜欢的专业化。

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]

问题出在您构建要引用的切片时的指针分配。地址不断变化。

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])
    }
}

检查未在顺序内存位置中创建的地址。

//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

您可以移动到 C/C++,在那里您可以处理随着数组大小增加而进行的所有内存调整。或者构建一个切片然后另一个。