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

如果一个函数将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

图片转代码服务由CSDN问答提供 功能建议

示例情况:

有一个全局结构,其中包含一部分结构。

 类型stctUser结构{
用户字符串
特权int 
创建时间。时间
} 
 
type stctAllUsers结构{
 sync.RWMutex 
 slcUsers  [] stctUser 
} 
var strctAllUsers stctAllUsers 
   
 
 

有一个函数希望对用户进行操作,并减少锁定全局结构的时间,我 想要抓住用户并释放锁

  var strctUserTemp stctUser 
strctAllUsers.RLock 
 for:=范围strctAllUsers.slcUsers {
,如果tmpName == strctAllUsers.slcUsers [  a] .user {
 strctUserTemp = strctAllUsers.slcUsers [a] 
 break 
} 
} 
strctAllUsers.RUnlock 
   
 
 

strctUserTemp是否与 是slcUsers [a]的单独副本,还是指向切片的那个元素的指针? 例如,strctAllUsers.slcUsers [a]是“ Tom”,更改strctUserTemp.user =“ Bob”,strctAllUsers.slcUsers [a]仍然是Tom吗?

(之前,似乎 将片段的副本复制到新变量将意味着对该新变量片段的更改可能会更改原始变量...因此它分配了一个指针而不是创建副本。还是我记错了?) \ n

更新:看来我太愚蠢了,无法花五分钟来测试一下……这是我最初对此提出质疑的行为的链接,我想在假设我理解之前先澄清一下实现 发生了什么,并在我正在研究的实际内容中创建了一个错误。 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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题