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

我怎样才能改变“_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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动