Sobes.tech
Middle

What is a pointer and how much memory does it occupy?

sobes.tech AI

Answer from AI

A pointer in Go is a variable that stores the address of another variable in memory. It allows working directly with data by address, which is useful for optimization and passing large structures without copying.

The size of a pointer depends on the system architecture:

  • On 32-bit systems, a pointer occupies 4 bytes.
  • On 64-bit systems, it occupies 8 bytes.

Example of using a pointer in Go:

package main
import "fmt"

func main() {
    var a int = 42
    var p *int = &a // p is a pointer to a
    fmt.Println("Value of a:", a)
    fmt.Println("Address of a:", p)
    *p = 100 // change value through pointer
    fmt.Println("New value of a:", a)
}
What is a pointer and how much memory does it occupy… - sobes.tech