Hey guys,

I am writing this code that is basically supposed to implement a 4D array, with a variable 4th Dimension based on the space coordinate of the element. So, for example if you are at coordinate (1,2,3) you might have 4 values, and if you are at (2,3,4) you would have 8 values. If I run the code this way, then I will get an Access violation reading location error. If I am to move the variable declarations outside the class, then the program works. I don't like the idea of those variables to be declared globally as I need to have them as part of the class. Any ideas how I can fix the error?

<code>

#include <cstdlib>
#include <conio.h>
#include <iostream>


/*
Project Name: oneDundistributed.dev
File Name: main.cpp
Copyright:
Author: Alex Blidaru
Date: 05/07/10 15:50
Description:
*/

using namespace std;


class MPIVDOFArray {

public:
double **** data;
int *cDOF;
int Nx, Ny, Nz;

MPIVDOFArray(int Nx2, int Ny2, int Nz2, int *&DOFArr) {
int Nx = Nx2, Ny = Ny2, Nz = Nz2,i,j,k,z;
cDOF = new int[Nx * Ny * Nz + 1];
data = new double ***[Nx];

cDOF[0] = 0;
for (i = 0; i < Nx * Ny * Nz; i++)
cDOF[i + 1] = cDOF[i] + DOFArr[i];
delete [] DOFArr;
DOFArr=NULL;

for (i = 0; i < Nx; i++) {
data[i] = new double **[Ny];
for (j = 0; j < Ny; j++) {
data[i][j] = new double *[Nz];
for (z = 0; z < Nz; z++) {
cout<<*LinearIndex(i,j,z);
data[i][j][z] = new double[*GetDOF(i,j,z)];
for(k=0; k<*GetDOF(i,j,z); k++){
data[i][j][z][k] = rand() % 7 + 1;
cout << "data[i][j][z][k]=" << data[i][j][z][k]<<"\n";
//cout << "DOFArr[LinearIndex(i, j, z)]" <<DOFArr[i + Nx * j + Ny * Nx * z]<<"\n\n\n";
}
}
}
}
}
//
int *LinearIndex(int i, int j, int z) {
int blah1=(i + Nx * j + Ny * Nx * z);
int *blah=&blah1;
return blah;
}

int *GetDOF(int i, int j, int z) {

int ind = cDOF[*LinearIndex(i,j,z)] - cDOF[*LinearIndex(i,j,z)];
int *blah= &ind;
return blah;
}

void print(int Nx, int ny, int Nz) {
for (int i = 0; i < Nx; i++)
for (int j = 0; j < Ny; j++)
for (int z = 0; z < Nx; z++)
for (int k = 0; k < *GetDOF(i, j, k); k++)
cout << data[i][j][z][k] << " ";
}
};

//------------------------------------------------------------------------------
void setDOFArr(int *DOFArr, int Nx, int Ny, int Nz) {
for (int i = 0; i < Nx * Ny * Nz; i++)
DOFArr[i] = rand() % 3 + 1;
}

int main(int argc, char *argv[]) {


int Nx, Ny, Nz;
Nx = Nz = Ny = 3;

int *DOF = new int[Nx * Ny * Nz];
//setDOFArr(DOF, Nx, Ny, Nz);
for (int i = 0; i < Nx * Ny * Nz; i++)
DOF[i] = rand() % 3 + 1;

MPIVDOFArray a(Nx, Ny, Nz, DOF);

delete [] DOF;
DOF=NULL;

a.print(Nx, Ny, Nz);

int c;
cin>>c;
}

</code>

Cheers,
Alex