Python is passing 32bit pointer address to C functions

前端 未结 4 979
说谎
说谎 2021-02-15 03:41

I would like to call my C functions within a shared library from Python scripts. Problem arrises when passing pointers, the 64bit addresses seem to be truncated to 32bit address

4条回答
  •  迷失自我
    2021-02-15 04:45

    The problem is that the ctypes module doesn't check the function signature of the function you're trying to call. Instead, it bases the C types on the Python types, so the line...

    plate.plate(x.ctypes.data, y.ctypes.data, ctypes.c_int(N))
    

    ...is passing the the first two params as integers. See eryksun's answer for the reason why they're being truncated to 32 bits.

    To avoid the truncation, you'll need to tell ctypes that those params are actually pointers with something like...

    plate.plate(ctypes.c_void_p(x.ctypes.data),
                ctypes.c_void_p(y.ctypes.data),
                ctypes.c_int(N))
    

    ...although what they're actually pointers to is another matter - they may not be pointers to float as your C code assumes.


    Update

    eryksun has since posted a much more complete answer for the numpy-specific example in this question, but I'll leave this here, since it might be useful in the general case of pointer truncation for programmers using something other than numpy.

提交回复
热议问题