Junior Dev Blog

What is a Pointer?

2022-08-15 at Guide category

Definition...

Pointers are special kind of variables responsible for holding memory address of other variables.

They act just like another variable, they have a memory address of their own but what makes them special is that we can derefference their address value to reach the underlying memory value (emphasis on value, not the address).

alt memory diagram

As you can see in the above diagram, Number2 is a int pointer which points to the address of Number1 variable, but note that Number2 has an address of it's own as well.

Also, Int Pointer doesn't mean that pointer contains an int value, but rather the pointer points to a int value.

Alt Text

Confused Enough? Great!! Let's now makes things a bit more clear via code!

Working of a pointer in GO

Consider the following example

package main

import "fmt"

//Function to double the given number
func doubleIt (number int){
    number = number * 2
    return
}


func main(){
    number := 2
    //calling doubleIt function with above `number` as argument
    doubleIt(number)

    //This will print out "number is equals to 2"
    fmt.Printf("number is equals to %d ", number)
    return
}

Output:
number is equal to 2

In the above code, we created a function named doubleIt which takes a number int as an argument and doubles it, sweet!

Soo.... what's the catch?!!

Well... if only things were that simple, you see, when we give number to doubleIt as an argument, it makes a copy of it in the HEAP memory, it doesn't affect the number variable inside the main function.


But let's look at another example now

package main

import "fmt"

// Function to double the given number
func doubleIt(number *int) {
	*number = *number * 2
	return
}

func main() {
	number := 2
	//calling doubleIt function with above `number` as argument
	doubleIt(&number)

	//This will print out "number is equals to 4"
	fmt.Printf("number is equals to %d ", number)
	return
}



Output:
number is equal to 4

Note the changes made to line 6 , 7 and 14 .

On line 6 and 7 instead of passing the value directly, which in turn produces a copy of variable in the heap, what the doubleIt function accepts is an int pointer to which on line 7 we mutate via a technique called Dereferencing .

and finally on line 14 while calling the doubleIt function, we provide it the address of number via the & (address of) operator.


Pointer deferencing

Dereferencing is used to access or manipulate data contained in memory location pointed to by a pointer. *(asterisk) is used with pointer variable when dereferencing the pointer variable, it refers to variable being pointed, so this is called dereferencing of pointers.


Conclusion

Since GO is a garbage collected language, passing addresses to functions instead of values would result in much better performance because of much less copying of data in the heap, hence less work for garbage collector, but it could lead to much undefined behaviour since it would be hard to figure which function is mutating which value.

Zulfiqar Ali

Personal blog by Zulfiqar Ali.

developer tutorial treats