I am learning go and cgo and ffi, I have the header file below:
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef struct MyStruct3 {
const char *const *data;
int length;
} MyStruct3;
void my_struct_3(const struct MyStruct3 *c_data);
And this go file
package main
/*
#cgo LDFLAGS: -L./release -lrust_ffi
#cgo CFLAGS: -I./target
#include <rust-ffi.h>
*/
import "C"
import (
"fmt"
"unsafe"
)
func main() {
goStrings2 := []string{"Hello", "World", "from", "Go", "again"}
// Convert Go slice to a C array of C strings
cStrings2 := make([]*C.char, len(goStrings2))
for i, s := range goStrings2 {
cStrings2[i] = C.CString(s)
defer C.free(unsafe.Pointer(cStrings2[i])) // Ensure we free the C string memory
}
cData := (**C.char)(unsafe.Pointer(&cStrings2[0]))
cStruct3 := C.MyStruct3{
data: cData,
length: C.int(len(goStrings2)),
}
C.my_struct_3((*C.struct_MyStruct3)(unsafe.Pointer(&cStruct3)))
}
I am getting the following error
panic: runtime error: cgo argument has Go pointer to unpinned Go pointer
goroutine 1 [running]:
main.main.func5(0x499ef3?)
/helloworld-ffi/cgo/main.go:97 +0x30
main.main()
/helloworld-ffi/cgo/main.go:97 +0x5e8
exit status 2
Any idea on how to solve this issue?
thank you very much