-2

I have written a code for multidimensional very large arrays. It compiles but when I execute it, it gives me segmentation error. My code is:

int NT = 35; int NX = 25; int NY = 25; int NZ = 25;
double dx = 0.5; double dy = 0.5; double dz = 0.5; double dt = 0.1;
double PosT[NT];
double PosX[NX];
double PosY[NY];
double PosZ[NZ];
for (int i = 0; i < NT; i++)
    PosT[i] = i * dt + dt;
for (int i = 0; i < NX; i++)
    PosX[i] = dx * i;
for (int i = 0; i < NY; i++)
    PosY[i] = dy * i;
for (int i = 0; i < NZ; i++)
    PosZ[i] = dz * i;
double* b_x = (double*)malloc(NX*NY*NZ * sizeof(double));
double* b_y = (double*)malloc(NX*NY*NZ * sizeof(double));
if (b_x == NULL || b_y == NULL) {
    cout << "Malloc space error!" << endl;
    return 0;
}
for (int ix = 0; ix < NX; ix++) {
    for (int iy = 0; iy < NY; iy++) {
        for (int iz = 0; iz < NZ; iz++) {
            int position = ix * NY*NZ + iy * NZ + iz;
            b_x[position] = 0.;
            b_y[position] = 0.;
        }
    }
}

My code works till here but when I use following very large 2d array by malloc I get error. Ny further code follows following lines:

double** B=(double**)malloc(NX*NY*NZ*LT*sizeof(double*));
if (B == NULL) {
    cout << "Malloc space error!" << endl;
    return 0;
}
cout << "following not works" << endl;
for (int ix = 0; ix < NX; ix++) {
    for (int iy = 0; iy < NY; iy++) {
        for (int iz = 0; iz < NZ; iz++) {
            int position = ix * NY*NZ + iy * NZ + iz;
            for (int it = 0; it < NT; it++) {
                B[position][it] = 0.;
                cout << B[position][it] << endl;
            }
        }
    }
}

I have tried malloc but it did not works. Any solutions or suggestion. what are alternative way to use very large such arrays? because I am getting segmentation fault core dumped error.

Swordfish
  • 12,971
  • 3
  • 21
  • 43
IrfanS
  • 57
  • 1
  • 3
  • 1
    `B[position]` is an uninitialized `double*` pointer, pointing to some random address in memory. `B[position][it]` attempts to access that memory, whereupon your program exhibits undefined behavior. – Igor Tandetnik Nov 28 '18 at 05:19
  • 1
    Cure: `std::vector foo(NX * NY * NZ);` & `std::vector bar(NX * NY * NZ * LT * NT);` – Swordfish Nov 28 '18 at 05:26
  • C++ ? `malloc()` ? – John3136 Nov 28 '18 at 05:27
  • & Please don't put `'` around your code, just indent it 4 spaces. – Swordfish Nov 28 '18 at 05:32
  • *It compiles* -- It shouldn't compile. `double PosT[NT];` This is not valid C++, as C++ requires that array sizes be denoted by a compile-time expression, not a runtime value. Use `std::vector`, as suggested previously. – PaulMcKenzie Nov 28 '18 at 05:37
  • Related: https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new But a vector of vector is better ... – Damien Nov 28 '18 at 09:01

1 Answers1

0

I'm hesitant to answer this but the following looks incorrect: double** B=(double**)malloc(NX*NY*NZ*LT*sizeof(double*)); and should be: double** B=malloc(NX*NY*NZ*NT*sizeof(double));

It looks like you only allocated maybe half the size of what you needed.

fdk1342
  • 3,274
  • 1
  • 16
  • 17