The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Allocating 4D Array in C
Discuss Allocating 4D Array in C in the C Programming forum on Dev Shed. Allocating 4D Array in C C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 10th, 2013, 07:04 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 2
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!
|

January 11th, 2013, 01:28 AM
|
 |
Contributed User
|
|
|
|
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;
}
|

January 11th, 2013, 02:17 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 2
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?
|

January 11th, 2013, 02:40 PM
|
 |
Contributed User
|
|
|
|
|
Yes, the memory where data is actually stored is contiguous.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|