home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / std / c / 2989 < prev    next >
Encoding:
Internet Message Format  |  1992-11-08  |  2.6 KB

  1. Path: sparky!uunet!olivea!hal.com!decwrl!pa.dec.com!jrdzzz.jrd.dec.com!jit345.bad.jit.dec.com!diamond
  2. From: diamond@jit345.bad.jit.dec.com (Norman Diamond)
  3. Newsgroups: comp.std.c
  4. Subject: Re: Allocating memory like an array
  5. Message-ID: <BxCE0u.F1n@jrd.dec.com>
  6. Date: 7 Nov 92 10:48:30 GMT
  7. References: <1992Nov5.203939.1323@fcom.cc.utah.edu> <1debc4INNieg@chnews.intel.com>
  8. Sender: usenet@jrd.dec.com (USENET News System)
  9. Reply-To: diamond@jit.dec.com (Norman Diamond)
  10. Organization: Digital Equipment Corporation Japan , Tokyo
  11. Lines: 46
  12. Nntp-Posting-Host: jit345
  13.  
  14. In article <1debc4INNieg@chnews.intel.com> bhoughto@sedona.intel.com (Blair P. Houghton) writes:
  15. >In article <1992Nov5.203939.1323@fcom.cc.utah.edu> tim@osiris.usi.utah.edu (Tim Burns 581-4439) writes:
  16. >>memory dynamically.  Right now, when I allocate my array using the 
  17. >>standard array allocation:  double A[L][M], where L and M are constants,
  18.  
  19. >What you see is that `A' has a type compatible with `(double *)',
  20.  
  21. Reread the section on compatibility.  In most expressions this A would decay
  22. into a pointer of type double* (some would say it decays into a constrained
  23. subtype of some form that isn't described very well in the standard...), so
  24. it's close, but it's better to omit false red herrings.
  25.  
  26. >whereas what the _Numerical_Recipes_ method does is create
  27. >`m' as compatible with `(double **)'.
  28.  
  29. This is sort of necessary, if he wants to use C-like syntax to access the
  30. elements.  He needs Iliffe vectors as well, in order to do that.
  31.  
  32. >All you need to do is allocate enough space for the two-dimensional array.
  33. >C's array-memory mapping is flat, in row-major order.
  34.  
  35. And construct a way to access each element with his desired syntax.  If he
  36. uses your method, he has to construct AND USE macros to compute the address
  37. of each element, instead of using C-like syntax to access each element.
  38.  
  39. >You want L rows of M columns of doubles, so you need
  40. >    m = (double *) malloc ( L * M * sizeof(double) );
  41. >and now `m' is compatible with `A'.
  42.  
  43. I guess your declaration was    double *m;
  44.  
  45. >Accesses to `m' can now be made using C's array-access syntax; That is,
  46. >    int row, col;
  47. >    m[row][col]
  48. >gives the number at row and col.
  49.  
  50. m[row] gives the number at row, in the one-dimensional array that was
  51. allocated.  That is, if a number has been stored there first.
  52.  
  53. m[row][col] gives a diagnostic for violating the Constraint in 3.3.2.1,
  54. followed by undefined behavior such as (usually) denial of execution.
  55. For the second subscript, either m[row] or col is required to be a pointer.
  56. --
  57. Norman Diamond       diamond@jit081.enet.dec.com
  58. If this were the company's opinion, I wouldn't be allowed to post it.
  59. "It's been a lovely recession."
  60.