🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #2087

Re: Allocating memory like an array

#2087
From: Matthias Blume
Date: Fri, 06 Nov 1992 21:44
81 lines
2841 bytes
In article <1debc4INNieg@chnews.intel.com>, bhoughto@sedona.intel.com
(Blair P. Houghton) writes:
|> In article <1992Nov5.203939.1323@fcom.cc.utah.edu>
tim@osiris.usi.utah.edu (Tim Burns 581-4439) writes:
|> >memory dynamically.  Right now, when I allocate my array using the 
|> >standard array allocation:  double A[L][M], where L and M are constants,
|> 
|> What you see is that `A' has a type compatible with `(double *)',
|> whereas what the _Numerical_Recipes_ method does is create
|> `m' as compatible with `(double **)'.
|> 
|> All you need to do is allocate enough space for the
|> two-dimensional array.  C's array-memory mapping is flat,
|> in row-major order.
|> 
|> You want L rows of M columns of doubles, so you need
|> 
|> 	m = (double *) malloc ( L * M * sizeof(double) );
|> 
|> and now `m' is compatible with `A'.
|> 
|> Accesses to `m' can now be made using C's array-access syntax;
|> That is,
|> 
|> 	int row, col;
|> 	...
|> 
|> 	m[row][col]
|> 
|> gives the number at row and col.
|> 
|> 	m[row][col+1]
|> 
|> gives the next number in `m', and
|> 
|>         m[row+1][col]
|> 
|> gives the number in the next row, i.e., L elements away from
|> the number in this row of `m'.
|> 
|> 				--Blair
|> 				  "Isn't this just a little basic?"

Haven't we seen this kind of wrong explanation only few weeks ago in
comp.lang.c?
How shall the poor compiler know, after defining

	double **m;

that m[x][y] means an element, which is x*M+y elements away from the
origin of m?  What you need is either to declare

	double (*m) [M];

which doesn't give you the freedom to have variable dimensions except
for L, or

	double *m;
	# define access(matrix,x,y,dim) ((matrix) [(x) * (dim) + (y)])

which has to be used like:

	m = malloc (L * M * sizeof (double));

	... access (m, x, y, M) ...

Of course, you may want to ``hide'' ``m'' and ``M'' inside access.
In ANSI C you can write

	# define m(x,y) (m[(x) * M + (y)])

because the ANSI C preprocessor will not go into a recursion on m.
With this kludge you can use ``m'' almost like ever:

	... m (x, y) ...

instead of
	... m [x][y]

Enjoy!
-Matthias
Technical Details
Message-ID: <1992Nov6.214446.9471@csservices.Princeton.EDU>
Path: arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!pacbell.com!decwrl!netsys!ukma!wupost!zaphod.mps.ohio-state.edu!cs.utexas.edu!rutgers!princeton!csservices!kastle!blume
References: <1992Nov5.203939.1323@fcom.cc.utah.edu> <1debc4INNieg@chnews.intel.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!charon.amdahl.com!pacbell.com!decwrl!netsys!ukma!wupost!zaphod.mps.ohio-state.edu!cs.utexas.edu!rutgers!princeton!csservices!kastle!blume
From: blume@kastle.Princeton.EDU (Matthias Blume)
Newsgroups: comp.std.c
Subject: Re: Allocating memory like an array
Message-ID: <1992Nov6.214446.9471@csservices.Princeton.EDU>
Date: 6 Nov 92 21:44:46 GMT
References: <1992Nov5.203939.1323@fcom.cc.utah.edu> <1debc4INNieg@chnews.intel.com>
Sender: news@csservices.Princeton.EDU (USENET News System)
Reply-To: blume@kastle.Princeton.EDU (Matthias Blume)
Organization: Dept. of Computer Science, Princeton University
Lines: 81