Relationship of a to b...https://play.golang.org/p/jihZRiYy8bO


(Cherolyn Lexvold) #1

https://play.golang.org/p/jihZRiYy8bO

In this code, (and others) I am trying to understand the relationship of “a” to “b”


(Cherolyn Lexvold) #2

The relationship of the following question to the above is that both of these codes are included under a topic in one of Todd McLeod’s lessons about pointers.
https://play.golang.org/p/RvznxHr_tda
What is this? “0x20818a220”


(Yamil Bracho) #3

That is the contents of the pointer. It means the memory address where it points…


(Jay Ts) #4

Hi Cherolyn,

Here’s one of the simplest and most correct ways I know to explain pointers.

Memory in modern computers is a huge bunch of simple electronic circuits, each of which can hold either a 1 or a 0 (as a high voltage or low voltage). Those bits are organized in groups of 8, called bytes. To store or retrieve a specific byte, you give the computer the address, which is just a number. For example, if your computer has 1 GB (gigabyte, or a billion bytes) of memory, the addresses range from 0 to (1 billion minus 1). So let’s look at a very small section of that. The following table shows memory from address 1001 to address 1007:

variable     address      memory
----------------------------------
x            1001         00000002
a            1002         00000043
             1003         00000009
b            1004         00001002
             1005         00000000
c            1006         00000043
d            1007         00001004

First, look at just the 2nd and 3rd columns. You will see the address of the byte in memory, and in the 3rd column is the number stored at that memory location … except in this case, I’m taking “artistic liberty” to allow the memory location to hold a much larger number than a byte can hold, to make things look simpler, so (hopefully) it will be easier to understand.

First, let’s look at memory at address 1001. I named it x. In the computer’s memory system, if you request what is stored at location (or address) 1001, it will give you 2. That is the data stored at that address.

To put it into human terms, the value of the variable called x is 2. If we tell the computer to store a 3 at address 1001, then the 2 there will be replaced with a 3. We can write it in a programming language as

x = 3

Now look at the memory location (address) 1002. It is called a, and holds the value 43. That is what the line

a := 43

does in your program. In Go, you can get the address of that data with the & operator, so

b := &a

would set b to 1001 in this example. In a real computer, addresses are usually huge numbers (because you have gigabytes of memory), and are often written in hexidecimal (base 16) notation, which can be clearer for programmers. When you printed the address (which is also called a pointer) in your program, you got a hexidecimal number. You can tell it’s hexidecimal because the number starts with 0x (that is a convention used in Go and many other programming languages).

Look at memory location 1004 in the table. That is where the value of b is stored. It also has an address, so you can even take the pointer of that. You can see that address stored at memory location 1007, which I have called d. (This can go on nearly forever).

Since b holds the address of a, you can retrieve the value of a this way:

c := *b

This is saying, “use b as a pointer (an address). Get the value of b and then use that as the address of the data to retrieve. Then set c to that value”.

In other words,

a = 43     // set a to 43
b = &a     // set b to the address of a
c = *b     // use the value of b as an address and set c to the value at that address
fmt.Println(c)  //  prints "43"

I hope that helps clarify pointers for you.


(Yamil Bracho) #5

Excellent explanation!!!
Thanks a lot!!!


(Cherolyn Lexvold) #6

I agree with Yamil. That is a wonderful, detailed explanation, as all your help has been to me. Yhanks so much. I will save it to a file so I can keep it “forever” :wink:
Interesting what you said about adresses as distinguished from memory. I’m going to have to go over this information more than once, and I definitely think it is worthy of that. Thanks again, and it is very nice to hear from you again.


(Cherolyn Lexvold) #7

“Look at memory location 1004 in the table. That is where the value of b is stored. It also has an address, so you can even take the pointer of that. You can see that address stored at memory location 1007 , which I have called d . (This can go on nearly forever).”
(I don’t remember the correct way to display a quote here on the forum)
I notice here that you describe both 1004 and 1007 as memory locations. I’m not clear on the distinction.

Todd McLeod follows up this lesson with another called “When to use Pointers.” He stars with: * step 1 no pointer: https://play.golang.org/p/lxsWkhTaYv
It seems like in this code, the computer “executes” (for the lack of a better word, or is this the correct word?) first line 14, then 16, and then 10. Am I understanding this correcty? and if so, I wonder why the computer does not “execute” the lines in order.


(Jay Ts) #8

Hi Cherolyn,

By “memory location 1004”, I just meant to write “the data (or memory) at address 1004” in another way. If I ever get around to writing this up formally, I will need to decide on exactly what words to use, and explain them carefully.

About the sample code, notice that after execution begins in main(), the function foo() is called. Execution transfers to thefoo() function, where the fmt.Println()'s on line 14 and 16 are executed in order. At the end of foo(), execution returns to line 10 in main(), where the fmt.Println() there executes.

That’s how functions work. In other languages, they are called subroutines or procedures, and way down in machine code, CPU instruction sets have a “jump to subroutine”, instruction that makes it happen.

In that example, it looks like Todd is showing that in Go, function arguments are passed by value. That means that foo() is working on a copy of the variable passed to it, and can modify only it’s own copy, and not the variable x that is in main().

If you want foo() to modify x, then you can pass a pointer to x to a function, and then have that function operate on the pointer. I modified the program to show how:

https://play.golang.org/p/XOScAjYxgp7


(Cherolyn Lexvold) #9

Got it! Thanks! Saving this

Why is this so exciting to me!