<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abhinav Singh</title>
    <description>The latest articles on DEV Community by Abhinav Singh (@__init__abs).</description>
    <link>https://dev.to/__init__abs</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1310197%2F4cfb9061-2d20-4a06-aad9-f6ff023afd2e.jpg</url>
      <title>DEV Community: Abhinav Singh</title>
      <link>https://dev.to/__init__abs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/__init__abs"/>
    <language>en</language>
    <item>
      <title>An ode to Stacks and Pointers in Go!</title>
      <dc:creator>Abhinav Singh</dc:creator>
      <pubDate>Sat, 11 May 2024 13:26:29 +0000</pubDate>
      <link>https://dev.to/__init__abs/an-ode-to-stacks-and-pointers-in-go-525n</link>
      <guid>https://dev.to/__init__abs/an-ode-to-stacks-and-pointers-in-go-525n</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;Let's be honest: pointers can be a tough nut to crack. Misusing them can lead to frustrating bugs and slow performance, especially in concurrent or multi-threaded programs. That's why many languages shield developers from dealing with pointers altogether. However, if you're coding in Go, you can't escape them. Mastering pointer is essential for crafting elegant, efficient code in Go.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function Frames
&lt;/h3&gt;

&lt;p&gt;In Go programming, functions execute within a defined function frame, each maintaining their distinct memory space. These frames allow the functions to operate autonomously within their designated context, facilitating smooth flow control. While a function can readily access memory within its frame, venturing beyond its frame necessitates indirect access. To tap into the memory within a different function frame, the memory in question must be shared with the function via frame pointer. Grasping the intricacies and constraints imposed by these function frames is paramount.&lt;/p&gt;

&lt;p&gt;When a function is invoked within another function, there is a transaction that occurs between the calling and the called function frame. If the function call requires data, then the data must be passed to the other frame in "pass by value" fashion.&lt;/p&gt;

&lt;p&gt;Pass by value (also known as pass-by-copy) is the argument passing technique utilized in the Go programming language. This technique permits various forms of arguments in the call, such as constants, variables, and complex expressions, while also ensuring the immutability of arguments. It achieves this by guaranteeing that the function receives a copy of the data. Consequently, the function can modify the data without impacting the original data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;
&lt;span class="m"&gt;1.&lt;/span&gt; &lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;
&lt;span class="m"&gt;2.&lt;/span&gt; 
&lt;span class="m"&gt;3.&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="s"&gt;"fmt"&lt;/span&gt;
&lt;span class="m"&gt;4.&lt;/span&gt; 
&lt;span class="m"&gt;5.&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="m"&gt;6.&lt;/span&gt;  &lt;span class="c"&gt;// Increment the value of val&lt;/span&gt;
&lt;span class="m"&gt;7.&lt;/span&gt;  &lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
&lt;span class="m"&gt;8.&lt;/span&gt; 
&lt;span class="m"&gt;9.&lt;/span&gt;  &lt;span class="c"&gt;// Printing the value of the val variable&lt;/span&gt;
&lt;span class="m"&gt;10.&lt;/span&gt;     &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Val value:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="m"&gt;11.&lt;/span&gt; 
&lt;span class="m"&gt;12.&lt;/span&gt;     &lt;span class="c"&gt;// Printing the address of the val variable&lt;/span&gt;
&lt;span class="m"&gt;13.&lt;/span&gt;     &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Val Address:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="m"&gt;14.&lt;/span&gt; 
&lt;span class="m"&gt;15.&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="m"&gt;16.&lt;/span&gt; 
&lt;span class="m"&gt;17.&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="m"&gt;18.&lt;/span&gt;     &lt;span class="c"&gt;// Declaring variable of type int&lt;/span&gt;
&lt;span class="m"&gt;19.&lt;/span&gt;     &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="m"&gt;20.&lt;/span&gt; 
&lt;span class="m"&gt;21.&lt;/span&gt;     &lt;span class="c"&gt;// Setting the value of the counter variable to 1&lt;/span&gt;
&lt;span class="m"&gt;22.&lt;/span&gt;     &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;
&lt;span class="m"&gt;23.&lt;/span&gt; 
&lt;span class="m"&gt;24.&lt;/span&gt;     &lt;span class="c"&gt;// Printing the value of the counter variable&lt;/span&gt;
&lt;span class="m"&gt;25.&lt;/span&gt;     &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Counter value:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="m"&gt;26.&lt;/span&gt; 
&lt;span class="m"&gt;27.&lt;/span&gt;     &lt;span class="c"&gt;// Printing the address of the counter variable&lt;/span&gt;
&lt;span class="m"&gt;28.&lt;/span&gt;     &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Counter Address:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="m"&gt;29.&lt;/span&gt; 
&lt;span class="m"&gt;30.&lt;/span&gt;     &lt;span class="c"&gt;// Pass counter as an argument to the increment function&lt;/span&gt;
&lt;span class="m"&gt;31.&lt;/span&gt;     &lt;span class="n"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="m"&gt;32.&lt;/span&gt; 
&lt;span class="m"&gt;33.&lt;/span&gt;     &lt;span class="c"&gt;// Printing the value of the counter variable&lt;/span&gt;
&lt;span class="m"&gt;34.&lt;/span&gt;     &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Counter value after increment:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="m"&gt;35.&lt;/span&gt; 
&lt;span class="m"&gt;36.&lt;/span&gt;     &lt;span class="c"&gt;// Printing the address of the counter variable&lt;/span&gt;
&lt;span class="m"&gt;37.&lt;/span&gt;     &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Counter Address after increment:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="m"&gt;38.&lt;/span&gt; 
&lt;span class="m"&gt;39.&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When executing a Go program, the runtime initiates the main goroutine to commence executing all code, including that within the &lt;strong&gt;main&lt;/strong&gt; function. A goroutine represents a path of execution assigned to an operating system thread for execution on one of the cores. Each goroutine is assigned an initial ~2KB block of contiguous memory, forming its stack space.&lt;/p&gt;

