passing arrays with ctypes

15,122

The first argument's type is POINTER(POINTER(c_int16)) not POINTER(ARRAY(c_int16,size)).

Here's a short example:

x.c (compiled with cl /LD x.c:

#include <stdlib.h>
#include <stdint.h>
__declspec(dllexport) void read(int16_t** input, size_t size)
{
  int i;
  int16_t* p = (int16_t*) malloc (size*sizeof(int16_t));
  for(i=0;i<size;i++)
    p[i] = i;
  *input = p;
}
__declspec(dllexport) void release(int16_t* input)
{
    free(input);
}

x.py

from ctypes import *
x = CDLL('x')
x.read.argtypes = POINTER(POINTER(c_int16)),c_size_t
x.read.restype = None
x.release.argtypes = [POINTER(c_int16)]
x.release.restype = None
p = POINTER(c_int16)()
x.read(p,5)
for i in range(5):
    print(p[i])
x.release(p)

Output:

0
1
2
3
4

Note this leaves you with potential memory leak if you don't remember to free the malloc. A better way would be to allocate the buffer in Python and tell the C function the size:

x.c

#include <stdlib.h>
#include <stdint.h>
__declspec(dllexport) void read(int16_t* input, size_t size)
{
  int i;
  for(i=0;i<size;i++)
    input[i] = i;
}

x.py

from ctypes import *
x = CDLL('x')
x.read.argtypes = POINTER(c_int16),c_size_t
x.read.restype = None
p = (c_int16*5)()
x.read(p,len(p))
print(list(p))

Output

[0, 1, 2, 3, 4]
Share:
15,122

Related videos on Youtube

erik
Author by

erik

Updated on September 15, 2022

Comments

  • erik
    erik over 1 year

    I have a C function

    void read_FIFO_AI0(int16_t** input, size_t size, NiFpga_Session* session, NiFpga_Status* status)
    {
      *input = (int16_t*) malloc (size*sizeof(int16_t));
      // function that populates the array *input
    }
    

    which populates the array "*input". Now I want to pass the data in that array to python for further processing. I try to use ctypes to do that:

    def read_FIFO_AI0(size,session,status):
        _libfpga.read_FIFO_AI0.argtypes = [POINTER(ARRAY(c_int16, size)), c_int, POINTER(c_uint32), POINTER(c_int32)]
        _libfpga.read_FIFO_AI0.restype = None
    
        values = (c_int16*size)()
        _libfpga.read_FIFO_AI0(byref(values),size,byref(session),byref(status))
        return values
    

    The code executes but I get wrong results in the array. When I try to use the C function from within C I get proper results:

    size_t size=20;
    int16_t* input;
    
    read_FIFO_AI0(&input, size, &session, &status);
    

    What would be the right way to populate the array such that I can access the data in Python? I'm not tied to using a pointer to an array that gets populated, I would also be fine with creating the array in the C function and sending this as a return to Python, but I did not get to work either.

  • erik
    erik over 9 years
    I implemented the second method and it works like a charm. Thanks! Does this mean, that memory allocation and releasing are handled by Python completely? So I don't have to worry about memory leaks or similar things?
  • Mark Tolonen
    Mark Tolonen over 9 years
    Yes, p above is a reference to the buffer. When p goes out of scope the buffer's reference count will decrement and it will be freed if it is the last reference.