dongxin1999 2019-02-21 17:32
浏览 482
已采纳

我怎样才能改变“_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 2019-02-21 18:12
    关注

    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
    $ 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试