Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to thoroughly understand Go pointers

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

How to thoroughly understand the Go pointer, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

What is a pointer?

To put it simply, a pointer is a value that points to another address. This is a textbook explanation, but if you transfer from a development language that doesn't have to talk about variable addresses, it looks like a string of cuneiform and is hard to understand.

Let's break it down.

What is memory?

Computer memory, or RAM, can be thought of as a string of boxes, lined up one by one.

Each box (or cell) is marked with a unique number, which is incremented sequentially; this is the address of the cell and its memory location.

Each cell stores a value. If you know the memory address of a cell, you can access the cell and read its contents. Or replace the previous value in the cell with another value.

This is all about memory, and all CPU does is to get and store values into memory units.

What is a variable?

Write a piece of code to read the value stored at memory address 200, multiply it by 3, and store the result at memory address 201. The pseudo-code flow is as follows:

Read the value stored in memory address 200 and temporarily store it in CPU

Multiplies the value stored in CPU by 3

Store the value stored in CPU in a location with memory address 201

This is how early programs were written. The programmer will keep a list of memory locations, including who uses it, when, and what the values stored in it represent.

Obviously, this is tedious and error-prone, which means that every possible value stored in memory must be assigned an address during programming. To make matters worse, this approach makes it extremely difficult to dynamically allocate memory to variables while the program is running-imagine if you have to use global variables to write large programs.

In order to solve this problem, the concept of variable is created. A variable is just a katakana of numeric letters that identifies the location of the storage.

Now, instead of talking about storage locations, we are talking about variables, which are the memorable names we provide for memory locations. The previous program can now be expressed as:

Read the value stored in variable an and put it into CPU

Multiply it by 3.

Save the result in variable b

This is the same program, but with one important improvement-we no longer need to talk about memory locations directly, and we no longer need to track them-leave this heavy work to the compiler.

Now, we can write the program like this:

Var a = 62 var b = a * 3

The compiler will ensure that variables an and b are allocated unique memory locations so that their values can be saved as needed.

What is a pointer?

Now that we know that memory is a series of numbered cells, and variables are just nicknames that identify the location of memory, what is the pointer?

A pointer is a value that points to the memory location of another variable.

The pointer points to the memory address of the variable, just as the memory address of the variable identifies the value.

Let's take a look at this code:

1func main () {2 a: = 200 3b: & a 4 * baked + 5 fmt.Println (a) 6}

The second line declares the variable an and assigns a value of 200.

Next, you declare the variable b and assign the address of the variable a to it. Remember, we don't know exactly where the variable an is stored, but we can still store the address of an in b.

The fourth line of code is the hardest to understand. Variable b stores the address of variable a, but we want to add one to the value of a. To do this, you must use dereferencing to get the value of a through b.

Then add the value to one and store the result in the memory location pointed to by b, that is, the memory location where the variable an is located.

The last line of code prints the value of a, which is also the value 201 after adding one.

If your previous language does not have the concept of pointers or every variable implies pointers, don't panic. It takes time and practice to understand the relationship between variables and pointers. Remember this rule:

A pointer is a value that points to the memory location of another variable.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report