douxing1969 2018-04-23 17:48
浏览 222
已采纳

如何从Golang访问C指针数组

I'm writing an app for the windows platform using FFmpeg and it's golang wrapper goav, but I'm having trouble understanding how to use the C pointers to gain access to an array.

I'm trying to get the streams stored in the AVFormatContext class to use in go, and eventually add frames to a texture in OpenGl to make a video player with cool transitions.

I think understanding how to cast and access the C data will make coding this a lot easier.

I've stripped out all the relevant parts of the C code, the wrapper and my code, shown below:

C code - libavformat/avformat.h

typedef struct AVFormatContext { 
    unsigned int nb_streams; 
    AVStream **streams; 
}

Golang goav wrapper

package avutil

//#cgo pkg-config: libavformat
//#include <libavformat/avformat.h>
import "C"
import (
    "unsafe"
)

type Context C.struct_AVFormatContext; 

func (ctxt *Context) StreamsGet(i uintptr) *Stream {
    streams := (**Stream)(unsafe.Pointer(ctxt.streams));
    // I think this is where it's going wrong, I'm brand new to this stuff 
    return (*Stream)(unsafe.Pointer(uintptr(unsafe.Pointer(streams)) + i*unsafe.Sizeof(*streams)));
}

My Golang code

package main

import "github.com/giorgisio/goav/avformat"

func main() {
    ctx := &avformat.Context{} // the actual function to initiate this does an mallocz for the streams

    stream := ctx.StreamsGet(0)

    //do stuff with stream...
}

In C it looks like I just have to do just streams[i], but that wont work in go, so I added a function to the wrapper using the technique from my question here. However I'm not getting the data; It looks like I'm getting a pointer to somewhere random in memory. So, how can I access these elements form golang? Any resources would be helpful too; I'm going to be investing a fair bit of time into this.

  • 写回答

1条回答 默认 最新

  • doom910730 2018-04-24 03:48
    关注

    As you noticed, the problem is in the following code:

    func (ctxt *Context) StreamsGet(i uintptr) *Stream {
        streams := (**Stream)(unsafe.Pointer(ctxt.streams));
        // I think this is where it's going wrong, I'm brand new to this stuff 
        return (*Stream)(unsafe.Pointer(uintptr(unsafe.Pointer(streams)) + i*unsafe.Sizeof(*streams)));
    }
    

    In the code, variable streams is double pointer, thus the result of adding offset to streams is also a double pointer (i.e. the type is **Stream). But, in your snippets, it is casted to *Stream which is incorrect. The correct code is:

    func (ctxt *Context) StreamsGet(i uintptr) *Stream {
        streams := (**Stream)(unsafe.Pointer(ctxt.streams))
        // Add offset i then cast it to **Stream
        ptrPtr := (**Stream)(unsafe.Pointer(uintptr(unsafe.Pointer(streams)) + i*unsafe.Sizeof(*streams)))
        return *ptrPtr
    }
    

    Additional note:
    If you would like to avoid pointer arithmetic in Go side, you can define a helper function for accessing the element of the pointer (i.e. streams) in C side as follows:

    /*
    void * ptr_at(void **ptr, int idx) {
        return ptr[idx];
    }
    
    struct AVStream * stream_at(struct AVFormatContext *c, int idx) {
        if (i >= 0 && i < c->nb_streams)
            return c->streams[idx];
        return NULL;
    }
    */
    import "C"
    import (
        "unsafe"
    )
    
    type Context C.struct_AVFormatContext
    type Stream C.struct_AVStream
    
    func (ctx *Context) StreamAt(i int) *Stream {
        p := (*unsafe.Pointer)(unsafe.Pointer(ctx.streams))
        ret := C.ptr_at(p, C.int(i))
    
        return (*Stream)(ret)
    }
    
    func (ctx *Context) StreamAt2(i int) *Stream {
        ret := C.stream_at((*C.struct_AVFormatContext)(ctx), C.int(i))
    
        return (*Stream)(ret)
    }
    

    You may choose either ptr_at function which accepts generic (any) double pointer as its argument, or more specific stream_at function which only accepts pointer to AVFormatContext as its argument. The former approach can be used to access element from any double pointer such as: AVProgram **, AVChapter **, etc. The later approach is preferable if we need to implement additional processing such as boundary checking.

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

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