dongxin1999
dongxin1999
2019-02-21 17:32

我怎样才能改变“_Ctype_int”为“int”进去吗?

已采纳

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douzhui1972 douzhui1972 2年前

    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
    $ 
    
    点赞 评论 复制链接分享