Note that there are some explanatory texts on larger screens.

plurals
  1. POPython is passing 32bit pointer address to C functions
    primarykey
    data
    text
    <p>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 addresses within the called function. Both Python and my library are 64bit.</p> <p>The example codes below demonstrate the problem. The Python script prints the address of the data being passed to the C function. Then, the address received is printed from within the called C function. Additionally, the C function proves that it is 64bit by printing the size and address of locally creating memory. If the pointer is used in any other way, the result is a segfault.</p> <p><strong>CMakeLists.txt</strong></p> <pre><code>cmake_minimum_required (VERSION 2.6) add_library(plate MODULE plate.c) </code></pre> <p><strong>plate.c</strong></p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; void plate(float *in, float *out, int cnt) { void *ptr = malloc(1024); fprintf(stderr, "passed address: %p\n", in); fprintf(stderr, "local pointer size: %lu\n local pointer address: %p\n", sizeof(void *), ptr); free(ptr); } </code></pre> <p><strong>test_plate.py</strong></p> <pre><code>import numpy import scipy import ctypes N = 3 x = numpy.ones(N, dtype=numpy.float32) y = numpy.ones(N, dtype=numpy.float32) plate = ctypes.cdll.LoadLibrary('libplate.so') print 'passing address: %0x' % x.ctypes.data plate.plate(x.ctypes.data, y.ctypes.data, ctypes.c_int(N)) </code></pre> <p><strong>Output from python-2.7</strong></p> <blockquote> <p>In [1]: run ../test_plate.py</p> <p>passing address: 7f9a09b02320</p> <p>passed address: 0x9b02320</p> <p>local pointer size: 8</p> <p>local pointer address: 0x7f9a0949a400</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload