在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. 第四章 部署K8s前准备工作

    一.主机准备 1.硬件 准备5台2C/2g/50g虚拟机: Centos7.6系统 2.集群规划 使用10.4.7.0/24网络 IP 主机名 10.4.7.11 hdss7-11.host.com ...

  2. KingbaseES 全局索引是否因为DDL操作而变为Unusable ?

    前言 Oracle 在对分区做DDL操作时,会使分区全局索引失效,需要加上关键字update global indexes.KingbaseES 同样支持全局索引.那么,如果对分区表进行DDL操作,那 ...

  3. KingbaseES R3 受限dba影响集群切换

    ​ 一.受限dba功能说明(参考自官方文档) 受限DBA 受限DBA可以对当前DBA的权限进行一定限制.当功能开启后DBA将不能更改以下对象: Table Database Function(by n ...

  4. KingbaseES 匿名块如何传递参数

    匿名块的基本语法结构包括声明和执行两部分.匿名块每次提交都被重新编译和执行.因为匿名块没有名称并不在数据库中存储,所以匿名块不能直接从其他PL/SQL 块中调用. 定义语法: [ DECLARE ] ...

  5. 【Git进阶】基于文件(夹)拆分大PR

    背景 前段时间为了迁移一个旧服务到新项目,由此产生了一个巨大的PR,为了方便Code Review,最终基于文件夹,将其拆分成了多个较小的PR:现在这里记录下,后面可能还会需要. 演示 为了方便演示, ...

  6. Windows服务器TLS协议

    今天在Windows Admin Center里试图安装扩展插件的时候遇到一个问题.在可用插件里没有任何显示,包括各种微软自己开发的插件. 在Feeds里删除默认的链接,重新添加的时候也会遇到报错.说 ...

  7. WSL 2简介

    Windows Subsystem for Linux(WSL)适用于 Linux 的 Windows 子系统是微软在Windows 10上提供的一项供用户快速运行Linux命令和工具的功能.相比前一 ...

  8. Windows服务器限制进程CPU使用率

    在Windows server 2012 之前的服务系统 2008和2008 R2中有系统资源管理器System Resource Manager可以管理系统的CPU和内存使用情况.特别对于一些自己开 ...

  9. 【Spring】Spring bean中id和name的差异

    id和name都是spring 容器中中bean 的唯一标识符. id: 一个bean的唯一标识 , 命名格式必须符合XML ID属性的命名规范 name: 可以用特殊字符,并且一个bean可以用多个 ...

  10. day01-项目开发流程

    多用户即时通讯系统01 1.项目开发流程 2.需求分析 用户登录 拉取在线用户列表 无异常退出(包括客户端和服务端) 私聊 群聊 发文件 服务器推送新闻/广播 3.设计阶段 3.1界面设计 用户登录: ...