为了账号安全,请及时绑定邮箱和手机立即绑定

存在与无效内存访问相关的错误

存在与无效内存访问相关的错误

Go
慕标琳琳 2022-12-19 10:45:58
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)    }}我真的很感激对这个问题的一些讨论!
查看完整描述

2 回答

?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

基本问题是您永远不会为结构指针分配内存。当你写:


var list *ListNode

您已经创建了一个指向 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


查看完整回答
反对 回复 2022-12-19
?
慕桂英4014372

TA贡献1871条经验 获得超13个赞

您必须为列表节点分配内存。创建列表节点时,更新前一个节点中的 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)

}

    

https://go.dev/play/p/qUhza05kUFT


查看完整回答
反对 回复 2022-12-19
  • 2 回答
  • 0 关注
  • 78 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信