How to print the memory address of a slice in Golang?

huangapple go评论91阅读模式
英文:

How to print the memory address of a slice in Golang?

问题

我在C语言方面有一些经验,对golang完全不熟悉。

func learnArraySlice() {
    intarr := [5]int{12, 34, 55, 66, 43}
    slice := intarr[:]
    fmt.Printf("the len is %d and cap is %d \n", len(slice), cap(slice))
    fmt.Printf("address of slice 0x%x add of Arr 0x%x \n", &slice, &intarr)
}

现在在golang中,切片是一个数组的引用,它包含指向数组的指针、切片的长度和切片的容量。但是这个切片也会在内存中分配空间,我想打印出那块内存的地址,但是无法做到。

英文:

I have some experience in C and I am totally new to golang.

func learnArraySlice() {
  intarr := [5]int{12, 34, 55, 66, 43}
  slice := intarr[:]
  fmt.Printf("the len is %d and cap is %d \n", len(slice), cap(slice))
  fmt.Printf("address of slice 0x%x add of Arr 0x%x \n", &slice, &intarr)
}

Now in golang slice is a reference of array which contains the pointer to an array len of slice and cap of slice but this slice will also be allocated in memory and i want to print the address of that memory. But unable to do that.

答案1

得分: 50

http://golang.org/pkg/fmt/

fmt.Printf("切片的地址 %p 数组的地址 %p \n", &slice, &intarr)

%p 会打印地址。

英文:

http://golang.org/pkg/fmt/

fmt.Printf("address of slice %p add of Arr %p \n", &slice, &intarr)

%p will print the address.

答案2

得分: 15

切片及其元素是可寻址的:

s := make([]int, 10)
fmt.Printf("第一个元素的地址:%p\n", &s[0])
fmt.Printf("切片本身的地址:%p\n", &s)
英文:

Slices and their elements are addressable:

s := make([]int, 10)
fmt.Printf("Addr of first element: %p\n", &s[0])
fmt.Printf("Addr of slice itself:  %p\n", &s)

答案3

得分: 5

对于切片底层数组和数组的地址(在你的示例中它们是相同的),

package main

import "fmt"

func main() {
    intarr := [5]int{12, 34, 55, 66, 43}
    slice := intarr[:]
    fmt.Printf("切片的长度为 %d,容量为 %d \n", len(slice), cap(slice))
    fmt.Printf("切片的地址为 %p,数组的地址为 %p\n", &slice[0], &intarr)
}

输出结果:

切片的长度为 5,容量为 5 
切片的地址为 0x1052f2c0,数组的地址为 0x1052f2c0
英文:

For the addresses of the slice underlying array and the array (they are the same in your example),

package main

import "fmt"

func main() {
	intarr := [5]int{12, 34, 55, 66, 43}
	slice := intarr[:]
	fmt.Printf("the len is %d and cap is %d \n", len(slice), cap(slice))
	fmt.Printf("address of slice %p add of Arr %p\n", &slice[0], &intarr)
}

Output:

the len is 5 and cap is 5 
address of slice 0x1052f2c0 add of Arr 0x1052f2c0

答案4

得分: 1

package main

import (
	"fmt"
	"reflect"
	"unsafe"
)

func main() {
	intArr := [5]int{1, 2, 3, 4, 5}
	intSlice := intArr[:]
	fmt.Printf("切片地址= %p, 长度= %d, 容量= %d\n", &intSlice, len(intArr), cap(intArr))
	fmt.Printf("数组地址= %p, 长度= %d, 容量= %d\n", &intArr, len(intArr), cap(intArr))
	fmt.Printf("切片第一个元素地址=%p\n", &intSlice[0])
	fmt.Printf("切片的底层数组地址=%p\n", intSlice)
	fmt.Printf("切片的底层数组地址=%#v\n", *((*reflect.SliceHeader)(unsafe.Pointer(&intSlice))))
}

输出:

切片地址= 0xc0000a4018, 长度= 5, 容量= 5
数组地址= 0xc0000b6000, 长度= 5, 容量= 5
切片第一个元素地址=0xc0000b6000
切片的底层数组地址=0xc0000b6000
切片的底层数组地址=reflect.SliceHeader{Data:0xc0000b6000, Len:5, Cap:5}
英文:
package main

import (
	"fmt"
	"reflect"
	"unsafe"
)

func main() {
	intArr := [5]int{1, 2, 3, 4, 5}
	intSlice := intArr[:]
	fmt.Printf("slice addr= %p, len= %d, cap= %d\n", &intSlice, len(intArr), cap(intArr))
	fmt.Printf("array addr= %p, len= %d, cap= %d\n", &intArr, len(intArr), cap(intArr))
	fmt.Printf("slice first elem addr=%p\n", &intSlice[0])
	fmt.Printf("backend array of slice addr=%p\n", intSlice)
	fmt.Printf("backend array of slice addr=%#v\n", *((*reflect.SliceHeader)(unsafe.Pointer(&intSlice))))
}

output:

slice addr= 0xc0000a4018, len= 5, cap= 5
array addr= 0xc0000b6000, len= 5, cap= 5
slice first elem addr=0xc0000b6000
backend array of slice addr=0xc0000b6000
backend array of slice addr=reflect.SliceHeader{Data:0xc0000b6000, Len:5, Cap:5}

huangapple
  • 本文由 发表于 2014年4月2日 20:13:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/22811138.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定