unsafe.Pointer to byte slice

2,937 views
Skip to first unread message

Santhosh Ram Manohar

unread,
Jul 8, 2017, 6:31:24 PM7/8/17
to golang-nuts
hello,

This piece of code prints [10 0 0 0] as expected..

func main() {
        i := 10
        p := unsafe.Pointer(&i)
        intPtr := (*[4]byte)(p)
        fmt.Println(*intPtr)
}

But if I convert the unsafe.Pointer to pointer to a byte slice rather than an array of 4 bytes it prints an empty slice, []

func main() {
        i := 10
        p := unsafe.Pointer(&i)
        intPtr := (*[]byte)(p)
        fmt.Println(*intPtr)
}

Can someone explain why its so ? thanks.

-Santhosh.



Dave Cheney

unread,
Jul 8, 2017, 7:09:32 PM7/8/17
to golang-nuts
An array is a vector of values in memory. A slice is a small struct that describes an array stored elsewhere in memory.

https://blog.golang.org/go-slices-usage-and-internals

Santhosh Ram Manohar

unread,
Jul 8, 2017, 9:59:55 PM7/8/17
to golang-nuts
Dave,

On Saturday, July 8, 2017 at 4:09:32 PM UTC-7, Dave Cheney wrote:
An array is a vector of values in memory. A slice is a small struct that describes an array stored elsewhere in memory.

I understand the slice vs array difference. But in this statement,

intPtr := (*[]byte)(p)

my interpretation was intPtr is a pointer to a slice of bytes and slice's underlying array reference is the 4 bytes of the integer, isn't it ?

 

https://blog.golang.org/go-slices-usage-and-internals

Ian Lance Taylor

unread,
Jul 8, 2017, 10:37:25 PM7/8/17
to Santhosh Ram Manohar, golang-nuts
On Sat, Jul 8, 2017 at 6:59 PM, Santhosh Ram Manohar
<santho...@gmail.com> wrote:
>
> On Saturday, July 8, 2017 at 4:09:32 PM UTC-7, Dave Cheney wrote:
>>
>> An array is a vector of values in memory. A slice is a small struct that
>> describes an array stored elsewhere in memory.
>
>
> I understand the slice vs array difference. But in this statement,
>
> intPtr := (*[]byte)(p)
>
> my interpretation was intPtr is a pointer to a slice of bytes and slice's
> underlying array reference is the 4 bytes of the integer, isn't it ?

A slice is not a backing array. A pointer to a slice is not a pointer
to the slice's backing array.

Ian

Santhosh Ram Manohar

unread,
Jul 8, 2017, 11:11:34 PM7/8/17
to Ian Lance Taylor, golang-nuts
So in this case, a pointer to 4 bytes of memory is being interpreted as pointer to a slice. When the slice is referenced, runtime sees its not actually a slice and hence treats it as if its a nil slice ?

thanks,
Santhosh.

 

Ian

Dave Cheney

unread,
Jul 9, 2017, 3:30:06 AM7/9/17
to golang-nuts
As your using the unsafe package your doing things which are not considered safe by the lanaguge so only imprecise interpretations are possible. My interpretation is, assuming that next words after p in memory are zeroed the you have effectively a slice that points to a backing array at *p, and a length and capacity of zero.

If the words following p were not see, you'd have some fun memory corruption bugs to chase down.

T L

unread,
Jul 9, 2017, 5:00:33 AM7/9/17
to golang-nuts

The current internal slice structure used in the official Go compiler is like:

// slice
struct {
	elements unsafe.Pointer
	len      int // number of elements
	cap      int // capacity
}

If happens that the len and cap fields are both 0 for your unsafe slice.
 

Santhosh Ram Manohar

unread,
Jul 9, 2017, 12:46:35 PM7/9/17
to Dave Cheney, golang-nuts
On Sun, Jul 9, 2017 at 12:30 AM, Dave Cheney <da...@cheney.net> wrote:
As your using the unsafe package your doing things which are not considered safe by the lanaguge so only imprecise interpretations are possible. My interpretation is, assuming that next words after p in memory are zeroed the you have effectively a slice that points to a backing array at *p, and a length and capacity of zero.

ah, that makes sense. Thanks, Dave.
 

If the words following p were not see, you'd have some fun memory corruption bugs to chase down.

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/tTmhJZTz3no/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages