新来的。存在与无效内存访问相关的错误
New to GO. Having invalid memory access related errors
GO 的世界非常绿色 - 事实上,这是我在 GO 中的第一个程序。我正在编写一个反转链表的算法,特别是来自 leetcode。下面的代码片段没有公开我的算法,只公开了我用来测试我的实现的 main()
函数。调试后发现在这里箭头处失败,错误信息为panic: runtime error: invalid memory address or nil pointer dereference
.
type ListNode struct {
Val int
Next *ListNode
}
func main(){
var list *ListNode
head := list
for i := 0; i <= 5; i++ {
var dummy *ListNode
list.Val = i <--------------------- here
list.Next = dummy
dummy = list
}
result := reverseList(head)
for result != nil{
fmt.Printf("%d\t", result.Val)
}
}
非常感谢对这个问题的一些讨论!
基本问题是您永远不会为结构指针分配内存。当你写:
var list *ListNode
您已经创建了一个 pointer-to-ListNode 类型,但实际上您还没有为它分配任何内存。所以当你尝试写...
list.Val = i
您收到“无效内存地址”错误,因为您正在尝试取消引用未定义的指针。分配内存的一种方法是使用 new()
新内置函数:
var list *ListNode = new(ListNode)
你也可以获取结构的地址,像这样:
list := &ListNode{}
以上显示了正确的语法,但如果您只是简单地替换
你现有的 var
声明与上面你仍然有
代码中的逻辑问题:您不想分配任何内存
直到将第一个节点添加到列表中。这意味着我们要等待
直到我们进入 for
循环以分配内存。
通过对您的代码进行一些小改动,我们得到:
package main
import "fmt"
type ListNode struct {
Val int
Next *ListNode
}
func main() {
var head, tail *ListNode
for i := 0; i <= 5; i++ {
node := new(ListNode)
node.Val = i
if tail == nil {
// This is the first node in the list, so just point head
// and tail at the new node.
tail = node
head = tail
} else {
// There is at least one node in the list, so attach the new
// node to the tail
tail.Next = node
tail = node
}
}
result := head
for result != nil {
fmt.Printf("%d\t", result.Val)
// Don't forget to increment to the next node!
result = result.Next
}
}
运行 此代码生成:
0 1 2 3 4 5
您必须为列表节点分配内存。创建列表节点时,更新前一个节点中的 Next 字段或更新列表头(如果这是第一个节点)。
var head *ListNode
// p is pointer to head or pointer to previous node's Next.
// We start with p set as pointer to the head.
p := &head
for i := 0; i <= 5; i++ {
// Allocate a new ListNode with Val initialized.
n := &ListNode{Val: i}
// Update head or previous node'a Next field.
*p = n
// The next iteration of the loop should update
// the Next field in the node that we just created.
p = &n.Next
}
// Loop while list node is not nil.
for n := head; n != nil; n = n.Next {
fmt.Println(n.Val)
}
GO 的世界非常绿色 - 事实上,这是我在 GO 中的第一个程序。我正在编写一个反转链表的算法,特别是来自 leetcode。下面的代码片段没有公开我的算法,只公开了我用来测试我的实现的 main()
函数。调试后发现在这里箭头处失败,错误信息为panic: runtime error: invalid memory address or nil pointer dereference
.
type ListNode struct {
Val int
Next *ListNode
}
func main(){
var list *ListNode
head := list
for i := 0; i <= 5; i++ {
var dummy *ListNode
list.Val = i <--------------------- here
list.Next = dummy
dummy = list
}
result := reverseList(head)
for result != nil{
fmt.Printf("%d\t", result.Val)
}
}
非常感谢对这个问题的一些讨论!
基本问题是您永远不会为结构指针分配内存。当你写:
var list *ListNode
您已经创建了一个 pointer-to-ListNode 类型,但实际上您还没有为它分配任何内存。所以当你尝试写...
list.Val = i
您收到“无效内存地址”错误,因为您正在尝试取消引用未定义的指针。分配内存的一种方法是使用 new()
新内置函数:
var list *ListNode = new(ListNode)
你也可以获取结构的地址,像这样:
list := &ListNode{}
以上显示了正确的语法,但如果您只是简单地替换
你现有的 var
声明与上面你仍然有
代码中的逻辑问题:您不想分配任何内存
直到将第一个节点添加到列表中。这意味着我们要等待
直到我们进入 for
循环以分配内存。
通过对您的代码进行一些小改动,我们得到:
package main
import "fmt"
type ListNode struct {
Val int
Next *ListNode
}
func main() {
var head, tail *ListNode
for i := 0; i <= 5; i++ {
node := new(ListNode)
node.Val = i
if tail == nil {
// This is the first node in the list, so just point head
// and tail at the new node.
tail = node
head = tail
} else {
// There is at least one node in the list, so attach the new
// node to the tail
tail.Next = node
tail = node
}
}
result := head
for result != nil {
fmt.Printf("%d\t", result.Val)
// Don't forget to increment to the next node!
result = result.Next
}
}
运行 此代码生成:
0 1 2 3 4 5
您必须为列表节点分配内存。创建列表节点时,更新前一个节点中的 Next 字段或更新列表头(如果这是第一个节点)。
var head *ListNode
// p is pointer to head or pointer to previous node's Next.
// We start with p set as pointer to the head.
p := &head
for i := 0; i <= 5; i++ {
// Allocate a new ListNode with Val initialized.
n := &ListNode{Val: i}
// Update head or previous node'a Next field.
*p = n
// The next iteration of the loop should update
// the Next field in the node that we just created.
p = &n.Next
}
// Loop while list node is not nil.
for n := head; n != nil; n = n.Next {
fmt.Println(n.Val)
}