Go-unsafe詳解

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
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 mainimport ("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">結構體的大小: 24unsafe取址:0xc0820045c0結構體取址:0xc0820045c02222nameSize: 16ageSize: 4nameAlignof: 8ageAlignof: 4name位移量: 0age位移量: 16name: chage: 22




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.