目录
前言
newmakepanic
new的使用
new
func main() {
var a *int
fmt.Println(a) // nil
*a = 123 //panic
fmt.Println(a)
}
varnil
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x10a9043]
综上可以总结出初始化一个指针变量,其值为nil,nil的值是不能直接赋值的。
既然我们知道了没有为其分配内存,那么我们使用new分配一个吧。代码修改后:
func main() {
var a *int
a = new(int)
fmt.Printf("a type is :%T,a point value is :%v,a value is:%v,a size is: %v\n", a, a, *a, unsafe.Sizeof(a))
//a type is :*int,a point value is :0xc00001a0a0,a value is:0,a size is: 8
*a = 123
fmt.Printf("a type is :%T,a point value is :%v,a value is:%v,a size is: %v\n", a, a, *a, unsafe.Sizeof(a))
//a type is :*int,a point value is :0xc00001a0a0,a value is:123,a size is: 8
}
new
底层实现
new
func new(Type) *Type
Typenew
runtime.newobject
func newobject(typ *_type) unsafe.Pointer {
return mallocgc(typ.size, typ, true)
}
mallocgctyp.size
make的使用
makenewslicemapchannel
注意:这三种类型都是引用类型,所以没必要返回他们的指针了,必须得初始化,但是不是设置为零值。
我们通过一个示例看一下:
func test() {
var s *[]int
fmt.Printf("s: %p %#v \n", &s, s) //s: 0xc00000e028 (*[]int)(nil)
s = new([]int)
fmt.Printf("s: %p %#v \n", &s, s) //s: 0xc00000e028 &[]int(nil)
(*s)[0] = 8
fmt.Printf("s: %p %#v \n", &s, s) //panic: runtime error: index out of range [0] with length 0
}
我们先用new进行初始化,会给引用类型初始化为nil,nil是不能直接赋值的。下面改为make。
func test() {
var s = make([]int, 5)
fmt.Printf("s: %p %#v \n", &s, s) //s: 0xc00000c060 []int{0, 0, 0, 0, 0}
s[0] = 8
fmt.Printf("s: %p %#v \n", &s, s) //s: 0xc00000c060 []int{8, 0, 0, 0, 0}
}
mapchannel
底层实现
make
func make(t Type, size ...IntegerType) Type
可以看到make返回的是复合类型本身。
make在申请slice内存时,底层调用的是runtime.makeslice,
func makeslice(et *_type, len, cap int) unsafe.Pointer {
mem, overflow := math.MulUintptr(et.size, uintptr(cap))
if overflow || mem > maxAlloc || len < 0 || len > cap {
mem, overflow := math.MulUintptr(et.size, uintptr(len))
if overflow || mem > maxAlloc || len < 0 {
panicmakeslicelen()
}
panicmakeslicecap()
}
return mallocgc(mem, et, true)
}
makeslicemallocgcMulUintptr容量captype.sizmakemapchannelruntime.makemap_smallruntime.makechanmallocgc
总结
- make和new都是golang用来分配内存的函数,且在堆上分配内存,make 即分配内存,也初始化内存。new只是将内存清零,并没有初始化内存。
- make返回的还是引用类型本身;而new返回的是指向类型的指针。
- make只能用来分配及初始化类型为slice,map,channel的数据;new可以分配任意类型的数据。
您可能感兴趣的文章: