Dynamic allocation of multi dimensional array

G

gianluca

Hy list,

I've to declare a 3D matrix in C . I tried with that code:

double ***mat;

mat=(double***)G_malloc(ndimension*(sizeof(double));

but the program send me a run time error.

could anybody help me?
thanks
gianluca
 
B

Ben Bacarisse

gianluca said:
I've to declare a 3D matrix in C . I tried with that code:

double ***mat;

mat=(double***)G_malloc(ndimension*(sizeof(double));

The cast is either hiding a type error (if G_malloc does not return the
right sort of pointer) or it is pointless. Much better to leave it out.
but the program send me a run time error.

could anybody help me?

Not without seeing the program, or a shorter version that just shows
the error you are getting.
 
B

Bartc

gianluca said:
Hy list,

I've to declare a 3D matrix in C . I tried with that code:

double ***mat;

mat=(double***)G_malloc(ndimension*(sizeof(double));

but the program send me a run time error.

could anybody help me?

I tried this:

#include "stdio.h"
#include "stdlib.h"

#define xdim 5
#define ydim 10
#define zdim 15

int main(void) {
double ***mat;
int i,j,k,n;

mat=malloc(sizeof(double*)*xdim);

for (i=0; i<xdim; ++i) {
mat=malloc(sizeof(double*)*ydim);
for (j=0; j<ydim; ++j)
mat[j]=malloc(sizeof(double)*zdim);
}

n=0;
for (i=0; i<xdim; ++i)
for (j=0; j<ydim; ++j)
for (k=0; k<zdim; ++k)
mat[j][k] = ++n;

for (i=0; i<xdim; ++i)
for (j=0; j<ydim; ++j)
for (k=0; k<zdim; ++k)
printf("mat[%d][%d][%d] = %f\n",i,j,k,mat[j][k]);

}

In practice, malloc returns should be checked.

C's multi-dimensional dynamic arrays I think need to be allocated as Iliffe
vectors. But once
done then they are indexed as normal.
 
V

vippstar

One way of creating a multi-dimensional array is to allocate a double ***for
the z axis, with as many entries as you have in the y axis, then allocate
double **s for the yaxes, then double *s for the x axes.

The other way is to create a single dimensional array of xsize * ysize *
zsize * sizeof(double). Then you have to do the index calcuations by hand.

Personally I tend to find the second way easier. However neither is ideal.

There's pros and cons with both. For example, in the second, if you
have columns/rows, and you want to change the rows from 10 to 5, you
can't simply realloc. (whereas with the first you just loop over the
rows and realloc)
It's best to either use some lib or some other programming language.
 
B

Barry Schwarz

#include "stdio.h"
#include "stdlib.h"

#define xdim 5
#define ydim 10
#define zdim 15

int main(void) {
double ***mat;
int i,j,k,n;

mat=malloc(sizeof(double*)*xdim);

This need not allocate the correct amount of space. The object(s) mat
points to must be double**.
 
B

Barry Schwarz

Hy list,

I've to declare a 3D matrix in C . I tried with that code:

double ***mat;

mat=(double***)G_malloc(ndimension*(sizeof(double));

but the program send me a run time error.

could anybody help me?

See question 6.16 of the C faq at www.c-faq.com.
 
B

Barry Schwarz

There's pros and cons with both. For example, in the second, if you
have columns/rows, and you want to change the rows from 10 to 5, you
can't simply realloc. (whereas with the first you just loop over the
rows and realloc)
It's best to either use some lib or some other programming language.

You know of a C library that makes dynamic arrays easier?

You know of a language that makes allocating dynamic arrays easier?
 
I

Ian Collins

Barry said:
You know of a language that makes allocating dynamic arrays easier?
In C++ it would be easier (both in construction and adding/removing
arrays) with vectors of vectors.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top