GO语言内存操作指导—unsafe的使用
在unsafe包里面,官方的说明是:A uintptr is an integer, not a reference
.Converting a Pointer to a uintptr creates an integer value with no pointer semantics. Even if a uintptr holds the address of some object, the garbage collector will not update that uintptr's value if the object moves
, nor will that uintptr keep the object from being reclaimed.
实际上Uintptr是一个int类型的变量,不是一个引用,没有pointer的语义
INVALID: uintptr cannot be stored in variable before conversion back to Pointer.
1
|
u := uintptr(p) |
2
|
p = unsafe.Pointer(u + offset) |
1 官方只建议在如下几种情况下可以使用uintptr
The remaining patterns enumerate the only valid conversions from uintptr to Pointer.
1.1 If p points into an allocated object, it can be advanced through the object by conversion to uintptr, addition of an offset, and conversion back to Pointer.
就是在需要用到地址偏移的操作的时候,将unsafe.pointer转换成uintptr,添加偏移之后,再转回来。
1
|
p = unsafe.Pointer(uintptr(p) + offset) |
2
|
|
3
|
// The most common use of this pattern is to access fields in a struct |
4
|
// or elements of an array: |
5
|
// |
6
|
// // equivalent to f := unsafe.Pointer(&s.f) |
7
|
f := unsafe.Pointer(uintptr(unsafe.Pointer(&s)) + unsafe.Offsetof(s.f)) |
8
|
// |
9
|
// // equivalent to e := unsafe.Pointer(&x[i]) |
10
|
e := unsafe.Pointer(uintptr(unsafe.Pointer(&x[0])) + i*unsafe.Sizeof(x[0])) |
1.2 Conversion of a Pointer to a uintptr when calling syscall.Syscall
这种我们不涉及
1.3 Conversion of the result of reflect.Value.Pointer or reflect.Value.UnsafeAddr from uintptr to Pointer.
包里面对这一块说明的很清楚:
Package reflect’s Value methods named Pointer and UnsafeAddr return type uintptr instead of unsafe.Pointer to keep callers from changing the result to an arbitrary type without first importing "unsafe". However, this means that the result is fragile and must be converted to Pointer immediately after making the call
由于这种方法返回的不是unsafe.pointer, 因此需要立马转回unsafe.pointer,再需要做这样的操作的时候。
1
|
p := (*int)(unsafe.Pointer(reflect.ValueOf(new(int)).Pointer())) |
2
|
|
3
|
// INVALID: uintptr cannot be stored in variable |
4
|
// before conversion back to Pointer. |
5
|
u := reflect.ValueOf(new(int)).Pointer() |
6
|
p := (*int)(unsafe.Pointer(u)) |
1.4 Conversion of a reflect.SliceHeader or reflect.StringHeader Data field to or from Pointer.
As in the previous case, the reflect data structures SliceHeader and StringHeader declare the field Data as a uintptr to keep callers from changing the result to an arbitrary type without first importing “unsafe”. However, this means that SliceHeader and StringHeader are only valid when interpreting the content of an actual slice or string value.
这个用法只在想要获取slice或者string这两个数据类型结构体的各个字段的值的时候是有效的。并不能做其它的使用。
1
|
// In this usage hdr.Data is really an alternate way to refer to the underlying |
2
|
// pointer in the string header, not a uintptr variable itself. |
3
|
var s string |
4
|
hdr := (*reflect.StringHeader)(unsafe.Pointer(&s)) // case 1 |
5
|
hdr.Data = uintptr(unsafe.Pointer(p)) // case 6 (this case) |
6
|
hdr.Len = n |
7
|
// |
8
|
// INVALID: a directly-declared header will not hold Data as a reference. |
9
|
var hdr reflect.StringHeader |
10
|
hdr.Data = uintptr(unsafe.Pointer(p)) |
11
|
dr.Len = n |
12
|
s := *(*string)(unsafe.Pointer(&hdr)) // p possibly already lost |
这种方法虽然官方建议,但本身就是不安全的
。不建议使用,在go语言之后的版本这种用法还会被替换掉
1
|
// StringHeader is the runtime representation of a string. |
2
|
// It cannot be used safely or portably and its representation may |
3
|
// change in a later release. |
4
|
// Moreover, the Data field is not sufficient to guarantee the data |
5
|
// it references will not be garbage collected, so programs must keep |
6
|
// a separate, correctly typed pointer to the underlying data. |
7
|
type StringHeader struct { |
8
|
Data uintptr |
9
|
Len int |
10
|
} |
11
|
|
12
|
// stringHeader is a safe version of StringHeader used within this package. |
13
|
type stringHeader struct { |
14
|
Data unsafe.Pointer |
15
|
Len int |
16
|
} |
2 现在我们的使用方法达成的一个共识是uintptr不能作为临时变量保存
我们在需要操作地址偏移的时候,再将unsafe.pointer转换成uintptr。其它指针的传递只能都使用unafe.pointer.
就拿取切片的首地址的操作来说:
2.1先获取切片底层数组的首地址的unsafe.pointer
2.2将unsafe.pointer作为值转递
2.3在使用uintptr做地址偏移的时候,千万不要保存成局部变量
GO语言内存操作指导—unsafe的使用的更多相关文章
- C语言嵌入式系统编程修炼之三:内存操作
数据指针 在嵌入式系统的编程中,常常要求在特定的内存单元读写内容,汇编有对应的MOV指令,而除C/C++以外的其它编程语言基本没有直接访问绝对地址的能力.在嵌入式系统的实际调试中,多借助C语言指针所具 ...
- 【C/C++】C语言嵌入式编程修炼·背景篇·软件架构篇·内存操作篇
C 语言嵌入式系统编程修炼之一:背景篇 不同于一般形式的软件编程,嵌入式系统编程建立在特定的硬件平台上,势必要求其编程语言具备较强的硬件直接操作能力.无疑,汇编语言具备这样的特质.但是,归因于汇编语言 ...
- C# unsafe模式内存操作深入探索
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Run ...
- C语言学习笔记--内存操作常见错误
1. 野指针 (1)指针变量中的值是非法的内存地址,进而形成野指针 (2)野指针不是 NULL 指针,是指向不可用内存地址的指针 (3)NULL 指针并无危害,很好判断,也很好调试 (4)C 语言中无 ...
- C语言处理字符串及内存操作
字符串处理函数 1.字符串长度 strlen表示包含的字符的个数,size_t strlen(char cosnt *string), 返回的是size_t类型,它是无符号整数类型,在表达式中进行运算 ...
- C语言字符串操作总结大全(超详细)
本篇文章是对C语言字符串操作进行了详细的总结分析,需要的朋友参考下 1)字符串操作 strcpy(p, p1) 复制字符串 strncpy(p, p1, n) 复制指定长度字符串 strcat( ...
- C语言字符串操作常用库函数
C语言字符串操作常用库函数 *********************************************************************************** 函数 ...
- iOS学习08之C语言内存管理
本次主要学习和理解C语言中的内存管理 1.存储区划分 按照地址从高到低的顺序:栈区,堆区,静态区,常量区,代码区 1> 栈区:局部变量的存储区域 局部变量基本都在函数.循环.分支中定义 栈区的内 ...
- 【转】《深入理解计算机系统》C程序中常见的内存操作有关的典型编程错误
原文地址:http://blog.csdn.net/slvher/article/details/9150597 对C/C++程序员来说,内存管理是个不小的挑战,绝对值得慎之又慎,否则让由上万行代码构 ...
- C 语言文件操作
C 语言文件操作 1. 数据流: 程序与数据的交互以流的形式进行.fopen 即打开数据流,fclose 即刷新数据流. 所谓数据流,是一种抽象,表示这段数据像流一样,需要逐步接收,不 ...
随机推荐
- 完全解析Array.apply(null, { length: 1000 })
Array.apply(null, { length: 1000 }) 点击打开视频讲解更加详细 在阅读VueJS教程时有这么段demo code: render: function (createE ...
- http服务(postman调用方法及反参)
#region 监听url #region 监听url路径请求 static HttpListener httpobj; private void listeningUrl() { //提供一个简单的 ...
- 输入法词库解析(六)QQ 拼音分类词库.qpyd
详细代码:https://github.com/cxcn/dtool 前言 .qpyd 是 QQ 拼音输入法 6.0 以下版本所用的词库格式,可以在 http://cdict.qq.pinyin.cn ...
- Java中的SPI原理浅谈
在面向对象的程序设计中,模块之间交互采用接口编程,通常情况下调用方不需要知道被调用方的内部实现细节,因为一旦涉及到了具体实现,如果需要换一种实现就需要修改代码,这违反了程序设计的"开闭原则& ...
- filebeat直接给es传输日志,自定义索引名
ElasticStack从2019年1月29日的6.6.0版本的开始,引入了索引生命周期管理的功能,新版本的Filebeat则默认的配置开启了ILM,导致索引的命名规则被ILM策略控制. 加上这个配置 ...
- 第1篇----Istio原理篇
Istio是什么 ◎ Istio是一个用于服务治理的开放平台. ◎ Istio是一个Service Mesh形态的用于服务治理的开放平台. ◎ Istio是一个与Kubernetes紧密结合的适用于云 ...
- CentOS 7.7系统安装Redis 6.0.3
前提操作 避免出现如下的错误 yum -y install gcc tcl yum -y install centos-release-scl yum -y install devtoolset-9- ...
- haproxy + keeplived
两台主机: 192.168.2.163 192.168.2.165 # yum安装haproxy yum install haproxy # cat /etc/haproxy/haproxy.cfg ...
- flask-bootstrap 模版中所需的CSS/JS文件实现本地引入
Flask-Bootstrap默认是加载CDN的css与js文件,每次刷新页面都要访问到外网的cdn来获取css与js文件; 模版扩展来自于bootstrap/base.html,就以bootstra ...
- 我的 Kafka 旅程 - 文件存储机制
存储机制 Topic在每个Broker下存储所属的Partition,Partition下由 Index.Log 两类文件组成. 写入 Log 由多个Segment文件组成,每个Segment文件容量 ...