dongtang5229 2018-08-18 20:41
浏览 54
已采纳

如何在Go中处理切片的副本?

mapArray is a 2D slice of float32. I make a copy of it so I can work on the copy without modifying mapArray. However, it's not the case. Assigning a value to Origin modifies mapArray.

origins := it.Empty2DArray(len(mapArray))
copy(origins, mapArray)
origins[5][5] = -1

Doing that makes mapArray[5][5] to be -1 instead of its original value.

How can i make a true independent copy of the slice?

Thanks.

Edit:

// Empty2DArray returns a zeroed 2D array.
func Empty2DArray(arraySize int) [][]float32 {
    emptyArray := make([][]float32, arraySize)
    for y := 0; y < arraySize; y++ {
        row := make([]float32, arraySize)
        for x := 0; x < arraySize; x++ {
            row[x] = 0
        }
        emptyArray[y] = row
    }
    return emptyArray
}
  • 写回答

1条回答 默认 最新

  • duanjing1276 2018-08-18 20:52
    关注

    A 2D slice is a slice of slices. In your function you allocate one slice to hold the others, then for each slice you allocate memory to hold that line of data. To copy this, you need to copy all of these data lines plus the overall slice.

    When you say copy(origins, mapArray), what you really do is make a copy of a slice of pointers to the original data. You do not however copy the original data.

    I would suggest that instead of doing a nested for-loop for copying the sub-slices, instead just use a one-dimensional slice and create wrapper functions to index into it. This is more memory efficient and you can use the built-in copy.

    Here is a sample of what I would do instead:

    package main
    
    import "fmt"
    
    type squareMat struct {
        size int
        data []float32
    }
    
    func newSquareMat(size int) *squareMat {
        return &squareMat{
            size: size,
            data: make([]float32, size*size),
        }
    }
    
    func (s *squareMat) get(i, j int) float32 {
        return s.data[i+j*s.size]
    }
    
    func (s *squareMat) set(i, j int, to float32) {
        s.data[i+j*s.size] = to
    }
    
    func (s *squareMat) copy() *squareMat {
        c := newSquareMat(s.size)
        copy(c.data, s.data)
        return c
    }
    
    func main() {
        m := newSquareMat(5)
        m.set(2, 3, 1.5)
        n := m.copy()
        n.set(2, 3, 99)
        fmt.Println(m.get(2, 3))
        fmt.Println(n.get(2, 3))
    }
    

    If you instead insist on using 2D float32 arrays, here is how to copy that:

    package main
    
    import "fmt"
    
    func copy2D(x [][]float32) [][]float32 {
        c := make([][]float32, len(x))
        for i := range c {
            c[i] = make([]float32, len(x[i]))
            copy(c[i], x[i])
        }
        return c
    }
    
    func main() {
        a := [][]float32{
            []float32{1, 2, 3},
            []float32{4, 5, 6},
            []float32{7, 8, 9},
        }
        b := copy2D(a)
        b[1][1] = 99
        fmt.Println(a)
        fmt.Println(b)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?