I have the following code to dynamically create 2D arrays. For my application, I need to have the data stored contiguously.
I would like to generalize the 2D code below to 3D arrays. How can I do this?
double *psiMemLoc;
double **psi;
psiMemLoc = malloc(sizeof(double)*(lasty-firsty+3)*(lastx-firstx+3));
psi = malloc(sizeof(double *) * (lasty-firsty+3));
for(j=firsty-1; j<=lasty+1; j++){
psi[j] = psiMemLoc + (lastx-firstx+3)*j;
}
With the above code, I can access elements of psi like psi[j][i]. To reiterate my goal, I would like to generate a 3D array that I can access using psi[k][j][i].
To give some context, I am solving a 2D PDE and the third dimension will be used to store a few of the previous timestep solutions. So the number of memory locations between timesteps should be (lasty-firsty+3)*(lastx-firstx+3).
Thank you!