golang的make和new的分析

golang makenew的源代码

make 和 new 都是进行分配空间

// The make built-in function allocates and initializes an object of type
// slice, map, or chan (only). Like new, the first argument is a type, not a
// value. Unlike new, make's return type is the same as the type of its
// argument, not a pointer to it. The specification of the result depends on
// the type:
//  Slice: The size specifies the length. The capacity of the slice is
//  equal to its length. A second integer argument may be provided to
//  specify a different capacity; it must be no smaller than the
//  length. For example, make([]int, 0, 10) allocates an underlying array
//  of size 10 and returns a slice of length 0 and capacity 10 that is
//  backed by this underlying array.
//  Map: An empty map is allocated with enough space to hold the
//  specified number of elements. The size may be omitted, in which case
//  a small starting size is allocated.
//  Channel: The channel's buffer is initialized with the specified
//  buffer capacity. If zero, or the size is omitted, the channel is
//  unbuffered.
func make(t Type, size ...IntegerType) Type

make的第一个参数是类型, 第二个参数是分配的长度,返回值是一个 引用类型。其实就是 初始化+分配空间

注意:map使用make声明时, make(map_type, 0)这样声明不会产生多余的内存,如果不加0,golang会在初始化时分配一个小尺寸内存。

// The new built-in function allocates memory(分配内存). The first argument is a type,
// not a value, and the value returned is a pointer to a newly
// allocated zero value of that type.
func new(Type) *Type

内置函数new作用是分配内存。接受的参数是一个 类型,返回的是一个新的分配的 零值的指针,只进行声明,给出一个地址,没有初始化。如下代码所示:

package main

import "fmt"

//func main() {
//  var i *int
//  *i = 10
//  fmt.Println(*i)
//  //out:panic: runtime error: invalid memory address or nil pointer dereference
//}

func main() {
    var i = new(int)
    *i = 10
    //以上两句:等同于 i := 10
    //new不常用,通常采用短语句及结构体的字面量获取其内存地址
    fmt.Println(*i)
    //out: 10
}

goalng 的内存分配

  • 值类型的声明不需要分配内存,go语言默认分配其内存
  • 对于引用类型的声明的同时需要分配内存空间,go提供了new和make两种方法

golang 局部变量为何能返回?

goalng 内存管理

  • 首先这是不同于C语言系列,变量栈堆分配是严格的
  • golang的变量分配是根据逃逸分析进行分配其是在栈上还是在堆上
package main

func foo() *int {
    var x int
    return &x
}

func bar() int {
    x := new(int)
    *x = 1
    return *x
}

func main() {
    
}
//out:
// $ go run -gcflags '-m -l' demo.go
// # command-line-arguments
// .\demo.go:5:9: &x escapes to heap
// .\demo.go:4:6: moved to heap: x
// .\demo.go:9:10: bar new(int) does not escape

猜你喜欢

转载自www.cnblogs.com/joelang/p/12095213.html