Re: Multi-dimensional array allocation with single 'new' call
From: Scott J. McCaughrin (sjmccaug_at_bluestem.prairienet.org)
Date: 10/29/04
- Next message: Alf P. Steinbach: "Re: MS Visual Studio 7.1 terminates (no message) when compiling a C# project"
- Previous message: Wolfgang Keller: "Re: Recommend a Conference for 2005"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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;
}
- Next message: Alf P. Steinbach: "Re: MS Visual Studio 7.1 terminates (no message) when compiling a C# project"
- Previous message: Wolfgang Keller: "Re: Recommend a Conference for 2005"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|