登录
首页 >  Golang >  Go问答

在golang中访问C数组

来源:stackoverflow

时间:2024-04-10 12:54:35 163浏览 收藏

目前golang学习网上已经有很多关于Golang的文章了,自己在初次阅读这些文章中,也见识到了很多学习思路;那么本文《在golang中访问C数组》,也希望能帮助到大家,如果阅读完后真的对你学习Golang有帮助,欢迎动动手指,评论留言并分享~

问题内容

我有两个文件,module.gotest.py。我的目标是加快在 python 中完成的一些计算,但在访问 go 中的整数数组时遇到问题。

module.go

package main

import "c"

//export example
func example(testarray []c.int) c.int {
    return testarray[2]
}

func main() {}

和 python 中的简单测试文件:

from ctypes import *

# load compiled go module
lib = cdll.loadlibrary("./gomodule.so")
# we are passing an array of 256 elements and recieving integer
lib.example.argtypes = [c_int * 256]
lib.example.restype = c_int
pyarr = [x for x in range(256)]
# make c array from py array
arr = (c_int * len(pyarr))(*pyarr)
print lib.example(arr)

使用 go build -buildmode=c-shared -o gomodule.so module.go 编译 go 模块并启动 python 文件后,我得到:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x12 pc=0x7fb18b6e688c]

goroutine 17 [running, locked to thread]:
main.Example(...)
        /home/metro/go/src/github.com/golubaca/carinago/module.go:7
main._cgoexpwrap_53c1c00d0ad3_Example(0xa, 0x7fff33a2eac0, 0x7fff33a2ea70, 0x722a921de6cae100)
        _cgo_gotypes.go:47 +0x1c
Aborted (core dumped)

我知道 c 数组与 go 不同,但找不到任何教程如何在不恐慌的情况下访问它的值。


解决方案


这是惯用的、高效的 go 解决方案(避免反射)。

module.go

package main

import "c"

import "unsafe"

//export example
func example(carray *c.int, csize c.int, i c.int) c.int {
    gslice := (*[1 << 30]c.int)(unsafe.pointer(carray))[:csize:csize]
    return gslice[i]
}

func main() {}

test.py

from ctypes import *

# load compiled go module
lib = cdll.loadlibrary("./gomodule.so")
# we are passing an array of 256 elements and receiving an integer
lib.example.argtypes = [c_int * 256]
lib.example.restype = c_int
pyarr = [x for x in range(256)]
# make c array from py array
arr = (c_int * len(pyarr))(*pyarr)
print(lib.example(arr, len(arr), 4))

输出:

$ go build -buildmode=c-shared -o gomodule.so module.go
$ python test.py
4
$

c 中的数组无法自动转换为 go 中的切片。请注意,在 go 中,切片有两部分:长度和指向支持数据的指针。

因此您必须从指针手动创建切片。

package main

import (
    "c"
    "reflect"
    "unsafe"
)

//export example
func example(carr *c.int, size int, idx int) c.int {

    // build the slice manually using unsafe
    var slice []c.int
    header := (*reflect.sliceheader)(unsafe.pointer(&slice))
    header.cap = size
    header.len = size
    header.data = uintptr(unsafe.pointer(carr))

    return slice[idx]
}

func main() {}

然后你可以在 python 代码中调用 go 导出的函数,如下所示:

from ctypes import *

# Load compiled go module
lib = cdll.LoadLibrary("./gomodule.so")
# We are passing an array of 256 elements and recieving integer
lib.Example.argtypes = [c_int * 256]
lib.Example.restype = c_int
pyarr = [x for x in range(256)]
# Make C array from py array
arr = (c_int * len(pyarr))(*pyarr)
print lib.Example(arr, len(arr), 4)

上面的示例应该打印数组的第四个索引元素

以上就是《在golang中访问C数组》的详细内容,更多关于的资料请关注golang学习网公众号!

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