# Understanding Pointers and Memory Allocation

- Published: July 27, 2013
- Author: William Kennedy
- URL: https://www.ardanlabs.com/blog/2013/07/understanding-pointers-and-memory.html

In the documentation provided by the Go language team you will find great information on pointers and memory allocation. Here is a link to that documentation:<br />
<br />
<a href="http://golang.org/doc/faq#Pointers"><span class="sr-only">External resource on golang.org: </span>http&#58;//golang.org/doc/faq#Pointers</a><br />
<br />
We need to start with the understanding that all variables contain a value. Based on the type that variable represents will determine how we can use it to manipulate the memory it contains. Read this post to learn more:&nbsp;<a href="https://www.ardanlabs.com/blog/2013/07/understanding-type-in-go.html">Understanding Type In Go</a>

<br />
<br />
In Go we can create variables that contain the &quot;value of&quot; the value itself or an address to the value. When the &quot;value of&quot; the variable is an address, the variable is considered a pointer.<br />
<br />
In the diagram below we have a variable called myVariable. The &quot;value of&quot;&nbsp;myVariable&nbsp;is the address to a value that was allocated of the same type. myVariable&nbsp;is considered a pointer variable.<br />
<br />
<div class="separator" style="margin:0 auto; display:block; width:400px;">
![Screen Shot](../../../images/goinggo/Screen+Shot+2013-07-27+at+2.57.16+PM.png)
</div>
<br />
<div class="separator" style="clear: left; float: left; margin:0 auto; display:block; width:200px;">
![Screen Shot](../../../images/goinggo/Screen+Shot+2013-07-27+at+3.01.52+PM.png)
</div>

<br />
In the next diagram the &quot;value of&quot;&nbsp;myVariable&nbsp;is the value itself, not a reference to the value.<br />
<br />
To access properties of a value we use a selector operator. The selector operator allows us to access a specific field in the value. The syntax is always Value.FieldName, where the period (.) is the selector operator.<br />
<br />
In the C programming language we need to use different selector operators depending on the type of variable we are using. If the &quot;value of&quot; the variable is the value, we use a period (.). If the &quot;value of&quot; the variable is an address, we use an arrow (-&gt;).<br />
<br />
One really nice thing about Go is that you don't need to worry about what type of selector operator to use. In Go we only use the period (.) regardless if the variable is the value or a pointer. The compiler takes care of the underlying details to access the value.<br />
<br />
So why is all of this important? It becomes important when we start using functions to abstract and break up logic. Eventually you need to pass variables to these functions and you need to know what you are passing.<br />
<br />
In Go, variables are passed to a function by value. That means the &quot;value of&quot; each variable that is specified is copied onto the stack for access by that function. In this example we call a function that is supposed to change the state of a value that is allocated in main.<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
package main<br />
<br />
import (<br />
&nbsp; &nbsp; &quot;fmt&quot;<br />
&nbsp; &nbsp; &quot;unsafe&quot;<br />
)<br />
<br />
type MyType struct {<br />
&nbsp; &nbsp; Value1 int<br />
&nbsp; &nbsp; Value2 string<br />
}<br />
<br />
func main() {<br />
&nbsp; &nbsp; // Allocate an value of type MyType<br />
&nbsp; &nbsp; myValue := MyType{10, &quot;Bill&quot;}<br />
<br />
&nbsp; &nbsp; // Create a pointer to the memory for myValue<br />
&nbsp; &nbsp; // &nbsp;For Display Purposes<br />
&nbsp; &nbsp; pointer := unsafe.Pointer(&amp;myValue)<br />
<br />
&nbsp; &nbsp; // Display the address and values<br />
&nbsp; &nbsp; fmt.Printf(&quot;Addr: %v  Value1 : %d  Value2: %s\n&quot;,<br />
&nbsp; &nbsp; &nbsp; &nbsp; pointer,<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;myValue.Value1,<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;myValue.Value2)<br />
<br />
&nbsp; &nbsp; // Change the values of myValue<br />
&nbsp; &nbsp; ChangeMyValue(myValue)<br />
<br />
&nbsp; &nbsp; // Display the address and values<br />
&nbsp; &nbsp; fmt.Printf(&quot;Addr: %v Value1 : %d Value2: %s\n&quot;,<br />
&nbsp; &nbsp; &nbsp; &nbsp; pointer,<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;myValue.Value1,<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;myValue.Value2)<br />
}<br />
<br />
func ChangeMyValue(myValue&nbsp;MyType) {<br />
&nbsp; &nbsp; // Change the values of myValue<br />
&nbsp; &nbsp;&nbsp;myValue.Value1 = 20<br />
&nbsp; &nbsp;&nbsp;myValue.Value2 = &quot;Jill&quot;<br />
<br />
&nbsp; &nbsp; // Create a pointer to the memory for myValue<br />
&nbsp; &nbsp; pointer := unsafe.Pointer(&amp;&nbsp;myValue)<br />
<br />
&nbsp; &nbsp; // Display the address and values<br />
&nbsp; &nbsp; fmt.Printf(&quot;Addr: %v  Value1 : %d  Value2: %s\n&quot;,<br />
&nbsp; &nbsp; &nbsp; &nbsp; pointer,<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;myValue.Value1,<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;myValue.Value2)<br />
}</div>
<br />
Here is the output of the program:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
Addr: 0x2101bc000  Value1 : 10  Value2: Bill<br />
<b>Addr:&nbsp;0x2101bc040  Value1 : 20  Value2: Jill</b><br />
Addr: 0x2101bc000  Value1 : 10  Value2: Bill</div>
<br />
So what went wrong? The changes the function made to main's myValue did not stay changed after the function call. The &quot;value of&quot; the&nbsp;myValue&nbsp;variable in main does not contain a reference to the value, it is not a&nbsp;pointer. The &quot;value of&quot; the&nbsp;myValue&nbsp;variable in main is the value. When we pass the &quot;value of&quot; the&nbsp;myValue&nbsp;variable in main to the function, a copy of the value is placed on the stack. The function is altering&nbsp;its own version of the value. Once the function terminates, the stack is popped, and the copy is technically gone. The &quot;value of&quot; the myValue&nbsp;variable in main is never touched.<br />
<br />
To fix this we can allocate the memory in a way to get a reference back. Then the &quot;value of&quot; the myValue variable in main will be the address to the new&nbsp;value, a pointer variable. Then we can change the&nbsp;function to accept the &quot;value of&quot; an address to the&nbsp;value.<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
package main<br />
<br />
import (<br />
&nbsp; &nbsp; &quot;fmt&quot;<br />
&nbsp; &nbsp; &quot;unsafe&quot;<br />
)<br />
<br />
type MyType struct {<br />
&nbsp; &nbsp; Value1 int<br />
&nbsp; &nbsp; Value2 string<br />
}<br />
<br />
func main() {<br />
&nbsp; &nbsp; // Allocate an value of type MyType<br />
&nbsp; &nbsp;&nbsp;myValue&nbsp;:= <b>&amp;MyType</b>{10, &quot;Bill&quot;}<br />
<br />
&nbsp; &nbsp; // Create a pointer to the memory for myValue<br />
&nbsp; &nbsp; // &nbsp;For Display Purposes<br />
&nbsp; &nbsp; pointer := unsafe.Pointer(<b>myValue</b>)<br />
<br />
&nbsp; &nbsp; // Display the address and values<br />
&nbsp; &nbsp; fmt.Printf(&quot;Addr: %v  Value1 : %d  Value2: %s\n&quot;,<br />
&nbsp; &nbsp; &nbsp; &nbsp; pointer,<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;myValue.Value1,<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;myValue.Value2)<br />
<br />
&nbsp; &nbsp; // Change the values of myValue<br />
&nbsp; &nbsp; ChangeMyValue(myValue)<br />
<br />
&nbsp; &nbsp; // Display the address and values<br />
&nbsp; &nbsp; fmt.Printf(&quot;Addr: %v Value1 : %d Value2: %s\n&quot;,<br />
&nbsp; &nbsp; &nbsp; &nbsp; pointer,<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;myValue.Value1,<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;myValue.Value2)<br />
}<br />
<br />
func ChangeMyValue(myValue&nbsp;<b>&#42;MyType</b>) {<br />
&nbsp; &nbsp; // Change the values of myValue<br />
&nbsp; &nbsp;&nbsp;myValue.Value1 = 20<br />
&nbsp; &nbsp;&nbsp;myValue.Value2 = &quot;Jill&quot;<br />
<br />
&nbsp; &nbsp; // Create a pointer to the memory for myValue<br />
&nbsp; &nbsp; pointer := unsafe.Pointer(<b>myValue</b>)<br />
<br />
&nbsp; &nbsp; // Display the address and values<br />
&nbsp; &nbsp; fmt.Printf(&quot;Addr: %v  Value1 : %d  Value2: %s\n&quot;,<br />
&nbsp; &nbsp; &nbsp; &nbsp; pointer,<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;myValue.Value1,<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;myValue.Value2)<br />
}</div>
<br />
When we use the ampersand (&amp;) operator to allocate the value, a reference is returned. That means the &quot;value of&quot; the myValue variable in main is now a pointer variable, whose value is the address to the newly allocated&nbsp;value. When we pass the &quot;value of&quot; the myValue variable in main to the function, the&nbsp;functions myValue variable now contains the address to the&nbsp;value, not a copy. We now have two pointers&nbsp;pointing to the same&nbsp;value. The myValue variable in main and the&nbsp;myValue&nbsp;variable in the&nbsp;function.<br />
<br />
If we run the program again, the function is now working the way we wanted it to. It changes the state of the value allocated in main.<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
Addr: 0x2101bc000  Value1 : 10  Value2: Bill<br />
<b>Addr: 0x2101bc000 Value1 : 20  Value2: Jill</b><br />
Addr: 0x2101bc000 Value1 : 20  Value2: Jill</div>
<br />
During the function call the&nbsp;value&nbsp;is no longer being copied on the stack, the address to the&nbsp;value&nbsp;is&nbsp;being&nbsp;copied. The function is now referencing the same&nbsp;value, via the local pointer variable, and&nbsp;changing the values.<br />
<br />
The Go document titled &quot;Effective Go&quot; has a great section about memory allocations, which includes how arrays, slices and maps work:<br />
<br />
<a href="http://golang.org/doc/effective_go.html#allocation_new"><span class="sr-only">External resource on golang.org: </span>http&#58;//golang.org/doc/effective_go.html#allocation_new</a><br />
<br />
Let's talk about the keywords new and make.<br />
<br />
The new keyword is used to allocate&nbsp;values&nbsp;of a specified type in memory. The memory allocation is zeroed&nbsp;out. The memory can't be further initialized on the call to new. In other words, you can't specify specific values for properties of the specified type when using new.<br />
<br />
If you want to specify values at the time you allocate the&nbsp;value, use a composite literal. They come in two&nbsp;flavors, with or without specifying the field names.<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
&nbsp; &nbsp; // Allocate an value of type MyType<br />
&nbsp; &nbsp; // Values must be in the correct order<br />
&nbsp; &nbsp; myValue := MyType{10, &quot;Bill&quot;}<br />
<br />
&nbsp; <br />
&nbsp; &nbsp; // Allocate a value of type MyType<br />
&nbsp; &nbsp; // Use labeling to specify the values<br />
&nbsp; &nbsp;&nbsp;myValue := MyType{<br />
&nbsp; &nbsp; &nbsp; &nbsp; Value1: 10,<br />
&nbsp; &nbsp; &nbsp; &nbsp; Value2: &quot;Bill&quot;,<br />
&nbsp; &nbsp; }</div>
<br />
The make keyword is used to allocate and initialize slices, maps and channels only. Make does not return a reference, it returns the &quot;value of&quot; a data structure that is created and initialized to manipulate the new slice, map or channel. This data structure contains references to other data structures that are used to manipulate the slice, map or channel.<br />
<br />
How does passing a map by value to a function work. Look at this sample code:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
package main<br />
<br />
import (<br />
&nbsp; &nbsp; &quot;fmt&quot;<br />
&nbsp; &nbsp; &quot;unsafe&quot;<br />
)<br />
<br />
type MyType struct {<br />
&nbsp; &nbsp; Value1 int<br />
&nbsp; &nbsp; Value2 string<br />
}<br />
<br />
func main() {<br />
&nbsp; &nbsp; myMap := make(map[string]string)<br />
&nbsp; &nbsp; myMap[&quot;Bill&quot;] = &quot;Jill&quot;<br />
<br />
&nbsp; &nbsp; pointer := unsafe.Pointer(&amp;myMap)<br />
&nbsp; &nbsp; fmt.Printf(&quot;Addr: %v  Value : %s\n&quot;, pointer, myMap[&quot;Bill&quot;])<br />
<br />
&nbsp; &nbsp; ChangeMyMap(myMap)<br />
&nbsp; &nbsp; fmt.Printf(&quot;Addr: %v  Value : %s\n&quot;, pointer, myMap[&quot;Bill&quot;])<br />
<br />
&nbsp; &nbsp; ChangeMyMapAddr(&amp;myMap)<br />
&nbsp; &nbsp; fmt.Printf(&quot;Addr: %v  Value : %s\n&quot;, pointer, myMap[&quot;Bill&quot;])<br />
}<br />
<br />
func ChangeMyMap(myMap map[string]string) {<br />
&nbsp; &nbsp; myMap[&quot;Bill&quot;] = &quot;Joan&quot;<br />
<br />
&nbsp; &nbsp; pointer := unsafe.Pointer(&amp;myMap)<br />
<br />
&nbsp; &nbsp; fmt.Printf(&quot;Addr: %v  Value : %s\n&quot;, pointer, myMap[&quot;Bill&quot;])<br />
}<br />
<br />
// Don't Do This, Just For Use In This Article<br />
func ChangeMyMapAddr(myMapPointer &#42;map[string]string) {<br />
&nbsp; &nbsp; (&#42;myMapPointer)[&quot;Bill&quot;] = &quot;Jenny&quot;<br />
<br />
&nbsp; &nbsp; pointer := unsafe.Pointer(myMapPointer)<br />
<br />
&nbsp; &nbsp; fmt.Printf(&quot;Addr: %v  Value : %s\n&quot;, pointer, (&#42;myMapPointer)[&quot;Bill&quot;])<br />
}
</div>
<br />
Here is the output for the program:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
Addr: 0x21015b018  Value : Jill<br />
<b>Addr: 0x21015b028  Value : Joan</b><br />
Addr: 0x21015b018  Value : Joan<br />
Addr: 0x21015b018  Value : Jenny<br />
Addr: 0x21015b018  Value : Jenny
</div>
<br />
We make a map and add a single key called &quot;Bill&quot; assigning the value of &quot;Jill&quot;. Then we pass the value of the map variable to the ChangeMyMap function. Remember the myMap variable is not a pointer so the &quot;value of&quot; myMap, which is a data structure, is copied onto the stack during the function call. Because the &quot;value of&quot; myMap is a data structure that contains references to the internals of the map, the function can use its copy of the data structure to make changes to the map that will be seen by main after the function call.<br />
<br />
If you look at the output you can see that when we pass the map by value, the function has its own copy of the map data structure. You can see changes made to the map are reflected after the function call. In main we display the value of the map key &quot;Bill&quot; after the function call and it has changed.<br />
<br />
It is unnecessary but the ChangeMyMapAddr function shows how you could pass and use a reference to the myMap variable in main. Again the Go team has made sure passing the &quot;value of&quot; a map variable can be performed without problems. Notice how we need to dereference the myMapPointer variable when we want to access the map. This is because the Go compiler will not allow us to access the map through a pointer variable directly. Dereferencing a pointer variable is equivalent to having a variable whose value is the value.<br />
<br />
I have taken the time to write this post because sometimes&nbsp;it can&nbsp;be confusing as to what the &quot;value of&quot; your variable contains. If the &quot;value of&quot; your variable is a large&nbsp;value, and you pass the &quot;value of&quot; that&nbsp;variable to a function, you will be making a large copy of that variable on the stack. You want to make sure you're passing addresses to your functions unless you have a very special use case.<br />
<br />
Maps, slices and channels are different. You can pass these variables by value without any concern. When we pass a map variable to a function, we are copying a data structure not the entire map.<br />
<br />
I recommend that you read the <a href="http://golang.org/doc/effective_go.html">Effective Go</a> documentation. I have read that document several times since I started programming in Go. As I gain more experience I always go back and read this document again. I always pick up on something new that didn't make sense to me before.<br />
<br />
<br />
