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 go ctypes
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 go ctypes
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
2 days ago
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
2 days ago
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 go ctypes
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 go ctypes
python go ctypes
asked 2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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 2 days ago
peterSO
87.4k13152167
87.4k13152167
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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 2 days ago
answered 2 days ago
ssemilla
1,736313
1,736313
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
add a comment |
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
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
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
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
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
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
2 days ago
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
2 days ago