C++ 动态的动态数组(结构的数组)
我有一个名为C++ 动态的动态数组(结构的数组),c++,python,dll,C++,Python,Dll,我有一个名为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. }; s
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.
};
我还创建了一个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.
};
我对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, b): %d, %d", pp.subarray[a].p[b].height, pp.subarray[a].p[b].weight);
printf("\n");
}
};
}
我在打印person数组时收到垃圾,当我尝试打印2d数组时,从windows收到访问冲突错误
以下是我的问题,按重要性排序(我不想在dll中使用python api,因为dll也可以被其他语言使用)
1) 如何使专用于阵列/2darray的内存保留在内存中,从而不会出现访问冲突错误。我尝试过使用静态的Arrayofperson
,但没有成功
2) 如何使访问2d阵列子阵列中的person
变得容易,而不是执行以下操作。
pp.subarray[a].p[b]
。(我想这样做:pp[a][b]
,其中pp是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
我尝试在Linux机器(GCC4.4.3)上编译您的代码,它可以正常工作
您考虑过使用STL容器(vector)吗?您可以使用vectors of vectors生成多维数组,而不必担心内存泄漏。您可以利用这样一个事实,即保证向量是一个连续的内存块,并返回指向第一个元素的指针
T * p = &v[0]
然后可以将该指针作为普通数组进行访问,并且可以安全地跨越模块边界。
同样的技术也适用于std::字符串,这些字符串可以通过指向存储的原始指针进行访问
const char * p = s.c_str();
在完成之前,您只需确保保存存储的对象不会意外超出范围
多维数组始终可以投影到一维上
11
2 2
三三三
可存储为:
1 1 2 2 3 3这里的问题是python不知道如何处理您的结构。检查文档中的ctypes,它有一个支持的python类型列表,您可以将其传递给C函数,以及关于如何使其处理更多类型的文档 按照您编写它的方式,python认为所有函数都返回int 你需要阅读 编辑:
如果你把事情做对了,你可能会返回一个从C函数到Python的不透明指针,在你的结构中,你可以使用所有C++特性,包括好的东西,比如STD::vector。< /p>你要创建2D数组的人……对吗?我已经知道如何创建数组,但是我不知道如何保存INF。向量在C++中是“活着”的。向量是C++,因此我不能使用Extn’c’导出它们。我确实尝试用向量来表示它,但是我传递给函数的参数总是错误的。例如:我的CurtEyArayAyHyman函数取了int参数,但是当我通过5的时候,我得到了2227068的东西。dll端。