0

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() {}
rohitsan
  • 1,001
  • 8
  • 31
  • I am not entirely sure this is a duplicate of https://stackoverflow.com/questions/14826319/go-cgo-how-do-you-use-a-c-array-passed-as-a-pointer though it appears similar. –  Jul 16 '20 at 13:06
  • This is not a duplicate. That other question only covers 1D slices. – rohitsan Jul 16 '20 at 14:33

0 Answers0