从 C++ 中的函数发送和 Return 二维动态结构

Send and Return 2D dynamic struct from a function in C++

我想从 C++ 中的函数发送和检索二维动态结构。我已经在一些网站上搜索过,但没有人可以使用。有人可以帮助我吗?

这是我的功能:

122 void initializePopulation(Body* bodies, int nBodies, Body** kromosom_awal, int nKromosom)
123 {
124    time_t t;
125    time(&t);
126    typedef boost::mt19937 RNGType;
127    RNGType rng((unsigned int) t);
128    boost::uniform_int<> awal_dan_akhir(1, 10);
129    boost::variate_generator< RNGType, boost::uniform_int<> >
130                     acak(rng, awal_dan_akhir);
131    cout << "fungsi inisialisasi" << endl;
132    for(int i=0; i<nKromosom; i++)
133    {
134       for(int j=0; j<nBodies; j++)
135       {
136          cout << "nBodies: " << nBodies << " " << i << j << " " << kromosom_awal[i][j].x << " " << bodies[i].x << endl;
137          kromosom_awal[i][j].x = bodies[j].x;
138          kromosom_awal[i][j].y = bodies[j].y;
139          kromosom_awal[i][j].z = bodies[j].z;
140          kromosom_awal[i][j].dx = bodies[j].dx;
141          kromosom_awal[i][j].dy = bodies[j].dy;
142          kromosom_awal[i][j].dz = bodies[j].dz;
143          kromosom_awal[i][j].rho = (acak()/(double) 10);
144       }
145       cout << "nKromosom : " << i << " " << nKromosom << endl;
146    }
147 }

我是这样调用该函数的:

160 void goGA(XYZ* stasiun, int nStasiun, Body* bodies, int nBodies)
161 {
162    //initialize population
163    int nKromosom = 10;
164    Body *kromosom_awal[nKromosom];
165    initializePopulation(bodies, nBodies, kromosom_awal, nKromosom);
166 }

但它给我一个错误:

Segmentation fault: 11

我的错误在哪里?

哦,顺便说一句,这是我的结构声明:

 17 typedef struct Body
 18 {
 19    double x;
 20    double y;
 21    double z;
 22    double dx;
 23    double dy;
 24    double dz;
 25    double rho;
 26 }Body;
 27 
 28 typedef struct Response
 29 {
 30    double x;
 31    double y;
 32    double z;
 33    double grav;
 34 }Response;
 35 
 36 typedef struct gen
 37 {
 38    double x;
 39    double y;
 40    double z;
 41    double rho;
 42 }gen;
 43 
 44 typedef struct XYZ
 45 {
 46    double x;
 47    double y;
 48    double z;
 49 }XYZ;

内存分配有问题。

1)Body *kromosom_awal[nKromosom];为nKromosom指针分配内存,但是数据的分配内存在哪里?

2) Body* bodies 的内存是在 goGA 函数之外分配的吗?