0

What is happening under the hood (allocation, pointers) such that getting the address of a dereferenced array doesn't give back the same address?

package main

import (
    "fmt"
)

func main() {
    in := [...]int{1, 2, 3, 4, 5}
    orig := *&in
    if &in != &orig {
        fmt.Printf("&in = %p, &orig = %p\n", &in, &orig)
    }

    a := 1
    b := *&a
    if &a != &b {
        fmt.Printf("&a = %p, &b = %p\n", &a, &b)
    }

}

Live example here.

I'm new to Go and I find this surprising; I would have expected the same address. What I suspect is happening is a new array (copy) is being allocated and assigned to orig and b.

yangmillstheory
  • 1,055
  • 13
  • 31

0 Answers0