Dynamic allocation of multi dimensional array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gianluca

    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(ndimen sion*(sizeof(do uble));

    but the program send me a run time error.

    could anybody help me?
    thanks
    gianluca
  • Ben Bacarisse

    #2
    Re: Dynamic allocation of multi dimensional array

    gianluca <geonomica@gmai l.comwrites:
    I've to declare a 3D matrix in C . I tried with that code:
    >
    double ***mat;
    >
    mat=(double***) G_malloc(ndimen sion*(sizeof(do uble));
    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.

    Comment

    • Bartc

      #3
      Re: Dynamic allocation of multi dimensional array


      "gianluca" <geonomica@gmai l.comwrote in message
      news:592333a7-1188-4096-a1a4-a3736079a99e@8g 2000hse.googleg roups.com...
      Hy list,
      >
      I've to declare a 3D matrix in C . I tried with that code:
      >
      double ***mat;
      >
      mat=(double***) G_malloc(ndimen sion*(sizeof(do uble));
      >
      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(size of(double*)*xdi m);

      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

      Comment

      • Malcolm McLean

        #4
        Re: Dynamic allocation of multi dimensional array


        "gianluca" <geonomica@gmai l.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(ndimen sion*(sizeof(do uble));
        >
        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.


        Comment

        • vippstar@gmail.com

          #5
          Re: Dynamic allocation of multi dimensional array

          On Aug 9, 5:17 pm, "Malcolm McLean" <regniz...@btin ternet.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.

          Comment

          • Gordon Burditt

            #6
            Re: Dynamic allocation of multi dimensional array

            >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.

            Comment

            • Barry Schwarz

              #7
              Re: Dynamic allocation of multi dimensional array

              On Sat, 09 Aug 2008 13:44:49 GMT, "Bartc" <bc@freeuk.comw rote:
              >#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(siz eof(double*)*xd im);
              This need not allocate the correct amount of space. The object(s) mat
              points to must be double**.


              --
              Remove del for email

              Comment

              • Barry Schwarz

                #8
                Re: Dynamic allocation of multi dimensional array

                On Sat, 9 Aug 2008 06:27:44 -0700 (PDT), gianluca
                <geonomica@gmai l.comwrote:
                >Hy list,
                >
                >I've to declare a 3D matrix in C . I tried with that code:
                >
                >double ***mat;
                >
                >mat=(double*** )G_malloc(ndime nsion*(sizeof(d ouble));
                >
                >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

                Comment

                • Barry Schwarz

                  #9
                  Re: Dynamic allocation of multi dimensional array

                  On Sat, 9 Aug 2008 08:18:29 -0700 (PDT), vippstar@gmail. com wrote:
                  >On Aug 9, 5:17 pm, "Malcolm McLean" <regniz...@btin ternet.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

                  Comment

                  • Ian Collins

                    #10
                    Re: Dynamic allocation of multi dimensional array

                    Barry Schwarz wrote:
                    On Sat, 9 Aug 2008 08:18:29 -0700 (PDT), vippstar@gmail. com wrote:
                    >
                    >On Aug 9, 5:17 pm, "Malcolm McLean" <regniz...@btin ternet.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.

                    Comment

                    • pete

                      #11
                      Re: Dynamic allocation of multi dimensional array

                      Barry Schwarz wrote:
                      On Sat, 09 Aug 2008 13:44:49 GMT, "Bartc" <bc@freeuk.comw rote:
                      >
                      >#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(siz eof(double*)*xd im);
                      >
                      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

                      Comment

                      • vippstar@gmail.com

                        #12
                        Re: Dynamic allocation of multi dimensional array

                        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

                        Comment

                        Working...