在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的使用的更多相关文章

  1. C语言嵌入式系统编程修炼之三:内存操作

    数据指针 在嵌入式系统的编程中,常常要求在特定的内存单元读写内容,汇编有对应的MOV指令,而除C/C++以外的其它编程语言基本没有直接访问绝对地址的能力.在嵌入式系统的实际调试中,多借助C语言指针所具 ...

  2. 【C/C++】C语言嵌入式编程修炼·背景篇·软件架构篇·内存操作篇

    C 语言嵌入式系统编程修炼之一:背景篇 不同于一般形式的软件编程,嵌入式系统编程建立在特定的硬件平台上,势必要求其编程语言具备较强的硬件直接操作能力.无疑,汇编语言具备这样的特质.但是,归因于汇编语言 ...

  3. C# unsafe模式内存操作深入探索

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Run ...

  4. C语言学习笔记--内存操作常见错误

    1. 野指针 (1)指针变量中的值是非法的内存地址,进而形成野指针 (2)野指针不是 NULL 指针,是指向不可用内存地址的指针 (3)NULL 指针并无危害,很好判断,也很好调试 (4)C 语言中无 ...

  5. C语言处理字符串及内存操作

    字符串处理函数 1.字符串长度 strlen表示包含的字符的个数,size_t strlen(char cosnt *string), 返回的是size_t类型,它是无符号整数类型,在表达式中进行运算 ...

  6. C语言字符串操作总结大全(超详细)

    本篇文章是对C语言字符串操作进行了详细的总结分析,需要的朋友参考下 1)字符串操作  strcpy(p, p1) 复制字符串  strncpy(p, p1, n) 复制指定长度字符串  strcat( ...

  7. C语言字符串操作常用库函数

    C语言字符串操作常用库函数 *********************************************************************************** 函数 ...

  8. iOS学习08之C语言内存管理

    本次主要学习和理解C语言中的内存管理 1.存储区划分 按照地址从高到低的顺序:栈区,堆区,静态区,常量区,代码区 1> 栈区:局部变量的存储区域 局部变量基本都在函数.循环.分支中定义 栈区的内 ...

  9. 【转】《深入理解计算机系统》C程序中常见的内存操作有关的典型编程错误

    原文地址:http://blog.csdn.net/slvher/article/details/9150597 对C/C++程序员来说,内存管理是个不小的挑战,绝对值得慎之又慎,否则让由上万行代码构 ...

  10. C 语言文件操作

    C 语言文件操作 1. 数据流:     程序与数据的交互以流的形式进行.fopen 即打开数据流,fclose 即刷新数据流.     所谓数据流,是一种抽象,表示这段数据像流一样,需要逐步接收,不 ...

随机推荐

  1. Linux之搭建FTP服务

    引用:FTP服务器(File Transfer Protocol Server)是在互联网上提供文件存储和访问服务的计算机,它们依照FTP协议提供服务. FTP是File Transfer Proto ...

  2. Ros入门21讲

    一.ROS是什么? ROS=通信机制+开发工具+应用功能+生态系统 目的:提高机器人研发中的软件复用率. 1.ROS中的通信机制 松耦合分布式通信: 注意:什么是耦合.紧耦合.松耦合? 1.1 耦合 ...

  3. springboot 集成 docsify 实现随身文档

    需求分析 文档可以和项目一起进行版本管理 文档可以在线访问 文档可以与springboot项目集成,不需要分开部署 MarkDown支持 文档跟随,打包jar也可以访问 技术选型 对于网上已有的方案, ...

  4. haodoop新特性

    Hadoop2.x新特性 scp实现两个远程主机之间的文件复制 scp -r hello.txt root@hadoop103:/user/atguigu/hello.txt // 推 push sc ...

  5. Python——索引与切片

    #索引与切片 ##1.序列 序列:list,tuple,str 其中list是可变序列 typle,str是不可变序列 #修改序列的值 list = [3,4,5] tup = (3,4,5) str ...

  6. Java程序设计(三)作业

    题目1:用户输入学号,如果是以ccutsoft开头,并且后边是4位数字,前两位大于06小于等于当前年份.判断用户输入是否合法.ccutsoft_0801. 1 //题目1:用户输入学号,如果是以abc ...

  7. 条件期望:Conditional Expectation 举例详解之入门之入门之草履虫都说听懂了

    我知道有很多人理解不了 "条件期望" (Conditional Expectation) 这个东西,有的时候没看清把随机变量看成事件,把 \(\sigma\)-algebra 看成 ...

  8. C++面向对象编程之类的写法

    1.头文件中的防卫式声明 例如自己要建复数complex类,可以建立一个complex.h的头文件. 头文件中必须先写防卫式声明: #ifndef __COMPLEX__ #define __COMP ...

  9. python-绘图与可视化

    python 有许多可视化工具,但本书只介绍Matplotlib.Matplotlib是一种2D的绘图库,它可以支持硬拷贝和跨系统的交互,它可以在python脚本,IPython的交互环境下.Web应 ...

  10. 记一次某制造业ERP系统 CPU打爆事故分析

    一:背景 1.讲故事 前些天有位朋友微信找到我,说他的程序出现了CPU阶段性爆高,过了一会就下去了,咨询下这个爆高阶段程序内部到底发生了什么? 画个图大概是下面这样,你懂的. 按经验来说,这种情况一般 ...