Re: Multi-dimensional array allocation with single 'new' call

From: Scott J. McCaughrin (sjmccaug_at_bluestem.prairienet.org)
Date: 10/29/04


Date: Fri, 29 Oct 2004 21:43:19 +0000 (UTC)

ip4ram@yahoo.com wrote:
: I used to work with C and have a set of libraries which allocate
: multi-dimensional arrays(2 and 3) with single malloc call.Now that I
: am moving to C++,am looking for something by which I can allocate a
: multi-dimensional array with one (and only one) 'new' call.Has anybody
: done it ?Thanks,in advance, for all your help.

: Ram

Yes, Ram. The program below does what you ask in C with malloc().

 /* This short program demonstrates that, with a single call to malloc(),
  * you can allocate all the memory required for an MxNxP matrix: this is
  * useful when you want to make just one malloc() call to ensure that
  * you can get enough memory from the start.
  * One drawback of this approach is when malloc() may not have a large
  * enough block available.
  */

#include <stdio.h>

 /* read in dimensions M,N,P of matrix mat[M][N][P] dynamically
  * allocated from heap, so mat -> vector of M row-ptrs, where
  * for i = 0...M-1: the i-th row-ptr -> row of N column-ptrs,
  * for j = 0...N-1: the j-th column-ptr -> vector of P double's;
  *
  * we will need to allocate a number of bytes Q =
  * M*sizeof(double**) + M*N*sizeof(double**) + M*N*P*sizeof(double) =
  * M*(sizeof(double**) + N*(sizeof(double**) + P*sizeof(double))).
  */

 double** *mat_malloc(int);

 int main()
 {
   double** *mat;
   double* *t;
   double* v;

   int M, N, P, Q, i, j, k, MN, MNP;

   printf("input 3 dimensions: ");
   scanf("%d %d %d", &M, &N, &P);
   printf("\nyou want a %d X %d X %d matrix of doubles:\n", M,N,P);

   Q = M*(sizeof(double**) + N*(sizeof(double**) + P*sizeof(double)));
   MN = M*N; MNP = MN*P;

   printf("...allocating %d bytes to hold %d doubles, each %d bytes\n",
          Q, MNP, sizeof(double));

   mat = mat_malloc(Q); /* return means successful allocation */

   /* assign the first M (double**) row-ptrs */
   for (i=0, t=(double**)&mat[M]; i<M; i++, t+=N) /* sets t -> i-th row */
       mat[i] = t; /* assigns i-th row-ptr */

   /* now, t -> start of data-area, where doubles can be stored */

   /* assign to each of the M rows N ptrs to P-extents of doubles */
   for (i=0, v=(double*)t; i<MN; i++, v+=P)
       mat[M+i] = (double**)v;

   /* ready to populate P-extents with doubles */
   for (i=0, v=(double*)t; i<MNP; i++, v++)
       *v = (double)i;

   /* now to test if we placed everything correctly */
   for (i=0; i<M; i++)
       for (j=0; j<N; j++)
           for (k=0; k<P; k++)
               printf("mat[%d][%d][%d] = %lf\n", i,j,k, mat[i][j][k]);

   return 0;
 }

 double** *mat_malloc(int size)
 {
    double** *s = (double***)malloc(size);

    if (s == NULL)
    {
       fprintf(stderr, "unable to malloc %d bytes -- quitting.\n", size);
       exit(-1);
    }
    /* else, s -> block of required size */
    return s;
 }



Relevant Pages

  • Re: xmalloc string functions
    ... Failed malloc() is. ... Why can't you test an allocation failure? ... each can fail on request, each needs to have the request failure dealt ... process Save click because it failed to allocate memory for the event ...
    (comp.lang.c)
  • Re: style question,itoa
    ... able to allocate buffers in some convenient location the callee ... dynamically allocate a block of memory and computing this size to use ... call might require allocating a page for the stack. ... " Checking every single malloc in a bigger application for possible ...
    (comp.unix.programmer)
  • Re: xmalloc string functions
    ... Failed malloc() is. ... minor annoyance to a critical failure. ... there is something to recover from. ... Now, if I don't use this half-baked notion of "allocate or die", I can ...
    (comp.lang.c)
  • Re: memory reported under linux higher that allocated?
    ... I have 4 gig of RAM and when I allocate 3 gig the utilities show that my application is using 87% of the available RAM, so it's a half a gig more, why? ... I count all malloc by incrementing a "m" variable and I decrement the same "m" variable when doing free and at the program exit I display the value of "m" and it is zero. ...
    (Fedora)
  • Re: Review: My C FAQ Page
    ... the definition of memory leak is good but the information on ... this is not the most efficient way to allocate the 2-d array. ... You have ROW+1 calls to malloc. ... Allocate ROW pointers. ...
    (comp.lang.c)