Home > other >  Passing objects by reference in python with ctypes
Passing objects by reference in python with ctypes

Time:02-17

I am trying to use ctypes to interface with c code. I am trying to pass obj by reference to c and I am using ndpointer function from numpy to pass to the function.

My C code:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>


void sum(int *a, int *b, int *c, size_t m) {
    int ix;
    for (ix=0; ix<m; ix  ) {
        printf("%d: (%d,%d)\n",ix, a[ix], b[ix]);
        c[ix] = a[ix]   b[ix];
    } 
}

Compiled it to a shared library on ubuntu 20.04 using

gcc -Wall -fPIC -shared -pedantic -shared test.c -o test.so

And my python wrapper is:

import ctypes
from numpy.ctypeslib import ndpointer
import numpy as np

add = ctypes.CDLL('testing_ctypes/test.so').sum
add.argtypes = [ndpointer(dtype=np.int64, ndim=1, flags="aligned, c_contiguous"), ndpointer(dtype=np.int64, ndim=1, flags="aligned, c_contiguous"), ndpointer(dtype=np.int64, ndim=1,flags="aligned, c_contiguous"), ctypes.c_size_t]
add.restype = None
def test_add(a, b):
    c = np.array([0,0,0], dtype=np.int64)
    add(a,b,c,5)
    return c

op = test_add(np.array([1, 10, 5], np.int64), np.array([5, 4, 6], np.int64))

When I run this code I get the output:

0: (1,5) 
1: (0,0)  
2: (10,4)  
3: (0,0)  
4: (5,6)

Why are the values not contiguous? I tried different flags given in numpy reference but none of them have any effect on output. I am super confused why this would be the output. Any help is appreciated!

CodePudding user response:

Your C function takes int * arguments but your np.array is using 64-bit integers. In C, int is 4 bytes (32-bit). Try np.int32.

  • Related