【问题标题】:Is pointer offset to a cython pointer?指针是否偏移到 cython 指针?
【发布时间】:2021-05-02 00:14:44
【问题描述】:

有没有办法让我稍后在 Cython 中将指针移动到 n 个字节?

例如,如果我有(伪代码):

cdef void *n_ary 
cdef void *eightbytes 
cdef void *n_ary_plus8

ary = cnp.PyArray_DATA(input_array)
eightbytes = 8
n_ary_plus8 = ary + eightbytes

我收到错误 Invalid operand types for '+' (void *; void *) 最后一行告诉我它不知道如何添加指针地址 ary 和指针八字节。看起来这应该很明显,但我在手册或这个八月的参考资料中找不到任何内容。

【问题讨论】:

    标签: numpy cython


    【解决方案1】:

    void* 算术在 C 和 C++ 标准中是不允许的。 如果你想这样做,你需要将指针转换为char*类型。

    更多信息,请查看thisthis非常小心使用此类指针强制转换的 strict aliasing rule(填充和对齐)。

    【讨论】:

      【解决方案2】:

      我能够做我需要做的事情

      cdef void *n_ary 
      cdef int eightbytes 
      cdef void *n_ary_plus8
      
      ary = cnp.PyArray_DATA(input_array)
      eightbytes = 8
      n_ary_plus8 = &ary[eightbytes]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-04
        • 2013-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-02
        相关资源
        最近更新 更多