3d array c/c++ initialization/conversion
float sampleGrid1[5][5][5] =
{
{
{0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0}
},
{
{0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 1.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0}
},
{
{0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 1.0, 0.0, 0.0},
{0.0, 1.0, 1.0, 1.0, 0.0},
{0.0, 0.0, 1.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0}
},
{
{0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 1.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0}
},
{
{0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0},
{0.0, 0.0, 0.0, 0.0, 0.0}
}
};
typedef struct
{
int Nx;
int Ny;
int Nz;
float*** M;
}OG;
Relevant function:
OG *newOG(){
OG *newOG = (OG *)malloc(sizeof(OG));
if (newOG == NULL)
{
throw std::exception("newOG : no memory is available");
}
return newOG;
}
int initiateOG(OG *MyOG)
{
ifstream dump("OGdump3.txt");
if (dump.is_open())
{
while ( dump.good() )
{
dump >> MyOG->Nx;
dump >> MyOG->Ny;
dump >> MyOG->Nz;
MyOG->M = new float**[MyOG->Nx];
for(int i = 0; i < MyOG->Nx; i++)
{
MyOG->M[i] = new float*[MyOG->Ny];
for(int j = 0; j < MyOG->Ny; j++)
{
MyOG->M[i][j] = new float[MyOG->Nz];
}
}
for(int z=0;z < MyOG->Nz; z++){
for(int y=0;y < MyOG->Ny; y++){
for(int x=0;x < MyOG->Nx; x++){
dump >> MyOG->M[x][y][z];
}
}
}
}
dump.close();
}
else return 0;
return 1;
}
I want to hard code some sample grids into the code, but don't know the best way to create them, do i have to use for loops?
i don't want to change my typedef struct OG, if possible
Modified:
OG *occupancyGrid;
void initialize3dArray(int x, int y, int z,float*** array)
{
array = new float**[x];
for(int i = 0; i < x; i++)
{
array[i] = new float*[y];
for(int j = 0; j < y; j++)
{
array[i][j] = new float[z];
}
}
}
void sampleOG1()
{
occupancyGrid = newOG();
occupancyGrid->Nx = 5;
occupancyGrid->Ny = 5;
occupancyGrid->Nz = 5;
initialize3dArray(5, 5, 5,occupancyGrid->M);
for(int z=0;z < occupancyGrid->Nz; z++){
for(int y=0;y < occupancyGrid->Ny; y++){
for(i开发者_StackOverflownt x=0;x < occupancyGrid->Nx; x++){
occupancyGrid->M[x][y][z] = sampleGrid1[x][y][z];
}
}
}
}
initialize3dArray this function doesn't have compiling error, but still causing the program to crash
Yes. that will not compile, because float[5][5][5]
and float ***
aren't same type. They're not even compatible type. One cannot convert to other automatically.
However, float[5][5][5]
can convert to float (*)[5][5]
automatically. So this is legal code:
float (*m)[5][5];
m = sampleGrid1; //legal - allowed!
Demo : http://ideone.com/RwAwI
So define OG
as,
struct OG
{
int Nx;
int Ny;
int Nz;
float (*M)[5][5];
};
If you OG
as defined above, then you can write this:
OG* temp = newOG();
temp->Nx = 5;
temp->Ny = 5;
temp->Nz = 5;
occupancyGrid->M = sampleGrid1; //DONT use &
Isn't the error clear? float[5][5][5]
is not as related to float***
as you think it is.
Use a std::vector<std::vector<std::vector<float> > >
instead and avoid the whole mess.
float x[2][2]
is a 2D array of float - it's not an 1D array of pointers to float. The conversion from array to pointer only works for the first dimension of such arrays.
Given float x[2][2]
you reserve space for 4 floats. A float **
variable on the other hand is a pointer to a pointer to a float - there's no pointers anywhere in float x[2][2]
The same of course holds true for a 3D array - your 3D array has no sneakily hidden pointers inside it, it cannot be treated as a float ***
精彩评论