为什么指针赋值会导致变量赋值不总是坚持? [英] Why does pointer assignment cause variable assignment to not always stick?

查看:49
本文介绍了为什么指针赋值会导致变量赋值不总是坚持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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
}

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

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]

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

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]

推荐答案

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

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

您可以转向 C/C++,在那里您可以随着数组大小的增加处理所有内存调整.或者先构建一个切片,然后再构建另一个.

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.

这篇关于为什么指针赋值会导致变量赋值不总是坚持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