&lt;p&gt;The stack serves as the physical memory location for each function frame. The image below illustrates the physical memory allocated to the main function frame on the stack.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fef4fq09yfcplzkc5a6pe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fef4fq09yfcplzkc5a6pe.png" width="800" height="384"&gt;&lt;/a&gt;&lt;br&gt;Figure 1 - Main Function Frame
  &lt;/p&gt;

&lt;p&gt;In Figure 1, a segment of the stack is delineated for the &lt;strong&gt;main&lt;/strong&gt; function, forming what is known as a "Function Stack Frame". This frame serves to demarcate the main function's boundary within the stack and is created during the execution of the function call. Additionally, within the main frame, memory for the &lt;strong&gt;counter&lt;/strong&gt; variable is located at address &lt;strong&gt;0xc000012028&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function Calls
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;main&lt;/strong&gt; function calls the increment function as shown on line 31. A new function call means that the goroutine needs to create a new stack frame for &lt;strong&gt;increment&lt;/strong&gt; function(called function). To successfully execute this function call, data needs to be passed across the stack frame and placed into the newly created stack frame as specified in the declaration of the increment function on line 5. In this specific case, an integer value(counter) is expected to be copied and passed during the call.&lt;/p&gt;

&lt;p&gt;As the increment function can only read and write to memory locations within its own frame, it needed a variable &lt;strong&gt;val&lt;/strong&gt; of type int to store and access its own copy of the counter value being passed. The passed value of &lt;strong&gt;counter&lt;/strong&gt; is copied and passed into the newly created &lt;strong&gt;val&lt;/strong&gt; variable inside the increment function frame.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F94gjxdnjme8of05nz6uf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F94gjxdnjme8of05nz6uf.png" width="800" height="479"&gt;&lt;/a&gt;&lt;br&gt;Figure 2 -  Main &amp;amp; Increment Frames
  &lt;/p&gt;

&lt;p&gt;You'll notice that the stack now comprises two frames: one for the main function and, beneath it, one for the increment function. Within the increment frame, the variable &lt;strong&gt;val&lt;/strong&gt; holds the copied value of 1 passed during the function call. Positioned at address &lt;strong&gt;0xc0000a2018&lt;/strong&gt;, the val variable resides lower in memory, reflecting the sequential arrangement of frames along the stack. This ordering is merely an implementation detail devoid of significance. Crucially, the goroutine extracted the &lt;strong&gt;counter&lt;/strong&gt; value from the main frame and duplicated it within the increment frame using the &lt;strong&gt;val&lt;/strong&gt; variable.&lt;/p&gt;

