Go-unsafe詳解

package unsafe

// ArbitraryType is here for the purposes of documentation only and is not actually
// part of the unsafe package.  It represents the type of an arbitrary Go expression.
<span style="color:#ff0000;">type ArbitraryType int</span>

// Pointer represents a pointer to an arbitrary type.  There are four special operations
// available for type Pointer that are not available for other types.
//	1) A pointer value of any type can be converted to a Pointer.
//	2) A Pointer can be converted to a pointer value of any type.
//	3) A uintptr can be converted to a Pointer.
//	4) A Pointer can be converted to a uintptr.
// Pointer therefore allows a program to defeat the type system and read and write
// arbitrary memory. It should be used with extreme care.
<span style="color:#ff0000;">type Pointer *ArbitraryType</span>
unsafe.pointer 指向一個Int類型的地址。存放的是十六進制的地址

<pre name="code" class="csharp">// Sizeof returns the size in bytes occupied by the value v.  The size is that of the
// "top level" of the value only.  For instance, if v is a slice, it returns the size of
// the slice descriptor, not the size of the memory referenced by the slice.
<span style="color:#ff0000;">func Sizeof(v ArbitraryType) uintptr 返回類型在內存所佔的大小</span>

// Offsetof returns the offset within the struct of the field represented by v,
// which must be of the form structValue.field.  In other words, it returns the
// number of bytes between the start of the struct and the start of the field.
<span style="color:#ff0000;">func Offsetof(v ArbitraryType) uintptr 返回成員變量的偏移量</span>

// Alignof returns the alignment of the value v.  It is the maximum value m such
// that the address of a variable with the type of v will always be zero mod m.
// If v is of the form structValue.field, it returns the alignment of field f within struct object obj.
<span style="color:#ff0000;">func Alignof(v ArbitraryType) uintptr 相對於CPU的對齊值</span>

測試代碼:

<pre name="code" class="csharp"><pre name="code" class="csharp">package main

import (
	"fmt"
	"unsafe"
)

type Person struct {
	name string //姓名
	age  uint32 //年齡

}

func main() {
	p := &Person{name: "chenghan", age: 22}
	size := unsafe.Sizeof(*p)
	//	fmt.Println(unsafe.Sizeof(p.name))
	fmt.Println("結構體的大小:", size)

	by := (*[1024]byte)(unsafe.Pointer(p))
	fmt.Printf("unsafe取址:%p\n", unsafe.Pointer(p))
	fmt.Printf("結構體取址:%p\n", p)
	p1 := (*Person)(unsafe.Pointer(p))
	p2 := (*Person)(unsafe.Pointer(by))
	fmt.Println(p1.age)
	fmt.Println(p2.age)
	//偏移量取值
	fmt.Println("nameSize:", unsafe.Sizeof(p.name))
	fmt.Println("ageSize:", unsafe.Sizeof(p.age))
	fmt.Println("nameAlignof:", unsafe.Alignof(p.name))
	fmt.Println("ageAlignof:", unsafe.Alignof(p.age))
	fmt.Println("name偏移量:", unsafe.Offsetof(p.name))
	fmt.Println("age偏移量:", unsafe.Offsetof(p.age))
	fmt.Println("name:", *(*string)(unsafe.Pointer(p)))
	fmt.Println("age:", *(*uint32)(unsafe.Pointer(uintptr(unsafe.Pointer(p)) + unsafe.Offsetof(p.age))))

}

運行結果:

<pre name="code" class="csharp">結構體的大小: 24
unsafe取址:0xc0820045c0
結構體取址:0xc0820045c0
22
22
nameSize: 16
ageSize: 4
nameAlignof: 8
ageAlignof: 4
name偏移量: 0
age偏移量: 16
name: ch
age: 22






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章