簡體   English   中英

動態動態數組(struct數組)

[英]Dynamic array of dynamic (array of struct)

我有一個名為person的結構如下:

struct person {
    int height, weight;
};

我還創建數組person如下:

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.
};       

而我這樣做對的數組的數組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");
        }
    }; 
}

我打算從上面的代碼中創建一個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)如何能夠輕松訪問二維數組子陣列中的person而不是進行操作。 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。

我試圖在Linux機器上編譯你的代碼(gcc 4.4.3)並且它有效。

您是否考慮過使用STL容器(矢量)? 您可以使用向量向量生成多維數組,而不必擔心內存泄漏。

您可以使用這樣的事實:向量保證是連續的內存塊並返回指向第一個元素的指針。

T * p = &v[0]

然后可以將該指針作為普通數組進行訪問,並且跨模塊邊界是安全的。 相同的技術也適用於可以通過指向存儲的原始指針訪問的std :: strings。

const char * p = s.c_str();

您必須確保在完成之前保存存儲的對象不會意外地超出范圍。

多維數組總是可以投影到一個維度上。

1 1 1

2 2 2

3 3 3

可以存儲為:

1 1 1 2 2 2 3 3 3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM