473,326 Members | 2,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Dynamic allocation of multi dimensional array

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
Aug 9 '08 #1
11 3099
gianluca <ge*******@gmail.comwrites:
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.

--
Ben.
Aug 9 '08 #2

"gianluca" <ge*******@gmail.comwrote in message
news:59**********************************@8g2000hs e.googlegroups.com...
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[i]=malloc(sizeof(double*)*ydim);
for (j=0; j<ydim; ++j)
mat[i][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[i][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[i][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.

--
Bartc

Aug 9 '08 #3

"gianluca" <ge*******@gmail.comwrote in message news:
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?
The first thing is to realise that multi-dimensional arrays in C are an
advanced feature. There are all sorts of subtle difficulties with them.

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.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Aug 9 '08 #4
On Aug 9, 5:17 pm, "Malcolm McLean" <regniz...@btinternet.comwrote:
<snip>
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.
Aug 9 '08 #5
>The first thing is to realise that multi-dimensional arrays in C are an
>advanced feature. There are all sorts of subtle difficulties with them.
There are a number of ways you can declare a so-called multi-dimensional
array and address it like x[j][k][m] to get to an element of them,
but they are NOT compatible with each other and if you pass one to
a function expecting another kind, you're going to get a disaster.
>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.
This is NOT compatible with a declaration like double x[5][6][8]; if
you have to allocate space for pointers. However, it does have the
advantage that you don't have to know the exact dimensions as long
as you don't go out-of-bounds.
>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.

Aug 9 '08 #6
On Sat, 09 Aug 2008 13:44:49 GMT, "Bartc" <bc@freeuk.comwrote:
>#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**.
--
Remove del for email
Aug 9 '08 #7
On Sat, 9 Aug 2008 06:27:44 -0700 (PDT), gianluca
<ge*******@gmail.comwrote:
>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.

--
Remove del for email
Aug 9 '08 #8
On Sat, 9 Aug 2008 08:18:29 -0700 (PDT), vi******@gmail.com wrote:
>On Aug 9, 5:17 pm, "Malcolm McLean" <regniz...@btinternet.comwrote:
<snip>
>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.
You know of a C library that makes dynamic arrays easier?

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

--
Remove del for email
Aug 9 '08 #9
Barry Schwarz wrote:
On Sat, 9 Aug 2008 08:18:29 -0700 (PDT), vi******@gmail.com wrote:
>On Aug 9, 5:17 pm, "Malcolm McLean" <regniz...@btinternet.comwrote:
<snip>
>>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.

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.

--
Ian Collins.
Aug 9 '08 #10
Barry Schwarz wrote:
On Sat, 09 Aug 2008 13:44:49 GMT, "Bartc" <bc@freeuk.comwrote:
>#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**.
A good allocation idiom:

mat = malloc(xdim * sizeof *mat);

mat[i] = malloc(ydim * sizeof *mat[i]);

mat[i][j] = malloc(zdim * sizeof *mat[i][j]);
--
pete
Aug 10 '08 #11
On Aug 10, 1:17 am, Barry Schwarz <schwa...@dqel.comwrote:
On Sat, 9 Aug 2008 08:18:29 -0700 (PDT), vipps...@gmail.com wrote:
<snip>
It's best to either use some lib or some other programming language.

You know of a C library that makes dynamic arrays easier?
No
You know of a language that makes allocating dynamic arrays easier?
Yes
Aug 10 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: sugaray | last post by:
I don't get why the correct way to allocate memory space for 3d-array should be like this in C++: long (*ptr); ptr=new long; and when deallocation delete ptr;
11
by: fivelitermustang | last post by:
Actually, how would I go about allocating a four-dimensional dynamic array? I only know how to make two dimensional dynamic arrays: double **v; v = new double*; for (int i=0; i<C; i++) { v =...
6
by: Gregory L. Hansen | last post by:
I'm sure I saw this mentioned somewhere, but I can't find it. How can I dynamically allocate a multi-dimensional array in C++? My next question, if this gets figured out, is if I should use the...
2
by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =...
1
by: Roberto Dias | last post by:
I'm a newbie in C++ programming. I bought a book yet and I have learned by means internet donwloadble materials. I feel not confortable using multi-dimensional arrays. I simply cannot understand...
4
by: Robert P. | last post by:
I can easily store a one-dimensional array in viewstate ( see Test1 ) If I try storing a multi-dimensional array in the viewstate it's crapping out on me when it goes to serialize the array (not...
60
by: Peter Olcott | last post by:
I need to know how to get the solution mentioned below to work. The solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below: >...
9
by: dennis.sam | last post by:
Hi, Is there away to define a multi-dimensional array with respect to the number of dimensions the array has? For example, given a user spec of "a b c d", I want to create a 4 dimensional array...
7
by: nw | last post by:
Hi, We've been having a discussion at work and I'm wondering if anyone here would care to offer an opinion or alternative solution. Aparently in the C programming HPC community it is common to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.