dongqing904999 2013-09-12 13:16
浏览 83
已采纳

无效的内存地址或nil指针取消引用

I am new to gccgo and I need help with compiling/running the below code (that works all right with the "standard" go compiler (sorry, I do not know the proper nomenclature)):

my gcc:

nailor@macbuntu:*rgo/src/tictoc-demo$ gccgo -v
Using built-in specs.
COLLECT_GCC=gccgo
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.2-0ubuntu3' --with-bugurl=file:///usr/share/doc/gccgo-4.7/README.Bugs --enable-languages=c,c++,go --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.7.2 (Ubuntu/Linaro 4.7.2-0ubuntu3) 

my first file:

nailor@macbuntu:*rgo/src/tictoc-demo$ cat tictoc.go        
package tictoc

import "fmt"
import "time"

var ticTime = time.Now()
const default_section_name = "measurement"
var section_name = default_section_name

func Tic() {
    TicM(default_section_name)
}

func TicM(name string) {
    section_name = name
    fmt.Printf("[%s] tic
", section_name)
    ticTime = time.Now()
}

func Toc() {
    TocM("")
}

func TocM(message string) {
    dur := time.Now().Sub(ticTime).Seconds()
    fmt.Printf("[%s] [%s] toc %5f
", section_name, message, dur)
}

func TocTic() {
   Toc();
   Tic();
}

func TocTicM(message string) {
   Toc();
   TicM(message);
}

my second file:

nailor@macbuntu:*rgo/src/tictoc-demo$ cat tictoc-demo.go                         
package main

import . "tictoc"

func main(){
    Tic()
    Toc()
    Toc()
    TicM("2nd tic")
    Toc()
    Toc()
    TocM("Error")
    Toc()
    TocM("More Fancy")
    Toc()
    Toc()
    TocTic()
    Toc()
}

my compilation with the error:

nailor@macbuntu:*rgo/src/tictoc-demo$ gccgo -c tictoc.go                   
nailor@macbuntu:*rgo/src/tictoc-demo$ gccgo -c tictoc-demo.go              
nailor@macbuntu:*rgo/src/tictoc-demo$ gccgo -o tictoc-demo tictoc-demo.o tictoc.o -static
nailor@macbuntu:*rgo/src/tictoc-demo$ ./tictoc-demo 
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0]

goroutine 2 [syscall]:

goroutine 1 [runnable]:
nailor@macbuntu:*rgo/src/tictoc-demo2$

What am I doing wrong here?

  • 写回答

2条回答 默认 最新

  • doukongpao0903 2013-09-12 17:08
    关注

    You are not doing anything wrong. This looks like a bug in the compiler when doing full-static linkage. Try linking with -static-libgo instead, and it should work.

    This is the backtrace in gdb:

    Program received signal SIGSEGV, Segmentation fault.
    0x0000000000000000 in ?? ()
    (gdb) bt
    #0  0x0000000000000000 in ?? ()
    #1  0x00000000004adf67 in __wrap_pthread_create ()
    #2  0x000000000040657e in runtime_newm ()
    #3  0x000000000040665b in matchmg ()
    #4  0x0000000000406f15 in syscall.Entersyscall ()
    #5  0x0000000000403e5c in runtime_MHeap_Scavenger ()
    #6  0x0000000000406e15 in kickoff ()
    #7  0x00000000004ba910 in ?? ()
    #8  0x0000000000000000 in ?? ()
    

    I'll see if there's a bug filed upstream for this already, or file one otherwise.

    (issue filed: http://golang.org/issue/6375 )

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)