简体   繁体   中英

Accessing C array in golang

I have two files, module.go and test.py . My goal is to speed up some calculations that is done in python, but have an issue accessing array of integers in go.

module.go

package main

import "C"

//export Example
func Example(testArray []C.int) C.int {
    return testArray[2]
}

func main() {}

and simple test file in 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)

After compiling go module with go build -buildmode=c-shared -o gomodule.so module.go and fire up python file I got:

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)

I get that C array is different from Go, but can't find any tutorial how to access it's values without panic.

This is the idiomatic, efficient Go solution (avoid reflection).

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))

Output:

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

An array in C cannot be automatically casted to a slice in Go. Note that in Go, a slice has two parts: a length, and a pointer to the backing data.

So you would have to manually create the slice from the pointer.

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() {}

Then you would call the Go exported function in your python code like:

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)

The example above should print the 4th index element of the array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM