I've gotten rid of the codes that do work as they were distracting. What I'm trying to do is allocate a single block of memory for a 3D array. I could simply just do the allocation as a jagged array, but it's more efficient to do a contiguous block.
Here's my current code snippet:
//Creating an array of proper size
//I first create an array of pointers
phi = new double**[xlength];
//I then create another array of pointers
phi[0] = new double*[xlength*ylength];
//At this point, I assume phi[0] and phi[0][0] have the same location. Is this not true?
phi[0][0] = new double[xlength*ylength*tlength];
//Now, I allocate the big block of data that will actually store my data
//I assume phi[0][0][0] has the same location as phi[0][0]. Is this right?
//Now, I'm trying to setup the pointers to match up in the single block of memory
for (int i=0;i<xlength;i++)
{
for (int j=0;j<ylength;j++)
{
phi[i][j] = phi[0][0] + tlength*ylength*i + tlength*j;
}
}
//Does this not work? It feels as though it SHOULD
Adding back working code since answer depends on working code
double*** A = new double**[m];
double** B = new double*[m*n];
double* C = new double[m*n*o];
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
B[n*i+j] = C + (n*i+j)*o;
}
A[i] = B + n*i;
}
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
for (int k=0; k<o; k++) {
A[i][j][k] = <my_value>;
}
}
Here's the solution guys!
Don't do 3D matrix, LOL! This is my most unpopular post ever!
Do a 1D matrix and follow the answer on this link C++ Segmentation Fault After When Trying to Write to Matrix
This is the important part of that link:
but then you cannot use the phi[i][j][k] notation, so you should
#define inphi(I,J,K) phi[(I)*xlength*ylength+(J)*xlength+(K)]
and write inphi(i,j,k) instead of phi[i][j][k]