Address of slice

513 views
Skip to first unread message

Anmol Sethi

unread,
Mar 23, 2015, 1:26:36 AM3/23/15
to golan...@googlegroups.com
I have this piece of code 

package main


import (
   
"fmt"
)


func
Extend(slice []int, element int) []int {
    n
:= len(slice)
   
if n == cap(slice) {
       
// Slice is full; must grow.
       
// We double its size and add 1, so if the size is zero we still grow.
        newSlice
:= make([]int, len(slice), 2*len(slice)+1)
        copy
(newSlice, slice)
        slice
= newSlice
   
}
    slice
= slice[0 : n+1]
    slice
[n] = element
   
return slice
}


func main
() {
    slice
:= make([]int, 0, 5)
   
for i := 0; i < 10; i++ {
        slice
= Extend(slice, i)
        fmt
.Printf("len=%d cap=%d slice=%v\n", len(slice), cap(slice), slice)
        fmt
.Println("address of 0th element:", &slice[0])
        fmt
.Println("address of slice:", &slice) // why does this print the slice and not its address?
        fmt
.Printf("address of slice: %p\n", &slice) // why is this different from below? and why does it not change when a new slice is created pointing to a different array?
        fmt
.Printf("address of slice: %p\n\n", slice)
   
}
}

    

My question on the second Println at the bottom in the loop. If you run it, you will see it prints out &[values...]. Why does it not print out the address? I know you can do it with Printf, among other ways, and it works, but what about Println? Println with &slice[0] works fine, it prints the address not the value, but Println with &slice is just like nope. 

I also just noticed that when I do the Printf statement %p with &slice, vs then I do only slice, I get different addresses. Why? And the address with &slice does not change when it is changed (run it, the program resizes the array and creates a new slice). But printf(%p, slice) does change?

Tamás Gulácsi

unread,
Mar 23, 2015, 2:03:46 AM3/23/15
to golan...@googlegroups.com
Println prints a nice readable representation of its arguments, using reflection. So it discovers that the arg is a pointer to a slice, so prints accordingly.

Why on earth do you roll your own slice growing logic? Why not use the built-in append?
And you overwrite the local slice variable, not return it, so it does not change in main.
You should return the new slice, or use a pointer to it.

Anmol Sethi

unread,
Mar 23, 2015, 2:10:18 AM3/23/15
to golan...@googlegroups.com
Just figuring out how stuff works behind the scenes thats all.  I do return the slice variable in main? It does change..? The address does not but when I do the second printf it changes when the array changes. Just wondering why.

Jesse McNelis

unread,
Mar 23, 2015, 2:18:30 AM3/23/15
to Anmol Sethi, golang-nuts
On Mon, Mar 23, 2015 at 5:10 PM, Anmol Sethi <anmol....@gmail.com> wrote:
>
> Just figuring out how stuff works behind the scenes thats all. I do return
> the slice variable in main? It does change..? The address does not but when
> I do the second printf it changes when the array changes. Just wondering
> why.

A 'slice' is a value that contain a reference to an array.
If you create a new slice and assign it to the same variable as a
previous slice then it will have the same address of the variable is
the same variable.
You're printing the address of the slice variable not the address the
array the slice value refers to.

Anmol Sethi

unread,
Mar 23, 2015, 2:21:51 AM3/23/15
to golan...@googlegroups.com, anmol....@gmail.com, jes...@jessta.id.au
Ok but what about that second printf. What does that do?

Jesse McNelis

unread,
Mar 23, 2015, 2:31:06 AM3/23/15
to Anmol Sethi, golang-nuts
On Mon, Mar 23, 2015 at 5:21 PM, Anmol Sethi <anmol....@gmail.com> wrote:
> Ok but what about that second printf. What does that do?
>

&slice[0] is the address of the first item in the array that the slice
is referring to.


This might help you understand the memory layout of slices better:
http://research.swtch.com/godata

Anmol Sethi

unread,
Mar 23, 2015, 2:36:33 AM3/23/15
to golan...@googlegroups.com, anmol....@gmail.com, jes...@jessta.id.au
fmt.Printf("address of slice: %p\n\n", slice)

that one

Anmol Sethi

unread,
Mar 23, 2015, 2:37:14 AM3/23/15
to golan...@googlegroups.com, anmol....@gmail.com, jes...@jessta.id.au
fmt.Printf("address of slice: %p\n\n", slice)

this is the statement I am referring to. What does this do?

Jesse McNelis

unread,
Mar 23, 2015, 3:07:32 AM3/23/15
to Anmol Sethi, golang-nuts
On Mon, Mar 23, 2015 at 5:37 PM, Anmol Sethi <anmol....@gmail.com> wrote:
> fmt.Printf("address of slice: %p\n\n", slice)
>
> this is the statement I am referring to. What does this do?

You're trying to print the slice as a pointer so it prints the only
pointer available which is the address of the array that the slice
refers to which is the same as the address of the first element of
that array ie. &slice[0]

Anmol Sethi

unread,
Mar 23, 2015, 7:22:45 AM3/23/15
to golan...@googlegroups.com, anmol....@gmail.com, jes...@jessta.id.au
thanks
Reply all
Reply to author
Forward
0 new messages