Article View: comp.std.c
Article #2089Re: Allocating memory like an array
From: Norman Diamond
Date: Sat, 07 Nov 1992 10:48
Date: Sat, 07 Nov 1992 10:48
46 lines
2751 bytes
2751 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 *)', Reread the section on compatibility. In most expressions this A would decay into a pointer of type double* (some would say it decays into a constrained subtype of some form that isn't described very well in the standard...), so it's close, but it's better to omit false red herrings. >whereas what the _Numerical_Recipes_ method does is create >`m' as compatible with `(double **)'. This is sort of necessary, if he wants to use C-like syntax to access the elements. He needs Iliffe vectors as well, in order to do that. >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. And construct a way to access each element with his desired syntax. If he uses your method, he has to construct AND USE macros to compute the address of each element, instead of using C-like syntax to access each element. >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'. I guess your declaration was double *m; >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] gives the number at row, in the one-dimensional array that was allocated. That is, if a number has been stored there first. m[row][col] gives a diagnostic for violating the Constraint in 3.3.2.1, followed by undefined behavior such as (usually) denial of execution. For the second subscript, either m[row] or col is required to be a pointer. -- Norman Diamond diamond@jit081.enet.dec.com If this were the company's opinion, I wouldn't be allowed to post it. "It's been a lovely recession."
Technical Details
Message-ID:
<BxCE0u.F1n@jrd.dec.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!olivea!hal.com!decwrl!pa.dec.com!jrdzzz.jrd.dec.com!jit345.bad.jit.dec.com!diamondReferences:
<1992Nov5.203939.1323@fcom.cc.utah.edu> <1debc4INNieg@chnews.intel.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!olivea!hal.com!decwrl!pa.dec.com!jrdzzz.jrd.dec.com!jit345.bad.jit.dec.com!diamond From: diamond@jit345.bad.jit.dec.com (Norman Diamond) Newsgroups: comp.std.c Subject: Re: Allocating memory like an array Message-ID: <BxCE0u.F1n@jrd.dec.com> Date: 7 Nov 92 10:48:30 GMT References: <1992Nov5.203939.1323@fcom.cc.utah.edu> <1debc4INNieg@chnews.intel.com> Sender: usenet@jrd.dec.com (USENET News System) Reply-To: diamond@jit.dec.com (Norman Diamond) Organization: Digital Equipment Corporation Japan , Tokyo Lines: 46 Nntp-Posting-Host: jit345