I have a struct named person as follows:
struct person {
int height, weight;
};
I also created an array of person as follows:
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.
};
And I do this for an array of array of person as follows:
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.
};
This is my code:
#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");
}
};
}
I intend to make a dll(the why is not important here) from the above code(it will have more code later on) and use it in python. So here are my questions:
1) It seems that when I do this on the python side:
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')
I get garbage for printing the array of person and get an access violation error from windows when I try to print the 2d array.
So here are my questions, in order of importance(I don't want to use python api within the dll, because the dll could also be used by other languages)
1) How do I make it so that the memory dedicated to the array/ 2darray stays in memory so that I don't get access violation errors. I've tried doing static Arrayofperson, but it didn't work.
2) How is possible to make it easy to access person in the subarray of the 2d array instead of doing.
pp.subarray[a].p[b]. (I want to do this: pp[a][b], where pp is 2darray of person). I believe it has something to do with overloading the [ ] operator, but I'm not familiar with making classes(thats why i made a struct now).
3) How do I access the array/2darray in python using the same way (I want to do this in python:
test = CDLL('test.dll')
array_of_person = test.create_arr_person(5)
print (array_of_person[0]) #something like this