0

I have a problem with my python code. I have a function in c that return an array of int. As following :

int* prochepoints_tabu (Client* points, int size, int distmax){
          //instructions
          int* path;
          for (int i = 1; i < nsize; i++){
             path[i] = res[i].number;
        
           }
    return path;
}

Then I called this function in python as following :

class CA(Structure):
    _fields_ = [('number', c_int),
                ('x',c_int),
                ('y',c_int)]

ca_list = []
newlist = nodes.copy()
newlist.pop('0')
for key, el in newlist.items():
    ca = CA()
    ca.number = int(key)
    ca.x = el[0]
    ca.y = el[1]
    ca_list.append(ca)

ca_array = (CA * len(ca_list))(*ca_list)
distmax = c_int(vehicles[8]['charged'])
cap = c_int(vehicles[8]['capacity'])
res =(CA * len(vehicles))()
dll = CDLL('./functions.so')
dll.prochepoints_tabu.argtypes = [POINTER(CA), c_int, c_int]
dll.prochepoints_tabu.restype = POINTER(c_int)
path = dll.prochepoints_tabu(ca_array, len(ca_array), vehicles[1]['charged'])

And when I compile this program I've this message of error : " path = dll.prochepoints_tabu(ca_array, len(ca_array), vehicles[1]['charged']) OSError: exception: access violation writing 0x0000000000000004"

Thus, how can I return an array in python from C without knowing his size, because I want to store this array in python

Thank you very much for your help

13
  • path is uninitialized. Try: int *path = malloc(sizeof(*path) * nsize); But python will have to do a free on this pointer when done with it and I don't know how to get it to do this. You may have to have python pass path as an arg with sufficient space. You'll have to look deeper into the python docs for calling C functions Commented Jun 10, 2022 at 1:14
  • Thanks for your answer. I've changed this, but it seems not work too. I modified my parameter in the aim to be able to communicate with the variable in python, but i'm not sure that it works Commented Jun 10, 2022 at 1:33
  • I've got this new error : argument 4: <class 'TypeError'>: expected LP_c_long instance instead of _ctypes.PyCPointerType Commented Jun 10, 2022 at 1:36
  • I think I've a problem with the initialisation of pls variable (pls = POINTER(c_int) that I've added to the parameter), but I don't know Commented Jun 10, 2022 at 1:36
  • 1
    If you return a pointer (containing a number of records) with unknown size how would you know how to interpret it afterwards? Seems like a design flaw here. There are many examples how to pass pointers back and forth, here's one: stackoverflow.com/questions/58226790/…. Plus, there's a lot of follow up info in the comments, add it to the question instead. Commented Jun 11, 2022 at 17:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.