【问题标题】:Common Lisp CFFI: Assign a struct value to an array indexCommon Lisp CFFI:将结构值分配给数组索引
【发布时间】:2015-12-23 19:53:36
【问题描述】:

如何将外部数组的索引值分配为外部结构的值。例如:

 (cffi:with-foreign-objects ((x '(:struct my-struct)) 
                             (arr '(:struct my-struct) 2))
       (setf (cffi:mem-aref arr '(:struct my-struct) 0)
             (cffi:mem-ref x '(:struct my-struct))))

我预计大致相当于

struct my_struct *x;
struct my_struct arr[2];

// x is initialized somewhere

arr[0] = *x;

相反,它想调用通用函数cffi:translate-into-foreign-memory。有什么办法可以通过这个将 SETF 外来内存传递到外来内存?

【问题讨论】:

    标签: common-lisp cffi


    【解决方案1】:

    在 irc.freenode.net #lisp 上其他一些人的帮助下,我们想通了:使用 memcpy

    (cffi:with-foreign-objects ((x '(:struct my-struct)) 
                                 (arr '(:struct my-struct) 2))
           (cffi:foreign-funcall "memcpy"
                                 :pointer (cffi:mem-aptr arr '(:struct my-struct) 0)
                                 :pointer x
                                 :int (cffi:foreign-type-size '(:struct my-struct))
                                 :void))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      • 2015-02-17
      • 2016-06-20
      • 1970-01-01
      相关资源
      最近更新 更多