#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Allocating 4D Array in C


    Howdy,

    I'm trying to write out a function within a project to create and allocate memory for a 4-dimensional array of doubles. I've tried many options and so far the best I've gotten was segmentation fault. My project is due this Sunday, so I'm somewhat desperate to figure this out :) Here goes the code:

    Code:
    double ****
    super_malloc(long int n1, long int n2, long int n3, long int n4)
    {
      double ****vect;
      double *temp1, **temp2, ***temp3;
      int i1;
    
      if(!(vect = malloc(n1*sizeof(double ***))))
        {
          perror("Error 1");
          return NULL;
        }
    
      if(!(temp3 = malloc(n1*n2*sizeof(double **))))
        {
          free(vect);
          vect = NULL;
    
          perror("Error 2");
          return NULL;
        }
    
      if(!(temp2 = malloc(n1*n2*n3*sizeof(double *))))
        {
          free(vect);
          vect = NULL;
          free(temp3);
          temp3 = NULL;
    
          perror("Error 3");
          return NULL;
        }
    
      if(!(temp1 = malloc(n1*n2*n3*n4*sizeof(double))))
        {
          free(vect);
          vect = NULL;
          free(temp3);
          temp3 = NULL;
          free(temp2);
          temp2 = NULL;
    
          perror("Error 4");
          return NULL;
        }
    
      for(i1 = 0; i1 < n3; ++i1)
        temp2[i1] = temp1 + i1*n4*sizeof(double);
    
      for(i1 = 0; i1 < n2; ++i1)
        temp3[i1] = temp2 + i1*n3*sizeof(double *);
    
      for(i1 = 0; i1 < n1; ++i1)
        vect[i1] = temp3 + i1*n2*sizeof(double **);
    
      return vect;
    }
    This function is used in another function which does something along the lines of:

    Code:
    double ****vect;
    
    ...
    
    vect = super_malloc(n1, n2, n3, n4);

    Thanks in advance!
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    You seemed to be on the right track, but your pointer arithmetic at the end seems off.

    This is how I usually do this.
    Note how all the 'temp' pointer adjustments don't involve any sizeof's, as this is automatically taken care of by the compiler.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int ****alloc ( int maxx, int maxy, int maxr, int maxc ) {
      int *rows = malloc(maxx*maxy*maxr*maxc*sizeof(*rows));
      int **cols = malloc(maxx*maxy*maxr*sizeof(*cols));
      int ***mat = malloc(maxx*maxy*sizeof(*mat));
      int ****result = malloc(maxx*sizeof(*result));
      for ( int x = 0 ; x < maxx ; x++ ) {
        result[x] = mat; 
        mat += maxy;
        for ( int y = 0 ; y < maxy ; y++ ) {
          result[x][y] = cols ; 
          cols += maxr;
          for ( int r = 0 ; r < maxr ; r++ ) {
            result[x][y][r] = rows;
            rows += maxc;
          }
        }
      }
      return result;
    }
    
    int main(void)
    {
      int rows = 5, cols = 10, width = 15, height = 20;
      int ****matgrid = alloc(width,height,rows,cols);
      for ( int x = 0 ; x < width ; x++ ) {
        for ( int y = 0 ; y < height ; y++ ) {
          for ( int r = 0 ; r < rows ; r++ ) {
            for ( int c = 0 ; c < cols ; c++ ) {
              matgrid[x][y][r][c] = 0;
            }
          }
        }
      }
    
      // should be another function
      free(matgrid[0][0][0]);
      free(matgrid[0][0]);
      free(matgrid[0]);
      free(matgrid);
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0
    Many thanks!

    I see where my pointer arithmetic failed! :) So I'm guessing your answer is exactly what I was looking to achieve: a contiguous block of allocated memory, is that right?
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    Yes, the memory where data is actually stored is contiguous.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo