doulun7739 2016-03-10 18:35
浏览 325
已采纳

Golang Cgo:恐慌:运行时错误:cgo参数具有指向Go指针的Go指针

I am working with a C library that, unlike below, I do not control. I need to pass to a C function a pointer to an array that also contains pointers.

package main

/*
#include <stdio.h>

typedef int* pInt;

void foo(pInt p[]) {
  printf("foo()
");
}
*/
import "C"
import "unsafe"

func main() {
    var i C.int
    var p1 C.pInt = (*C.int)(unsafe.Pointer(&i))
    var p2 C.pInt = (*C.int)(unsafe.Pointer(&i))
    var ps []C.pInt = []C.pInt{p1, p2}
    C.foo(unsafe.Pointer(&ps[0]))
}

This code results in the error panic: runtime error: cgo argument has Go pointer to Go pointer. I am wondering how I can rewrite the Go portion of this code so that it satisfies Cgo's pointer rules. My hope is that I can do this without having to write C helper code.

  • 写回答

2条回答 默认 最新

  • dongyuruan2957 2016-03-25 02:03
    关注

    Modifying OneOfOne's solution without the C helper functions, although it is nice to see how easy C helper functions are to write in the comments of the go file.

    package main
    
    /*
    #include <stdio.h>
    
    typedef int* pInt;
    
    void foo(pInt p[]) { // you probably wanna pass a len to the function.
        *p[0] = 100;
        printf("foo()
    ");
    }
    
    */
    import "C"
    import "unsafe"
    
    func main() {
        var (
            i, sz  = 0, 2
            arr    = (*C.pInt)(C.malloc(C.size_t(sz)))
            ps     = (*[100000]C.pInt)(unsafe.Pointer(arr))[:sz:sz]
            p1, p2 = (C.pInt)(unsafe.Pointer(&i)), (C.pInt)(unsafe.Pointer(&i))
        )
        ps[0], ps[1] = p1, p2
        C.foo(arr)
        C.free(unsafe.Pointer(arr))
        println("i", i)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • douqiang1910 2016-03-10 21:50
    关注

    Bad news, you have to define your helpers in C. Good news, well it's only 2 lines.

    package main
    
    /*
    #include <stdlib.h> // for malloc/free
    #include <stdio.h>
    
    typedef int* pInt;
    
    void foo(pInt p[]) { // you probably wanna pass a len to the function.
        *p[0] = 100;
        printf("foo()
    ");
    }
    
    pInt * allocArray(size_t ln) { return (pInt*) malloc(ln * sizeof(pInt)); }
    void freeArr(pInt * p) { free(p); }
    
    */
    import "C"
    import "unsafe"
    
    func main() {
        var (
            i, sz  = 0, 2
            arr    = C.allocArray(C.size_t(sz))
            ps     = (*[100000]C.pInt)(unsafe.Pointer(arr))[:sz:sz]
            p1, p2 = (C.pInt)(unsafe.Pointer(&i)), (C.pInt)(unsafe.Pointer(&i))
        )
        ps[0], ps[1] = p1, p2
        C.foo(arr)
        C.freeArr(arr)
        println("i", i)
    }
    
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Erasure Code纠删码表
  • ¥15 用vite创建的vue3项目,404重定向不起作用??
  • ¥15 关于#c语言#的问题:一个球从80米高度自由落下,每次落地后反弹的高度为原高度的一半计算6次小球反弹的高度.(反弹结果取整,使用走走for循环结构)
  • ¥15 SurfaceControl的screenshot问题
  • ¥15 基于51单片机的oled菜单代码,要C语言,模块化编程!
  • ¥15 JAVAswing,设计一个扑克牌什么的
  • ¥50 python ctypes调用dll实现分析
  • ¥40 用python解决数据统计问题
  • ¥100 是否有方案能通过抓包分析得到移动应用的名称和包名信息?
  • ¥15 opencv检测不到轮廓