1樓:gao xinge
slice header裡面是unsafe.Pointer,還是uintptr?
在runtime/slice.go裡面,即在slice的定義裡面,但沒有暴露給使用者,使用的是unsafe.Pointer;
在reflect/value.go裡面,即暴露給使用者,用於反射操作,使用的是uintptr。
2. 由於golang的runtime伴有gc或物件記憶體移動,使用reflect.SliceHeader(內部使用uintptr)會不會不安全?
在unsafe/unsafe.go的注釋中明確強調了使用uintptr的危險性,並在case 6中說明了如何使用reflect.SliceHeader才是安全的:
// (6) 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.
//// var s string
// hdr := (*reflect.StringHeader)(unsafe.Pointer(&s)) // case 1
// hdr.Data = uintptr(unsafe.Pointer(pcase 6 (this case)
// hdr.Len = n
//// In this usage hdr.Data is really an alternate way to refer to the underlying
// pointer in the string header, not a uintptr variable itself.
//// In general, reflect.SliceHeader and reflect.StringHeader should be used
// only as *reflect.SliceHeader and *reflect.StringHeader pointing at actual
// slices or strings, never as plain structs.
// A program should not declare or allocate variables of these struct types.
//// // INVALID: a directly-declared header will not hold Data as a reference.
// var hdr reflect.StringHeader
// hdr.Data = uintptr(unsafe.Pointer(p))
// hdr.Len = n
// s := *(*string)(unsafe.Pointer(&hdr)) // p possibly already lost//
go語言中database下sql中的func Open 是長鏈結還是短鏈結
sql.Open 不會建立連線 只會建立乙個DB例項,同時會建立乙個go程來管理該DB例項的乙個連線池 是長連線,但不是在Open的時候建立 在呼叫Begin 取乙個連線,回滾或者提交得時候歸還。如果你直接使用時Exec 執行,則每次會從連線池裡面取出乙個連線,到Exec執行完畢的時候歸還。可以通過...
弱型別語言中資料型別存在的意義?
小烏龜 強型別弱型別,靜態型別動態型別,這些只是概括性的分類而已。php型別不是很強,很多隱含的自動轉換,但是型別資訊確實是存在的,既然存在,有時候就可以加以利用,這是很正常的。 eechen 弱型別語言裡的型別判斷肯定有意義呀.比如不同型別的資料比較,很多時候不僅要比較值是否相等,還要比較型別是否...
c語言中為什麼不能用char 指向字元陣列?
心不變情依舊 char 是二級指標,表示指標的指標,就是把不連續的空間連線起來,第乙個指標指向char 而char 可以表示一階陣列。 喵NLI 因為char z不是指標,是直接存放於棧中的陣列,這種情況下r z與r z都是獲取到z這個陣列的位址而不是二級指標 charz hello char y ...