golang unsafe.Sizeof 函数

unsafe.Sizeof 函数

// Sizeof takes an expression x of any type and returns the size in bytes
// of a hypothetical variable v as if v was declared via var v = x.
// The size does not include any memory possibly referenced by x.
// For instance, if x is a slice, Sizeof returns the size of the slice
// descriptor, not the size of the memory referenced by the slice.
// The return value of Sizeof is a Go constant.

func Sizeof(x ArbitraryType) uintptr

翻译:

参数: 任何类型的表达式 x ;

返回: 假想有一个变量 v , 定义 var v = x , 返回 变量 v 的字节大小;返回值是一个常量;

注:返回的字节大小不包括 x 引用的内存;举个栗子,假设 x 是一个切片,Sizeof(x) 返回的是切片这个结构占用的内存大小,不包括切片引用值的占用内存大小。

各类型 Sizeof 大小

类型

大小

bool

1个字节

intN  uintN  floatN  complexN

N/8个字节(比如 int64是8个字节)

int  uint  uintptr

1个机器字(32位机器4字节,64位机器8字节)

*T

1个机器字

string

2个机器字

[]T 切片

3个机器字

map

1个机器字

func

1个机器字

chan

1个机器字

interface

2个机器字

[...]T 数组, strcut 结构体

至少是所有字段大小的总和(因为存在 内存对齐

认识一下几个特别的类型:

字符串 string

// StringHeader is the runtime representation of a string.
// It cannot be used safely or portably and its representation may
// change in a later release.
// Moreover, the Data field is not sufficient to guarantee the data
// it references will not be garbage collected, so programs must keep
// a separate, correctly typed pointer to the underlying data.

type StringHeader struct {
	Data uintptr
	Len  int
}

StringHeader 是 string 的运行时表示,从源码可以看出,string 实际上是一个由两个字段组成的结构体:

  • Data 是一个地址值,指向底层真正存储数据的内存地址;
  • Len 表示字符串长度;

因此, Sizeof(string) = Sizeof(uintptr) + Sizeof(int) = 2 个机器字。

切片 slice

// SliceHeader is the runtime representation of a slice.
// It cannot be used safely or portably and its representation may
// change in a later release.
// Moreover, the Data field is not sufficient to guarantee the data
// it references will not be garbage collected, so programs must keep
// a separate, correctly typed pointer to the underlying data.

type SliceHeader struct {
	Data uintptr
	Len  int
	Cap  int
}

SliceHeader 是 slice 的运行时表示,可以看出,切片是一个由三个字段组成的结构体:

  • Data 是一个地址值,指向底层数组;
  • Len 表示切片长度;
  • Cap 表示切片容量;

因此, Sizeof(slice) = Sizeof(uintptr) + Sizeof(int) + Sizeof(int) = 3 个机器字。

接口 interface

// emptyInterface is the header for an interface{} value.
type emptyInterface struct {
        typ  *rtype
        word unsafe.Pointer
}

// nonEmptyInterface is the header for an interface value with methods.
type nonEmptyInterface struct {
        // see ../runtime/iface.go:/Itab
        itab *struct {
                ityp *rtype // static interface type
                typ  *rtype // dynamic concrete type
                hash uint32 // copy of typ.hash
                _    [4]byte
                fun  [100000]unsafe.Pointer // method table
        }
        word unsafe.Pointer
}

interface 根据 接口是否含有方法 分别使用 nonEmptyInterface emptyInterface 表示。

  • 两个结构体中 word 字段都表示 实现该接口的类型的实例指针;
  • emptyInterface 里的 typ 是一个结构体指针,rtype 结构体用来描述 类型信息,比如类型大小、对齐方式...等信息;
  • nonEmptyInterface 里的 itab 也是一个结构体指针,itab 结构体描述的信息类似于 c++ 里的虚函数表,主要包含 接口自身定义的类型信息、实际对象类型、函数地址列表...等信息;

因此,两种结构都可用以下表达式计算:

Sizeof(interface) = Sizeof(*T) + Sizeof(*T) = 2 个机器字 (unsafe.Pointer 是一个特殊类型的指针,类似于 c 里面的 void*)。

### Golang 数据类型概述 在 Go 语言中,支持多种内置的数据类型。这些数据类型分为基本类型和复合类型。 #### 基本数据类型 Go 支持以下几种基本数据类型[^3]: - **布尔类型 (`bool`)** 表示真或假的逻辑值。 - **数值类型** - 整形: - `int8`, `int16`, `int32`, `int64`: 有符号整数 - `uint8`, `uint16`, `uint32`, `uint64`: 无符号整数 - `byte`: 同义于 `uint8` - `rune`: 同义于 `int32`,用于表示 Unicode 码点 - `int`, `uint`: 平台相关的默认整型大小 - 浮点型: - `float32`, `float64` - 复杂浮点型: - `complex64`, `complex128` - **字符串 (`string`)** 字符串是一组不可变字符序列,由双引号包围。 #### 查看变量的字节大小和数据类型 为了获取变量的具体信息,在程序运行期间可以通过特定函数来实现这一点。例如,要打印出一个变量的实际占用空间以及其具体类型,可以使用如下方式[^4]: ```go package main import ( "fmt" "unsafe" ) func main() { var i int = 100 fmt.Printf("i 的类型:%T\n", i) fmt.Printf("i 占用的空间大小:%d bytes\n", unsafe.Sizeof(i)) } ``` 这段代码展示了如何利用标准库中的 `fmt` 包来进行格式化输出,并借助 `unsafe.Sizeof()` 函数计算给定表达式的存储需求。 #### 显式类型转换 值得注意的是,在 Go 中不同类型之间的赋值操作不会发生隐含的自动转型;相反地,程序员必须通过显示的方式完成这一过程。对于简单的数值类型间转化,遵循这样的模式[^5]: ```go var a uint8 = 255 var b int = int(a) // 将 uint8 转换为 int 类型 ``` 这种设计有助于减少潜在错误并提高代码的安全性和可读性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值