I have a slice that I need to change from _Ctype_int to int.
fmt.Printf("Slice Type: %T", slice) //Returns Slice Type: []main._Ctype_int
Do you know of a simple conversion? Thank you!
Here is the code that returns a pointer to array in C++
#include "camera.hxx"
#include <iostream>
Camera::Camera()
{
}
int* Camera::retrieveDataPointerPerBuffer() {
const int size = 640 * 512;
static int cameraData[size];
for (int i = 0; i < size; i++) {
cameraData[i] = i;
} //Generates dummy 512x640 camera data
int* pSrc = cameraData;
return pSrc;
} //C++ file
This code calls the c++ function in Go.
func myCamera() {
cam := camera.NewCamera()
pSrc := cam.RetrieveDataPointerPerBuffer()
arraySize := 512 * 640
slice := (*[1 << 30]C.int)(unsafe.Pointer(pSrc))[:arraySize:arraySize]
fmt.Printf("
Slice Type: %T
", slice)
}
func main() {
go myCamera()
time.Sleep(15 * time.Millisecond)
}
I am using SWIG to wrap the C++ for Go
The size of a C int
and a Go int
are implementation dependent and they are not necessarily the same.
For example, to convert between different memory layouts,
package main
import (
"fmt"
"unsafe"
)
import "C"
func main() {
cInts := []C.int{0, 1, 2}
fmt.Println(C.sizeof_int, cInts)
goInts := make([]int, len(cInts))
for i, ci := range cInts {
goInts[i] = int(ci)
}
fmt.Println(unsafe.Sizeof(int(0)), goInts)
}
Output:
4 [0 1 2]
8 [0 1 2]
$ go version
go version devel +0349f29a55 Thu Feb 21 15:14:45 2019 +0000 linux/amd64
$ gcc --version
gcc (Ubuntu 8.2.0-7ubuntu1) 8.2.0
$ g++ --version
g++ (Ubuntu 8.2.0-7ubuntu1) 8.2.0
$