&lt;p&gt;The control executes all the lines under the increment function and the output on the terminal should look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Val value: 2
Val Address: 0xc0000a2018
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Function Returns
&lt;/h3&gt;

&lt;p&gt;After executing all the lines under the &lt;strong&gt;increment&lt;/strong&gt; function, the control returns to the &lt;strong&gt;main&lt;/strong&gt; function with a small change to the stack frames.&lt;/p&gt;

&lt;p&gt;The stack frame associated with the &lt;strong&gt;increment&lt;/strong&gt; function is now part of the garbage memory because the control has shifted to &lt;strong&gt;main&lt;/strong&gt; function thereby making the main function frame the active frame. The memory that was framed for the increment function is left untouched.&lt;/p&gt;

&lt;p&gt;It's pointless to tidy up the memory of the returning function's frame because there's no way to know if that memory will be needed again. Hence, the memory remains unchanged. The stack memory for each frame is actually wiped clean during every function call. This cleaning process happens when values are initialized within the frame. Since all values are set to their default "zero value"(0 is the default value for the int type) during initialization, the stacks naturally tidy themselves up with each function call.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1vyo6avhgd8t0zrcfzik.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1vyo6avhgd8t0zrcfzik.png" width="800" height="478"&gt;&lt;/a&gt;&lt;br&gt;Figure 3 -  Frames after increment func returns
  &lt;/p&gt;

&lt;p&gt;The final output of the above code block on the terminal should look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Counter value: 1
Counter Address: 0xc000012028
Val value: 2
Val Address: 0xc0000a2018
Counter value after increment: 1
Counter Address after increment: 0xc0000a2010
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Sharing Values
&lt;/h3&gt;

&lt;p&gt;In case if it was important for the increment function to operate directly on the counter variable that exists inside the main function's stack frame, pointers prove invaluable. They facilitate value sharing, enabling functions to operate on variables located outside their own stack frame.&lt;/p&gt;

&lt;h3&gt;
  
  
  Indirect Memory Access
&lt;/h3&gt;

&lt;p&gt;The code block below performs a function call passing an address "by value". The value of the counter variable from the main stack frame is shared with the increment function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;
&lt;span class="m"&gt;1.&lt;/span&gt; &lt;span class="k"&gt;package&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;
&lt;span class="m"&gt;2.&lt;/span&gt; 
&lt;span class="m"&gt;3.&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
&lt;span class="m"&gt;4.&lt;/span&gt;  &lt;span class="s"&gt;"fmt"&lt;/span&gt;
&lt;span class="m"&gt;5.&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="m"&gt;6.&lt;/span&gt; 
&lt;span class="m"&gt;7.&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="m"&gt;8.&lt;/span&gt;  &lt;span class="c"&gt;// Increment the value of val&lt;/span&gt;
&lt;span class="m"&gt;9.&lt;/span&gt;  &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
&lt;span class="m"&gt;10.&lt;/span&gt; 
&lt;span class="m"&gt;11.&lt;/span&gt;     &lt;span class="c"&gt;// Printing the value of the val variable&lt;/span&gt;
&lt;span class="m"&gt;12.&lt;/span&gt;     &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Val value:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="m"&gt;13.&lt;/span&gt; 
&lt;span class="m"&gt;14.&lt;/span&gt;     &lt;span class="c"&gt;// Printing the address of the val variable&lt;/span&gt;
&lt;span class="m"&gt;15.&lt;/span&gt;     &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Val Address:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="m"&gt;16.&lt;/span&gt; 
&lt;span class="m"&gt;17.&lt;/span&gt;     &lt;span class="c"&gt;// Printing the value the val pointer points to&lt;/span&gt;
&lt;span class="m"&gt;18.&lt;/span&gt;     &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Val Points To:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="m"&gt;19.&lt;/span&gt; 
&lt;span class="m"&gt;20.&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="m"&gt;21.&lt;/span&gt; 
&lt;span class="m"&gt;22.&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="m"&gt;23.&lt;/span&gt;     &lt;span class="c"&gt;// Declaring variable of type int&lt;/span&gt;
&lt;span class="m"&gt;24.&lt;/span&gt;     &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="m"&gt;25.&lt;/span&gt; 
&lt;span class="m"&gt;26.&lt;/span&gt;     &lt;span class="c"&gt;// Setting the value of the counter variable to 1&lt;/span&gt;
&lt;span class="m"&gt;27.&lt;/span&gt;     &lt;span class="n"&gt;counter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;
&lt;span class="m"&gt;28.&lt;/span&gt; 
&lt;span class="m"&gt;29.&lt;/span&gt;     &lt;span class="c"&gt;// Printing the value of the counter variable&lt;/span&gt;
&lt;span class="m"&gt;30.&lt;/span&gt;     &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Counter value:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="m"&gt;31.&lt;/span&gt; 
&lt;span class="m"&gt;32.&lt;/span&gt;     &lt;span class="c"&gt;// Printing the address of the counter variable&lt;/span&gt;
&lt;span class="m"&gt;33.&lt;/span&gt;     &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Counter Address:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="m"&gt;34.&lt;/span&gt; 
&lt;span class="m"&gt;35.&lt;/span&gt;     &lt;span class="c"&gt;// Pass counter as an argument to the increment function&lt;/span&gt;
&lt;span class="m"&gt;36.&lt;/span&gt;     &lt;span class="n"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="m"&gt;37.&lt;/span&gt; 
&lt;span class="m"&gt;38.&lt;/span&gt;     &lt;span class="c"&gt;// Printing the value of the counter variable&lt;/span&gt;
&lt;span class="m"&gt;39.&lt;/span&gt;     &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Counter value after increment:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="m"&gt;40.&lt;/span&gt; 
&lt;span class="m"&gt;41.&lt;/span&gt;     &lt;span class="c"&gt;// Printing the address of the counter variable&lt;/span&gt;
&lt;span class="m"&gt;42.&lt;/span&gt;     &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Counter Address after increment:"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="m"&gt;43.&lt;/span&gt; 
&lt;span class="m"&gt;44.&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The three interesting changes that were made to facilitate indirect memory access are as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;On &lt;strong&gt;line 36&lt;/strong&gt;, the code is not copying and passing the "value of" counter but instead the "address of" counter. "&amp;amp;" operator extracts the address of the counter variable in the main function frame. As Go is a pass by value language, so we are still passing a value but just in form of an address.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;On &lt;strong&gt;line 7&lt;/strong&gt;, &lt;strong&gt;*int&lt;/strong&gt; highlights that the increment function expects a address of the variable(pointer) rather than the variable itself. Since the variable &lt;strong&gt;counter&lt;/strong&gt; is of type &lt;strong&gt;int&lt;/strong&gt;, it's pointer type is &lt;strong&gt;*int&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fik37ntzvhtk6puntk8gw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fik37ntzvhtk6puntk8gw.png" width="800" height="464"&gt;&lt;/a&gt;&lt;br&gt;Figure 4 -  Frame Stack after func call to increment
  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;On &lt;strong&gt;line 9&lt;/strong&gt;, the * character in &lt;strong&gt;*val++&lt;/strong&gt; is acting as an operator and extracts the value &lt;strong&gt;that pointer is pointing to&lt;/strong&gt;. The pointer variable allows indirect memory access outside of the function's frame. The process of extracting the the value that pointer is pointing to is called &lt;strong&gt;dereferencing&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frsibac3h78v6levaeqid.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frsibac3h78v6levaeqid.png" width="800" height="464"&gt;&lt;/a&gt;&lt;br&gt;Figure 5 -  Frame Stack after executing line 9
  &lt;/p&gt;

&lt;p&gt;The final output of the above code block on the terminal should look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Counter value: 1
Counter Address: 0xc000012028
Val value: 0xc000012028
Val Address: 0xc00004a028
Val Points To: 2
Counter value after increment: 2
Counter Address after increment: 0xc000012028
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>go</category>
      <category>pointers</category>
      <category>stack</category>
    </item>
  </channel>
</rss>
