In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "the usage of pointers in Go language". In daily operation, I believe many people have doubts about the usage of pointers in Go language. Xiaobian consulted various materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "the usage of pointers in Go language"! Next, please follow the small series to learn together!
1. What is written before:
Before we talk about Go pointers, we need to talk about what pointers are. Why do we need pointers? (Note: This refers to the original pointer.)
1. What's a pointer?
First of all, a pointer is a variable, the full name is called a pointer variable, but the value stored in this variable is an address. So pointers, even null pointers, have addresses because variables have addresses. Examples are as follows:
Output:
From the output below, we can see that the address of the null pointer a exists. Before a points to b, the value of pointer a is nil. After pointing to b, the value becomes the address of variable b, and if you do the operation *a on a, the value of variable b is 10.
2. Why do we need pointers?
The reason we need pointers is because we want to do indirect addressing, that is, when the program runs, we want to jump to one address segment and execute to another address segment. For details see:
You know, why do I need a pointer?
II. Usage of pointer:
With that said, let's get back to what pointers look like in Go and how they differ from C++.
1. usage rules
1).C++ uses the operator->, Go uses the operator..
2).* The operation of is the same, indicating that the pointer points to the stored data.
In C++, pointers need to be created and deleted, new and delete, to manage and free memory space. Go can be created by making, but does not need to be released by itself.
2. Different storage locations in memory
In C++, memory allocated by pointers is stored in the heap, while Go is stored in the stack.
In C++, if a pointer points to a local variable, if the address of the local variable is destroyed, then the data in the address pointed to by the pointer may be dirty data. In Go, pointers can point to local variables, because variables overflow, even if the variable is out of its scope, it will not be released, because there are pointers to reuse, this part of the operation is guaranteed by Go at the language level.
Examples:
C++:
This example demonstrates that when a variable in the stack is pointed to by a pointer, once the memory location is used again, the memory data pointed to by the pointer becomes dirty data.
Output:
b[1024*128] is to cover the original value of the local variable b, from the output result, the value of *a is not 10, but 32522, which becomes an unexpected value.
Go:
As with the C++ example, we can see through the program that the memory pointed to by the local variable b, once used by the pointer, will not be overwritten if the pointer is still valid. (Note: This conclusion can also be determined by disassembly code.)
Output:
From the output, even if the size of b[1024*1024] is redefined, the memory occupied by the local variable b is still 10 and has not changed.
At this point, the study of "the use of pointers in Go language" is over, hoping to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.