我有一个名为person的结构如下:
struct person { int height,weight; };
我还创建了一个人数组如下:
struct Arrayofperson { int len; //indicates the length of this array(its supposed to be dynamic) person *p; //this is supposed to be the dynamic array of person. };
我这样做的数组如下:
struct Array_2d_ofperson{ int len; //indicates the length of this array(its supposed to be dynamic) Arrayofperson *subarray; //this is supposed to be the dynamic 2d array of person. };
这是我的代码:
#include <iostream> #include "test.h" using namespace std; #define DLLEXPORT extern "C" __declspec(dllexport) DLLEXPORT Arrayofperson create_arr_person(int len) { Arrayofperson arr_p; arr_p.len = len; arr_p.p = new person[len]; //populate the array here: for (int a = 0; a < len; a++) { arr_p.p[a].height = a; //yes they're the same,but it doesn't matter for now. arr_p.p[a].weight = a; }; return arr_p; } DLLEXPORT void print_arr_person(Arrayofperson pp) { printf("length: %d\n",pp.len); for (int b = 0; b < pp.len; b++) { printf("height,weight %d,%d\n",pp.p[b].height,pp.p[b].weight); }; } DLLEXPORT Array_2d_ofperson create_2darr_person(int len,int sublen) { Array_2d_ofperson arr_2d_person; arr_2d_person.len = len; arr_2d_person.subarray = new Arrayofperson[len]; for (int a = 0; a < len; a++) { arr_2d_person.subarray[a].len = sublen; arr_2d_person.subarray[a].p = new person[sublen]; for (int b = 0; b < sublen; b++) { arr_2d_person.subarray[a].p[b].height = b; arr_2d_person.subarray[a].p[b].weight = b; } }; for (int a = 0; a < len; a++) { for (int b = 0; b < sublen; b++) { printf("(a,b): %d,%d",arr_2d_person.subarray[a].p[b].height,arr_2d_person.subarray[a].p[b].weight); printf("\n"); } }; return arr_2d_person; cin.get(); } DLLEXPORT void print_2darr_person(Array_2d_ofperson pp) { int len = pp.len; int sublen = pp.subarray[0].len; //yes I haven't forgotten that it can change between different subarrays. for (int a = 0; a < len; a++) { for (int b = 0; b < sublen; b++) { printf("(a,pp.subarray[a].p[b].height,pp.subarray[a].p[b].weight); printf("\n"); } }; }
我打算从上面的代码中创建一个dll(这里为什么不重要)(稍后会有更多的代码)并在python中使用它.所以这是我的问题:
1)当我在python端执行此操作时似乎:
from ctypes import * test = CDLL('test.dll') //the dll from the code above,yes it works. arr = test.create_arr_person(6) test.print_arr_person(arr) arr2 = test.create_2darr_person(2,3) #test.print_2darr_person(arr2) raw_input('h')
当我尝试打印2d数组时,我得到了用于打印人员阵列的垃圾并从Windows获取访问冲突错误.
所以这是我的问题,按重要性排序(我不想在dll中使用python api,因为dll也可以被其他语言使用)
1)如何使它专用于array / 2darray的内存保留在内存中,这样我就不会出现访问冲突错误.我试过做静态Arrayofperson,但它没有用.
2)如何能够轻松访问二维数组子阵列中的人而不是进行操作.
pp.subarray并[a]的.p [B]. (我想这样做:pp [a] [b],其中pp是2darray of person).我相信它与重载[]运算符有关,但我不熟悉制作类(这就是我现在制作结构的原因).
3)如何使用相同的方式在python中访问array / 2darray(我想在python中执行此操作:
test = CDLL('test.dll') array_of_person = test.create_arr_person(5) print (array_of_person[0]) #something like this
解决方法
这里的问题是python不知道如何处理你的结构.查看ctypes的文档,它有一个可以传递给C函数的受支持的python类型列表,以及如何使它处理更多类型的文档.
你编写它的方式,python认为你的所有函数都返回一个int.
你需要阅读http://docs.python.org/library/ctypes.html
编辑:如果你做得对,你可能最终会从你的C函数返回一个不透明的指向你的结构的指针到python.在你的struct中,你可以使用所有的C特性,包括好东西,比如std :: vector.