duan3019 2019-05-02 15:31 采纳率: 100%
浏览 70
已采纳

如果一个函数将slice的一个元素复制到一个变量,那么它是否可以使用指向该元素或副本的指针?

Example situation:

There's a global struct holding a slice of structs.

type stctUser struct {
    user string
    privilege int
    created time.Time
}

type stctAllUsers struct {
    sync.RWMutex
    slcUsers []stctUser
}
var strctAllUsers stctAllUsers

There's a function that wants to operate on the users, and to reduce the time it's locking that global struct, I want to grab a user and release the lock

var strctUserTemp stctUser
strctAllUsers.RLock
for a := range strctAllUsers.slcUsers {
    if tmpName == strctAllUsers.slcUsers[a].user {
        strctUserTemp = strctAllUsers.slcUsers[a]
        break
    }
}
strctAllUsers.RUnlock

Is strctUserTemp working with a separate copy of slcUsers[a], or is it a pointer to that element of the slice? For example, strctAllUsers.slcUsers[a] is "Tom" and changing strctUserTemp.user = "Bob", would strctAllUsers.slcUsers[a] still be Tom?

(Before, it seems that making a copy of a slice to a new variable would mean changes to that new variable slice could change the original...so it assigned a pointer instead of creating a copy. Or am I misremembering?)

Update: Seeing as I was too stupid to take five minutes to test this out...here's a link to the behavior that had me questioning this in the first place, and I wanted to clarify the implementation before assuming I understood what was happening and creating a bug in the actual stuff I was working on. https://play.golang.org/p/ndmJ0h1z-sT

  • 写回答

1条回答 默认 最新

  • doubu5154 2019-05-02 15:39
    关注

    Most importantly: assignment always copies. However, it could be a copy of a pointer.

    There are three basic scenarios:

    1. You have a slice of values. You assign an element from the slice to a local variable, creating a copy of the value. There is no connection between the local variable and the slice element.
    2. You have a slice of pointers. You assign an element from the slice to a local variable, creating a copy of the pointer. Changes to the pointed-to value will be reflected in any other use of the slice element, because the local pointer and the pointer in the slice point to the same memory.
    3. You have a slice of values. You assign a reference to an element from the slice to a local variable (e.g. myVar := &mySlice[0]). The local variable contains a copy of the reference created by the addressing expression. Changes to the local variable's value are reflected by any other use of the slice element, because the local pointer points directly to the memory where that slice element is held.

    Note that the last option means you'll have a fragile pointer - if you append to the slice and the underlying array moves around in memory you'll get some confusing behavior.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划