Accessing C array in golang











up vote
2
down vote

favorite
1












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.










share|improve this question






















  • 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

















up vote
2
down vote

favorite
1












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.










share|improve this question






















  • 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















up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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




















  • 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














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
$





share|improve this answer





















  • 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


















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






share|improve this answer























  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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
































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
$





share|improve this answer





















  • 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















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
$





share|improve this answer





















  • 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













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
$





share|improve this answer












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
$






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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












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






share|improve this answer























  • 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















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






share|improve this answer























  • 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













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






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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




















































































Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues