关于slice的一点思考

本文所写的代码是在Mac OSX+Intellij IDEA 15 开发环境下调试编译通过

主要是拿两个例子来我说下对slice的理解
example1:
1.s:=[]int {5}
2.s=
append(s,7)
3.s=
append(s,9)
4.x:=
append(s,11)
5.y:=
append(s,12)
fmt.Println(s,x,y)
输出的结果为:
[5 7 9] [5 7 9 12] [5 7 9 12]
 首先,slice其本质上是一个结构体,如下:
type slice struct {
       array unsafe.Pointer
       len   int
       cap   int
}
array是一个指向存放数据的内存单元(称之为data0)的指针,len为底层数组的长度,cap为底层数组的容量
line1:初始化一个切片s,存放int型数据‘5’,此时的len=1,cap=1;

line2:在切片s上添加一个元素‘7’,由于len=cap,故需要扩容,重新开辟一块内存单元data1(data1的大小是data0的两倍),然后将data0中的数据拷贝到data1上来,此时len=2,cap=2,array也指向data1

line3:在s上再添加一个元素‘9’,由于len=cap,故需要扩容,原理同上(data2=2*data1),此时len=3,cap=4, array指向data2
注意:line1代码执行完成后,再执行line2的代码,由于要重新开辟一块内存,故array的值发生了改变;同理,line2~line3,array也发生了改变。

line4:在s上添加一个元素‘11’,此时len<cap,故不需要扩容,此时s的len=3,cap=4  不过新定义的切片x的len=4,cap=4, 两者array指向同一块内存单元data2

line5:在s上添加一个元素‘12’,此时len<cap,故不需要扩容,同理,不过新定义的切片y覆盖了原来的x

测试代码如下:
package main

import "fmt"

func main() {
       s:=[]int {5}
       fmt.Printf("The addr of s is: %p\n",s)

       s=append(s,7)
       fmt.Printf("The addr of s is: %p\n",s)

       s=append(s,9)
       fmt.Printf("The addr of s is: %p\n",s)

       x:=append(s,11)
       fmt.Printf("The addr of s is: %p\n",s)
       fmt.Printf("The addr of x is: %p\n",x)
       fmt.Printf("The addr of x[0] is: %p\n",&x[0])

       y:=append(s,12)
       fmt.Printf("The addr of s is: %p\n",s)
       fmt.Printf("The addr of y is: %p\n",y)
       fmt.Printf("The addr of y[0] is: %p\n",&y[0])

       fmt.Println(s,x,y)
}

结果如下:
The addr of s is: 0x8201ca280
The addr of s is: 0x8201ca290
The addr of s is: 0x8201d0180
The addr of s is: 0x8201d0180
The addr of x is: 0x8201d0180
The addr of x[0] is: 0x8201d0180
The addr of s is: 0x8201d0180
The addr of y is: 0x8201d0180
The addr of y[0] is: 0x8201d0180
[5 7 9] [5 7 9 12] [5 7 9 12]

总结:利用append函数对切片进行元素添加时,判断扩容与否的标准是第一个参数所对应的结构中len是否小于cap
当然假如在实际项目中实在是需要新创建一个切片,并将添加元素后的s赋给新的切片,一般的做法是:
newSlice:=make([ ]int)
copy(newSlice , s)

example2:

s1:=[]int {5,7,9,11}
s2:=s1[1:2]
s1=append(s1,11)
fmt.Println(s1,s2)

上面的例子很简单,不妨先在脑袋里面跑一遍代码。。

结果如下:
[5 7 9 11 11] [ 7 ]
分析:
分析情况同上,line1:创建并初始化一个切片s1,此时len=4,cap=4,array指向data0;

line2:将s1中的第1个元素开始的一个元素 (2-1=1) 创建新的切片s2,s1的array仍然指向data0,但是s2的array指向新开辟出来的内存单元data1,另外此时s2的cap=3,而不是我们认为的1;

line3:向s1中添加新的元素‘11’,同理,需要扩容,array指向新的内存单元data2

line4:输出s1 s2

测试代码如下:

s1:=[]int {5,7,9,11}
fmt.Println(len(s1),cap(s1))
fmt.Printf("The addr of s1 is: %p\n",s1)
fmt.Printf("The addr of s1[1] is: %p\n",&s1[1])
fmt.Println("\n")

s2:=s1[1:2]
fmt.Println(len(s2),cap(s2))

fmt.Printf("The addr of s1 is: %p\n",s1)
fmt.Printf("The addr of s2 is: %p\n",s2)
fmt.Printf("The addr of s2[0] is: %p\n",&s2[0])
fmt.Println("\n")

s1=append(s1,11)
fmt.Println(len(s1),cap(s1))
fmt.Printf("The addr of s1 is: %p\n",s1)
fmt.Printf("The addr of s2 is: %p\n",s2)
fmt.Println("\n")

fmt.Println(s1,s2)
fmt.Printf("The addr of s1 is: %p\n",s1)
fmt.Printf("The addr of s2 is: %p\n",s2)

显示结果如下:

4 4
The addr of s1 is: 0x8201d0120
The addr of s1[1] is: 0x8201d0128


1 3
The addr of s1 is: 0x8201d0120
The addr of s2 is: 0x8201d0128
The addr of s2[0] is: 0x8201d0128


5 8
The addr of s1 is: 0x8201d41c0
The addr of s2 is: 0x8201d0128


[5 7 9 11 11] [7]
The addr of s1 is: 0x8201d41c0
The addr of s2 is: 0x8201d0128

分析结果可以得知,基于数组切片创建切片时需要注意两点:
1.新创建的切片结构体中的array指向的是切片开始的第一个元素(s1[1:2]里面的1)
2.新创建的切片的cap并不是依赖于原切片内元素的个数,而是等于原切片cap的大小-切片起始位置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值