登录
首页 >  Golang >  Go问答

新手请注意:出现了无效内存访问错误

来源:stackoverflow

时间:2024-02-27 15:57:23 130浏览 收藏

本篇文章向大家介绍《新手请注意:出现了无效内存访问错误》,主要包括,具有一定的参考价值,需要的朋友可以参考一下。

问题内容

go 的世界非常绿色 - 事实上,这是我在 go 中的第一个程序。我正在编写一个反转链表的算法,特别是来自这个leetcode。下面的代码片段没有透露我的算法,只透露了我用来测试我的实现的 main() 函数。调试后,我发现我在 此处 箭头处失败,并显示错误消息 panic: 运行时错误: 无效的内存地址或 nil 指针取消引用

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

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

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

终于介绍完啦!小伙伴们,这篇关于《新手请注意:出现了无效内存访问错误》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>