Accessing C array in golang
up vote
2
down vote
favorite
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.
python
add a comment |
up vote
2
down vote
favorite
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.
python
As a suggestion, have you looked at numpy? (i will openly admit i have no idea what is happening in this code here. However, calculations on "array of integers" sounds like numpy department. It has helped me a lot with speeding up calculations)
– Paritosh Singh
Nov 10 at 11:51
Yeah, i have,but I would like to use go because of some other code I have already, and I would like to integrate license check in go as.sofile, because it's at least little harder to reverse engeneer than plain python file or pyc. So main idea here is to use license check with critical part of code in go, and compile it together.
– Aleksandar
Nov 10 at 12:21
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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.
python
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.
python
python
asked Nov 10 at 11:44
Aleksandar
479722
479722
As a suggestion, have you looked at numpy? (i will openly admit i have no idea what is happening in this code here. However, calculations on "array of integers" sounds like numpy department. It has helped me a lot with speeding up calculations)
– Paritosh Singh
Nov 10 at 11:51
Yeah, i have,but I would like to use go because of some other code I have already, and I would like to integrate license check in go as.sofile, because it's at least little harder to reverse engeneer than plain python file or pyc. So main idea here is to use license check with critical part of code in go, and compile it together.
– Aleksandar
Nov 10 at 12:21
add a comment |
As a suggestion, have you looked at numpy? (i will openly admit i have no idea what is happening in this code here. However, calculations on "array of integers" sounds like numpy department. It has helped me a lot with speeding up calculations)
– Paritosh Singh
Nov 10 at 11:51
Yeah, i have,but I would like to use go because of some other code I have already, and I would like to integrate license check in go as.sofile, because it's at least little harder to reverse engeneer than plain python file or pyc. So main idea here is to use license check with critical part of code in go, and compile it together.
– Aleksandar
Nov 10 at 12:21
As a suggestion, have you looked at numpy? (i will openly admit i have no idea what is happening in this code here. However, calculations on "array of integers" sounds like numpy department. It has helped me a lot with speeding up calculations)
– Paritosh Singh
Nov 10 at 11:51
As a suggestion, have you looked at numpy? (i will openly admit i have no idea what is happening in this code here. However, calculations on "array of integers" sounds like numpy department. It has helped me a lot with speeding up calculations)
– Paritosh Singh
Nov 10 at 11:51
Yeah, i have,but I would like to use go because of some other code I have already, and I would like to integrate license check in go as
.so file, because it's at least little harder to reverse engeneer than plain python file or pyc. So main idea here is to use license check with critical part of code in go, and compile it together.– Aleksandar
Nov 10 at 12:21
Yeah, i have,but I would like to use go because of some other code I have already, and I would like to integrate license check in go as
.so file, because it's at least little harder to reverse engeneer than plain python file or pyc. So main idea here is to use license check with critical part of code in go, and compile it together.– Aleksandar
Nov 10 at 12:21
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
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
$
Nice, I tried this and the result is the same as the first answer, but file size of the shared object is smaller which is great. I must accept this as an "accepted" answer because of efficient conversion. Many thanks to both of you.
– Aleksandar
Nov 10 at 22:05
add a comment |
up vote
2
down vote
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
This is the right answer. I didn't think of headers, but now I know what to look for other types as well. Thanks a lot!
– Aleksandar
Nov 10 at 18:32
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
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
$
Nice, I tried this and the result is the same as the first answer, but file size of the shared object is smaller which is great. I must accept this as an "accepted" answer because of efficient conversion. Many thanks to both of you.
– Aleksandar
Nov 10 at 22:05
add a comment |
up vote
3
down vote
accepted
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
$
Nice, I tried this and the result is the same as the first answer, but file size of the shared object is smaller which is great. I must accept this as an "accepted" answer because of efficient conversion. Many thanks to both of you.
– Aleksandar
Nov 10 at 22:05
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
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
$
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
$
answered Nov 10 at 19:27
peterSO
89.2k13155168
89.2k13155168
Nice, I tried this and the result is the same as the first answer, but file size of the shared object is smaller which is great. I must accept this as an "accepted" answer because of efficient conversion. Many thanks to both of you.
– Aleksandar
Nov 10 at 22:05
add a comment |
Nice, I tried this and the result is the same as the first answer, but file size of the shared object is smaller which is great. I must accept this as an "accepted" answer because of efficient conversion. Many thanks to both of you.
– Aleksandar
Nov 10 at 22:05
Nice, I tried this and the result is the same as the first answer, but file size of the shared object is smaller which is great. I must accept this as an "accepted" answer because of efficient conversion. Many thanks to both of you.
– Aleksandar
Nov 10 at 22:05
Nice, I tried this and the result is the same as the first answer, but file size of the shared object is smaller which is great. I must accept this as an "accepted" answer because of efficient conversion. Many thanks to both of you.
– Aleksandar
Nov 10 at 22:05
add a comment |
up vote
2
down vote
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
This is the right answer. I didn't think of headers, but now I know what to look for other types as well. Thanks a lot!
– Aleksandar
Nov 10 at 18:32
add a comment |
up vote
2
down vote
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
This is the right answer. I didn't think of headers, but now I know what to look for other types as well. Thanks a lot!
– Aleksandar
Nov 10 at 18:32
add a comment |
up vote
2
down vote
up vote
2
down vote
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
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
edited Nov 10 at 13:28
answered Nov 10 at 12:54
ssemilla
2,762423
2,762423
This is the right answer. I didn't think of headers, but now I know what to look for other types as well. Thanks a lot!
– Aleksandar
Nov 10 at 18:32
add a comment |
This is the right answer. I didn't think of headers, but now I know what to look for other types as well. Thanks a lot!
– Aleksandar
Nov 10 at 18:32
This is the right answer. I didn't think of headers, but now I know what to look for other types as well. Thanks a lot!
– Aleksandar
Nov 10 at 18:32
This is the right answer. I didn't think of headers, but now I know what to look for other types as well. Thanks a lot!
– Aleksandar
Nov 10 at 18:32
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238602%2faccessing-c-array-in-golang%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
As a suggestion, have you looked at numpy? (i will openly admit i have no idea what is happening in this code here. However, calculations on "array of integers" sounds like numpy department. It has helped me a lot with speeding up calculations)
– Paritosh Singh
Nov 10 at 11:51
Yeah, i have,but I would like to use go because of some other code I have already, and I would like to integrate license check in go as
.sofile, because it's at least little harder to reverse engeneer than plain python file or pyc. So main idea here is to use license check with critical part of code in go, and compile it together.– Aleksandar
Nov 10 at 12:21