指向 scala-native 中的指针的指针
Posted
技术标签:
【中文标题】指向 scala-native 中的指针的指针【英文标题】:pointer to pointer in scala-native 【发布时间】:2017-05-02 04:25:55 【问题描述】:我正在尝试将 czmq 与 scala-native 一起使用,但我还没有找到在 scala-native 中创建指针的方法。
这里是外部:
@native.link("czmq")
@native.extern
object czmq
//struct _zsock_t
// uint32_t tag; // Object tag for runtime detection
// void *handle; // The libzmq socket handle
// char *endpoint; // Last bound endpoint, if any
// char *cache; // Holds last zsock_brecv strings
// int type; // Socket type
// size_t cache_size; // Current size of cache
// uint32_t routing_id; // Routing ID for server sockets
//;
type zsock_t = native.CStruct7[
native.CUnsignedLongLong,
native.Ptr[Byte],
native.CString,
native.CString,
native.CInt,
native.CSize,
native.CUnsignedLongLong]
def zsock_new_push(endpoint: native.CString): native.Ptr[zsock_t] = native.extern
def zsock_new_pull(endpoint: native.CString): native.Ptr[zsock_t] = native.extern
def zstr_send(dest: native.Ptr[Unit], str: native.CString): native.CInt = native.extern
def zstr_recv(src: native.Ptr[Unit]): native.CString = native.extern
//void zsock_destroy (zsock_t **self_p)
def zsock_destroy(self_p: native.Ptr[native.Ptr[zsock_t]]):Unit = native.extern
这是我的简单主要方法:
object Main
def main(args: Array[String]): Unit =
val push = czmq.zsock_new_push(c"inproc://example")
val pull = czmq.zsock_new_pull(c"inproc://example")
czmq.zstr_send(push.cast[Ptr[Unit]], c"Hello World")
val s = fromCString(czmq.zstr_recv(pull.cast[Ptr[Unit]]))
println("msg: "+s)
czmq.zsock_destroy(push) // doesn't compile
czmq.zsock_destroy(pull) // doesn't compile
所以问题是如何使push
和pull
变量指针像c 中的&
一样?
【问题讨论】:
【参考方案1】:我已经联系了 gitter 上的 scala-native 团队,答案是使用 stackalloc
val ppush = stackalloc[Ptr[czmq.zsock_t]]
val ppull = stackalloc[Ptr[czmq.zsock_t]]
!ppush = push
!ppull = pull
czmq.zsock_destroy(ppush)
czmq.zsock_destroy(ppull)
【讨论】:
以上是关于指向 scala-native 中的指针的指针的主要内容,如果未能解决你的问题,请参考以下文章
Cython 中的这个声明是啥? cdef PyObject **工人。它是指向指针的指针吗?