doubeng3216 2014-03-07 21:38
浏览 1
已采纳

追加到另一个结构内部的结构片时,无效的内存地址崩溃

I do not understand why I get invalid memory address panic when appending to slice of structures inside another structure.

I get the following error when running the code.

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x400d5f]

goroutine 3 [running]:
main.Pairs.CollectTickers(0x0, 0x0)
        test.go:32 +0x15f
created by main.main
        test.go:42 +0x42

goroutine 1 [sleep]:
time.Sleep(0x174876e800)
        /usr/lib/go/src/pkg/runtime/ztime_linux_amd64.c:19 +0x2f
main.main()
        test.go:43 +0x57
exit status 2

This code produces the following error:

package main

import (
        "fmt"
        "sync"
        "time"
)

var PairNames = []string{ "kalle", "kustaa", "daavid", "pekka" }

type Data struct {
        a int
        b int
}

type Tickers struct {
    Tickers []Data
}

type Pairs struct {
        Pair map[string]*Tickers
        Mutex sync.Mutex
}

func (pairs Pairs) CollectTickers() {
        PairCount := len(PairNames)
        for x := 0; x <= 1000; x++ {
                for i := 0; i < PairCount-1; i++ {
                        var data Data
                        data.a = i * x
                        data.b = i + x
                        pairs.Mutex.Lock()
                        pairs.Pair[PairNames[i]].Tickers = append(pairs.Pair[PairNames[i]].Tickers, data)
                        pairs.Mutex.Unlock()
                        fmt.Printf("a = %v, b = %v
", data.a, data.b)
                }
        }
}


func main() {
        var pairs Pairs
        go pairs.CollectTickers()
        time.Sleep(100 * time.Second)
}
  • 写回答

2条回答 默认 最新

  • dsklzerpx64815631 2014-03-07 21:56
    关注

    The error is at:

    pairs.Pair[PairNames[i]].Tickers = append(pairs.Pair[PairNames[i]].Tickers, data)
    

    Tickers is a nil pointer, since you haven't yet allocated a Tickers instance.

    I'm not sure if this is what you intended, but it compiles and runs successfully.

    package main
    
    import (
        "fmt"
        "sync"
        "time"
    )
    
    var PairNames = []string{"kalle", "kustaa", "daavid", "pekka"}
    
    type Data struct {
        a int
        b int
    }
    
    type Tickers struct {
        Tickers []Data
    }
    
    type Pairs struct {
        Pair  map[string]*Tickers
        Mutex sync.Mutex
    }
    
    func (pairs Pairs) CollectTickers() {
        PairCount := len(PairNames)
        for x := 0; x <= 1000; x++ {
            for i := 0; i < PairCount-1; i++ {
                var data Data
                data.a = i * x
                data.b = i + x
                pairs.Mutex.Lock()
                name := PairNames[i]
                if t, ok := pairs.Pair[name]; ok {
                    t.Tickers = append(t.Tickers, data)
                } else {
                    pairs.Pair[name] = &Tickers{
                        Tickers: []Data{data},
                    }
                }
                pairs.Mutex.Unlock()
                fmt.Printf("a = %v, b = %v
    ", data.a, data.b)
            }
        }
    }
    
    func main() {
        var pairs = Pairs{
            Pair: make(map[string]*Tickers),
        }
        go pairs.CollectTickers()
        time.Sleep(1 * time.Second)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。