This is the seventh day of my participation in the August More text Challenge. For details, see:August is more challenging

The concept of Pointers

A program stores its values in memory. Each block of memory has an address, and the variable that stores this address is called a pointer variable, a pointer

It is usually represented by a hexadecimal number, such as 0x6B0820 or 0xf84001D7F0

A pointer variable can point to the memory address of any value. It points to the memory address of that value, which takes up 4 bytes on a 32-bit machine and 8 bytes on a 64-bit machine, regardless of the size of the value it points to.

A pointer can point to a value of any type, but specifying the type of the pointer is important in the actual encoding. A pointer type preceded by an asterisk is used to get what the pointer points to

Referring to a value using a pointer is called an indirect reference

Conclusion:

  • It is used to manage memory
  • A pointer is a special variable
  • It stores the memory address of another variable
  • A pointer is a variable that stores the memory address of another variable
  • As we all know, variables are handy placeholders for referencing computer memory addresses
  • A pointer variable can point to the memory address of any value and it points to the memory address of that value.

In the figure above, variable b has a value of 156 and is stored at memory address 0x1040a124. The variable A holds the address of b, and now a is considered to point to B.

Functions and application scenarios of Pointers

role

  1. Save memory space and improve execution efficiency (when the operation data amount is large)
  2. Access the value of a variable

Application scenarios

  1. Modify the value of a variable
  2. Access the value of a variable

Gets the address of the variable

Golang’s address symbol is &, and putting it in front of a variable returns the memory address of that variable

Package main import "FMT" func main() {var a int = 10 FMT.Printf(" variable address :%X\n", &a) // variable address: 20818a220}Copy the code

Pointer variable operation

  • First define the variable, and then define the pointer to get the address of the variable, so the variable stores the address, and the actual value is the space that the address points to
Package main import "FMT" func main() {var num1 int = 100 FMT.Printf(" %v\n", &num1) Var num2 *int = &num1 fmt.Printf(" num2 *int = &num1 fmt.Printf(" num2 *int = &num1 FMT.Printf(" num2 *int = &num1 FMT.Printf(" num2 *int = &num1 FMT.Printf(" num2 *int = &num1 FMT.Printf(" num2 *int = &num1 FMT. %v\n", &num2) // address of variable: 0xc00007c020}Copy the code

The address fetch operator & : commonly used in pointer operations, & uses the address

Value operator * : In pointer operations, * finds the value of the address that points to the space based on the address

*int: points to an integer

*float32: Points to a 32-bit floating-point type

Null pointer

When a pointer is defined and not allocated to any variable, its value is nil. A nil pointer is also called a null pointer. Nil is conceptually the same as null,None,nil, and null in other languages in that it refers to a zero or null value. A pointer variable is usually abbreviated to PTR

Null pointer judgment:

if(ptr ! = nil) // PTR is not null if(PTR == nil) // PTR is nullCopy the code

Notes:

  1. Value types generally have a corresponding pointer type, formatThe type of the data like int-> int float64->*float64
  2. A set of hexadecimal data starting with 0x
  3. What are the reference types in go? Pointer, slice, map, chan,interface
  4. Value type: The value stored by a variable is a value type, usually allocated on the stack
  5. Reference type: the variable stores the address, and the space corresponding to the address stores the actual value. Generally, reference types are allocated in the heap, and if there is no variable to reference the space, it is garbage collected by the operating system.
  6. Pointers in the GO language do not have pointer operations, because misreferences to Pointers can cause memory leaks and lead to a series of crashes
  7. Change the value of a pointer to a variable without changing the address. Change the value of a pointer to a variable
  8. A pointer variable can point to the memory address of any value
  9. Pointers can also point to another pointer, and can be nested to any depth, so you can have multiple levels of indirect references, but in most cases this will make your code structure unclear
  10. When a pointer is defined and not allocated to any variable, it has a value of nil. Backreferencing a null pointer is illegal and will crash the program

The last

Just learn Golang, hope everyone supervise and work together

Interested partners, welcome to follow my subscription number: EntrepreneurialG