pointer basics
package main
import "fmt"
func main() {
var p *int
p = new(int)
*p = 1
fmt.Println(p, &p, *p)
}
Output
0xc04204a080 0xc042068018 1
Go
*
represents the value stored in the pointer address, &
is the address to take a value. For the pointer, it’s important to understand that the pointer stores the address of a value, but the pointer itself also needs the address to store
As above p
is a pointer whose value is memory address 0xc04204a080</code b> and
p
memory address 0xc042068018
memory address 0xc04204a080
memory address 0 1
1
The
error instance
In golang if we define a pointer and assign it to it like a normal variable, such as the code below
package main
import "fmt"
func main() {
var i *int
*i = 1
fmt.Println(i, &i, *i)
}
Would have reported such an error
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x1 addr=0x0 pc=0x498025]
The reason for this error is that go
when you initialize the pointer it gives the pointer I
and it gives the pointer nil
, but I
is the address of * I
, 0 nil
1 the system doesn’t give the address of 2 * I
3, So there’s definitely an error in assigning * I
and it’s very easy to solve this problem, you can just create a block of memory to allocate to an assignment object before you assign to a pointer
package main
import "fmt"
func main() {
var i *int
i = new(int)
*i = 1
fmt.Println(i, &i, *i)
}
Read More:
- panic: runtime error: index out of range
- 0xc0000005: solution to access conflict when writing to location 0x00000000
- Golang gets the list of files under the folder
- Go run error go run: cannot run non main package
- There is an unhandled exception at: 0xc0000005: an access conflict occurred while reading location 0x00000000.
- Memory error unhandled exception: 0xc0000005: read location: 0x00000
- VC Critical error detected c0000374 crash problem and solution
- How to compare the equality of structures in golang
- Three possibilities of “unhandled exception: 0xc0000005: access conflict when reading location 0x00000000”
- error C2057: expected constant expression (Can the size of an array in C language be defined when the program is running?)
- “DNET: failed to open device eth1” error resolution of nmap
- How to get all the keys of map by golang
- Error while trying to retrieve text for error ORA-01019 solution
- Unhandled exception: 0xc0000005: access violation while reading location 0x00000000
- Analysis of compilation errors of “error conflicting types for function”
- stay web.xml Configure error page in
- Java – how to shuffle an ArrayList
- session_ start(): open(SESSION_ FILE, O_ Rdwr)) failed: processing of permission denied (13)
- C language write() function analysis: write failed bad address
- How to import Python from relative path