-1

I need to clear memory of 5D object, so first I need to define it with memory allocation. My previous object definition was:

   double I_object[N_GROUPS][NN][NN][NN][NN]={0};

I replaced it by code below and c file compiles but c program crashes :(

 I_object=(double *****) malloc(sizeof(double *****)*N_GROUPS);
  for(i = 0; i < N_GROUPS; i++){
      I_object[i]=(double ****) malloc(sizeof(double****)*NN);
      for(j = 0; j < NN; j++){
         I_object[i][j]=(double ***) malloc(sizeof(double***)*NN); 
         for(k = 0; k < NN; k++){
            I_object[i][j][k]=(double **) malloc(sizeof(double**)*NN); 
            for(l = 0; l < NN; l++){
                I_object[i][j][k][l]=(double *) malloc(sizeof(double*)*NN); 
            }
         }
      }
  }

Please let me know if this 5D object definition is wrong or where I can find how to define 5D object using malloc.

Blaze
  • 16,736
  • 2
  • 25
  • 44
Ania
  • 11
  • 8
    This is a first time in my life I am seeing a 5-star program. – Eugene Sh. Apr 03 '19 at 14:02
  • 2
    You have one extra star in each `sizeof`. – Eugene Sh. Apr 03 '19 at 14:04
  • 2
    Possible duplicate of [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays), which also addresses how to get rid of multiple levels of indirection. – Lundin Apr 03 '19 at 14:07
  • What's your value of `NN`? If this program compiles but crashes, you might be running out of memory. For a 5D array like this, the memory usage will be enormous unless `NN` is reasonably small. – Graeme Cole Apr 03 '19 at 14:09
  • Possible duplicate of [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays) – dandan78 Apr 03 '19 at 14:14
  • [Obligatoy *** joke](http://wiki.c2.com/?ThreeStarProgrammer). – HolyBlackCat Apr 03 '19 at 14:18

2 Answers2

3

You forgot to declare the type of I_object. Also, you need to declare i, j, k and l. Also, I wouldn't cast the result of malloc in C. Furthermore, the indirection levels are wrong in your sizeof() calls, which causes a problem in the last one, where there might be a difference between the sizes of a double and a double*.

With those changes, it looks like this:

#define N_GROUPS 3
#define NN 3
int main() {
    double *****I_object = malloc(sizeof(double ****)*N_GROUPS);
    for (size_t i = 0; i < N_GROUPS; i++) {
        I_object[i] = malloc(sizeof(double***)*NN);
        for (size_t j = 0; j < NN; j++) {
            I_object[i][j] = malloc(sizeof(double**)*NN);
            for (size_t k = 0; k < NN; k++) {
                I_object[i][j][k] = malloc(sizeof(double*)*NN);
                for (size_t l = 0; l < NN; l++) {
                    I_object[i][j][k][l] = malloc(sizeof(double)*NN);
                }
            }
        }
    }
}

Also, for future questions, I would recommend posting a Minimal, Complete, and Verifiable example. This way, people are much more willing (and capable) to help. For instance, I had to make assumptions about N_GROUPS and NN. Even with low values such as 100 for NN, the memory consumption would already be so high that it will likely cause problems, but without a MCVE, it's hard to tell.

Blaze
  • 16,736
  • 2
  • 25
  • 44
1

Instead of having array of some pointer to other arrays of some pointers to other arrays of .... simply use a pointer to array[nn][nn][nn][nn] of doubles.

Something like:

#define NN 2
#define N_GROUPS 4

int main(int argc, char* argv[])
{
  double (*I_object)[NN][NN][NN][NN];              // Define pointer
  I_object = calloc(N_GROUPS, sizeof * I_object);  // Allocate and zero-init memory
  I_object[3][1][1][1][1] = 42;                    // Use as 5D matrix
  printf("%f\n", I_object[3][1][1][1][1]);

  free(I_object);                                  // Free memory
  return 0;
}
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63