dqv84329 2014-09-15 04:44
浏览 56
已采纳

在Go中访问原始C结构的字段

I'm trying to use OpenCV from Go. OpenCV defines a struct CvMat that has a data field:

typedef struct CvMat
{
    ...
    union
    {
        uchar* ptr;
        short* s;
    } data;
}

I'm using the go bindings for opencv found here. This has a type alias for CvMat:

type Mat C.CvMat

Now I have a Mat object and I want to access the data field on it. How can I do this? If I try to access _data, it doesn't work. I printed out the fields on the Mat object with the reflect package and got this:

...
{data github.com/lazywei/go-opencv/opencv [8]uint8  24 [5] false}
...

So there is a data field on it, but it's not even the same type. It's an array of 8 uint8s! I'm looking for a uchar* that is much longer than 8 characters. How do I get to this uchar?

  • 写回答

1条回答 默认 最新

  • dongmao3131 2014-09-15 06:06
    关注

    The short answer is that you can't do this without modifying go-opencv. There are a few impediments here:

    1. When you import a package, you can only use identifiers that have been exported. In this case, data does not start with an upper case letter, so is not exported.

    2. Even if it was an exported identifier, you would have trouble because Go does not support unions. So instead the field has been represented by a byte array that matches the size of the underlying C union (8 bytes in this case, which matches the size of a 64-bit pointer).

    3. Lastly, it is strongly recommended not to expose cgo types from packages. So even in cases like this where it may be possible to directly access the underlying C structure, I would recommend against it.

    Ideally go-opencv would provide an accessor for the information you are after (presumably one that could check which branch of the union is in use, rather than silently returning bad data. I would suggest you either file a bug report on the package (possibly with a patch), or create a private copy with the required modifications if you need the feature right away.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上