如何从Golang访问C指针数组 [英] How to access a C pointer array from Golang
问题描述
我正在为使用FFmpeg的windows平台编写一个应用程序,它是golang wrapper goav,但我无法理解如何使用C指针访问数组。
我试图让存储在AVFormatContext类中的流在go中使用,并最终将帧添加到OpenGl中的纹理中,以制作具有较酷转场的视频播放器。
我认为理解如何转换和访问C数据会使编码变得更容易。
我已将所有相关部分C代码,包装器和我的代码,如下所示:
typedef struct AVFormatContext {
unsigned int nb_streams;
AVStream **流;
Golang goav wrapper
package avutil
//#cgo pkg-config:libavformat
//#include< libavformat / avformat.h>
导入C
导入(
不安全
)
类型Context C.struct_AVFormatContext;
func(ctxt * Context)StreamsGet(i uintptr)* Stream {
streams:=(** Stream)(unsafe.Pointer(ctxt.streams));
//我认为这是出错的地方,我是这个品牌的新手
return(* Stream)(unsafe.Pointer(uintptr(unsafe.Pointer(streams))+ i * unsafe .Sizeof(*流)));
}
我的Golang代码
package main
importgithub.com/giorgisio/goav/avformat
func main(){
ctx:=& avformat.Context {} //启动它的实际函数为流创建mallocz
stream:= ctx.StreamsGet(0)
/ /做流的东西...
}
在C中,它看起来像我只是有做只是流[我],但不会在工作中,所以我添加了一个函数的包装使用我的问题此处。
但是我没有收到数据;它看起来像我得到一个指向随机内存的地方。那么,我怎样才能从golang访问这些元素呢?任何资源也会有所帮助;我将会投入相当一段时间。
正如你注意到的那样,问题在于: code:
func(ctxt * Context)StreamsGet(i uintptr)* Stream {
streams:=(**流)(unsafe.Pointer(ctxt.streams));
//我认为这是出错的地方,我是这个品牌的新手
return(* Stream)(unsafe.Pointer(uintptr(unsafe.Pointer(streams))+ i * unsafe .Sizeof(*流)));
$ / code>
在代码中,变量 streams
是双指针,因此将偏移量添加到 streams
的结果也是一个双指针(即类型为 **流
)。但是,在你的片段中,它被转换为 * Stream
,这是不正确的。正确的代码是:
$ b
func(ctxt * Context)StreamsGet(i uintptr)* Stream {
streams:= (** Stream)(unsafe.Pointer(ctxt.streams))
//添加偏移量然后将它转换为** Stream
ptrPtr:=(** Stream)(unsafe.Pointer(uintptr( unsafe.Pointer(streams))+ i * unsafe.Sizeof(* streams)))
return * ptrPtr
}
$ b $
其他说明:
如果您想要避免在 Go 你可以定义一个 helper 函数来访问C端的指针元素(例如流),如下所示:
/ *
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 )
返回c->流[idx];
返回NULL;
$ /
导入C
导入(
不安全
)
类型上下文C.struct_AVFormatContext
类型Stream C.struct_AVStream
func(ctx * Context)StreamAt(i int)* Stream {
p:=(* unsafe.Pointer)(unsafe.Pointer(ctx。流))
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)
}
您可以选择 ptr_at
函数,它接受通用(任意)双指针作为它的参数,或者更具体的 stream_at
函数,它只接受指向 AVFormatContext
作为它的参数。前一种方法可用于从任何双指针访问元素,例如: AVProgram **
, AVChapter **
,等等。如果我们需要实施额外的处理,比如边界检查,则后面的方法更可取。
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.
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.
这篇关于如何从Golang访问C指针数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!