Skip to content

Commit

Permalink
Update ch2-03-cgo-types.md
Browse files Browse the repository at this point in the history
直接通过指针做强制类型转化,那么指定的 ```数组``` 大小不应该超过原先的数组大小,否则会出现访问到意外的数据。
  • Loading branch information
PaiGack authored Apr 24, 2023
1 parent e0dcbe4 commit 86e36a4
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ch2-cgo/ch2-03-cgo-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,15 @@ func main() {
arr0Hdr.Cap = 10

// 通过切片语法转换
arr1 := (*[31]byte)(unsafe.Pointer(&C.arr[0]))[:10:10]
arr1 := (*[10]byte)(unsafe.Pointer(&C.arr[0]))[:10:10]

var s0 string
var s0Hdr = (*reflect.StringHeader)(unsafe.Pointer(&s0))
s0Hdr.Data = uintptr(unsafe.Pointer(C.s))
s0Hdr.Len = int(C.strlen(C.s))

sLen := int(C.strlen(C.s))
s1 := string((*[31]byte)(unsafe.Pointer(C.s))[:sLen:sLen])
s1 := string((*[5]byte)(unsafe.Pointer(C.s))[:sLen:sLen])
}
```

Expand Down

0 comments on commit 86e36a4

Please sign in to comment.