go - Golang atomic.StorePointer(...) 行为怪异
问题描述
尝试使用 atomic 包并得到一个很奇怪的行为,可能不理解规范......
游乐场: https: //play.golang.org/p/oPeqwETBpuZ
代码:
func main() {
x := 1
y := 2
xp := &x
yp := &y
fmt.Println("xp before: ", xp)
fmt.Println("yp before: ", yp)
xpu := (unsafe.Pointer)(xp)
ypu := (unsafe.Pointer)(yp)
atomic.StorePointer(&xpu, ypu)
fmt.Println("xp after: ", xp)
fmt.Println("yp after: ", yp)
}
输出:
xp before: 0xc000100010
yp before: 0xc000100018
xp after: 0xc000100010
yp after: 0xc000100018
预期的:
xp before: 0xc000100010
yp before: 0xc000100018
xp after: 0xc000100018
yp after: 0xc000100018
我做错了什么?
解决方案
正确的方式: https: //play.golang.org/p/__P_5E4di79
事实上,函数需要 **int 强制转换为 *unsafe.Pointer
推荐阅读
- javascript - 如何允许函数访问循环中的所有变量并使用 javascript 执行它们?
- css - 如何将订单状态类添加到正文标签?
- c++ - 我可以从使用 Visual Studio 2017 构建的 C++ 程序中使用 Access Database Engine 2010 吗?
- c++ - 悬空指针(我认为)导致程序崩溃?
- python - Dlib 已安装但无法打开 - Mac M1
- python - 在 Numpy 中,如何获得与 arr 百分位数相同的值,但包括负数?
- python - 如何在两个python类实例之间执行求和并获取每个实例变量的长度列表
- paging - 如何使用页表基址寄存器 (PTBR) 使用页码定位正确的帧?
- node.js - 反应错误:找不到模块'./common'
- reactjs - 错误:每次调整屏幕大小后,呈现的挂钩数量少于预期