I am trying to write a Go function (in a Windows DLL) that accepts a supplied memory buffer allocated in a C++ program (the calling program) and access its elements using a 2D slice.
I don't want to copy any of the elements for performance reasons.
In the code below, based on Google searches, I can do this using a 1D slice.
However, if I extend the same idea to a 2D slice, it does not work. I am only able to access elements in one of the dimensions
Any attempt to pass an index greater than 0 in the first dimension results in a crash.
Can anyone help?
package main
import "C"
import "unsafe"
//export foo
func foo(width int, height int, input_buffer *uint16) int {
input_array_length := width * height
input_data := (*[1 << 30]C.ushort)(unsafe.Pointer(input_buffer)) [:input_array_length:input_array_length] // this works fine
input_data[5] = 4 // works fine
input_data_2D := (*[1 << 15][1 << 15]C.ushort)(unsafe.Pointer(input_buffer))[:width:width][:height:height]
input_data_2D[0][1] = 2 // works fine
input_data_2D[1][0] = 4 // This results in a run time crash.
return 0
}
func main() {}