Return C++ integer array in Python ctypes / numpy ctypeslib incorrect output -
i attempting create c++ function return pointer array, can use in numpy. seem stuck @ basics. when print out values of array back, (which in case should numbers 0 99), after 83 don't expected results. when ask c++ function print out contents of array seems fine. know going on here?
edit:
so cannot return variable has gone out of scope. understand that. how can return array python, without passing in pointer 1 in arguments?
my python file:
python_file.py
import ctypes import itertools import numpy np h_list=np.array(range(100), dtype=int) length = len(h_list) data = h_list.astype(np.int32) lib = ctypes.cdll('/path/to/file/ctypes_test/hello.so') mynum = lib.mynum mynum.restype = np.ctypeslib.ndpointer(dtype=ctypes.c_int, shape=(length,), flags='c_contiguous') mynum.argtypes = [np.ctypeslib.ndpointer(ctypes.c_int, flags="c_contiguous"), ctypes.c_int] h = mynum(data, length) in h: print
the truncated output, can see after 83 things go funny:
output:
... ... 77 78 79 80 81 82 83 -23162618 -671088158 250053520 1 261248736 1 246869272 1 0 0 250053520 1 1359648784 32767 246824912 1
my c++ file:
hello.cpp
extern "c" { int * mynum(int *hkls, int length){ int out [length]; (int = 0; < length; i++) { out[i]=hkls[i]; } int * out_a = out; return out_a; } }
my makefile:
makefile
main: hello.cpp g++-6 -shared -o3 -wl,-install_name,hello.so -o hello.so -fpic hello.cpp
Comments
Post a Comment