C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 10th, 2013, 07:04 PM
plikokv plikokv is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 plikokv User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 6 m 7 sec
Reputation 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!

Reply With Quote
  #2  
Old January 11th, 2013, 01:28 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 25 m 12 sec
Reputation Power: 1774
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

Reply With Quote
  #3  
Old January 11th, 2013, 02:17 PM
plikokv plikokv is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 plikokv User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 6 m 7 sec
Reputation 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?

Reply With Quote
  #4  
Old January 11th, 2013, 02:40 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 25 m 12 sec
Reputation Power: 1774
Yes, the memory where data is actually stored is contiguous.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Allocating 4D Array in C

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap