home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / std / c / 2987 < prev    next >
Encoding:
Text File  |  1992-11-08  |  2.7 KB  |  94 lines

  1. Path: sparky!uunet!charon.amdahl.com!pacbell.com!decwrl!netsys!ukma!wupost!zaphod.mps.ohio-state.edu!cs.utexas.edu!rutgers!princeton!csservices!kastle!blume
  2. From: blume@kastle.Princeton.EDU (Matthias Blume)
  3. Newsgroups: comp.std.c
  4. Subject: Re: Allocating memory like an array
  5. Message-ID: <1992Nov6.214446.9471@csservices.Princeton.EDU>
  6. Date: 6 Nov 92 21:44:46 GMT
  7. References: <1992Nov5.203939.1323@fcom.cc.utah.edu> <1debc4INNieg@chnews.intel.com>
  8. Sender: news@csservices.Princeton.EDU (USENET News System)
  9. Reply-To: blume@kastle.Princeton.EDU (Matthias Blume)
  10. Organization: Dept. of Computer Science, Princeton University
  11. Lines: 81
  12.  
  13. In article <1debc4INNieg@chnews.intel.com>, bhoughto@sedona.intel.com
  14. (Blair P. Houghton) writes:
  15. |> In article <1992Nov5.203939.1323@fcom.cc.utah.edu>
  16. tim@osiris.usi.utah.edu (Tim Burns 581-4439) writes:
  17. |> >memory dynamically.  Right now, when I allocate my array using the 
  18. |> >standard array allocation:  double A[L][M], where L and M are constants,
  19. |> 
  20. |> What you see is that `A' has a type compatible with `(double *)',
  21. |> whereas what the _Numerical_Recipes_ method does is create
  22. |> `m' as compatible with `(double **)'.
  23. |> 
  24. |> All you need to do is allocate enough space for the
  25. |> two-dimensional array.  C's array-memory mapping is flat,
  26. |> in row-major order.
  27. |> 
  28. |> You want L rows of M columns of doubles, so you need
  29. |> 
  30. |>     m = (double *) malloc ( L * M * sizeof(double) );
  31. |> 
  32. |> and now `m' is compatible with `A'.
  33. |> 
  34. |> Accesses to `m' can now be made using C's array-access syntax;
  35. |> That is,
  36. |> 
  37. |>     int row, col;
  38. |>     ...
  39. |> 
  40. |>     m[row][col]
  41. |> 
  42. |> gives the number at row and col.
  43. |> 
  44. |>     m[row][col+1]
  45. |> 
  46. |> gives the next number in `m', and
  47. |> 
  48. |>         m[row+1][col]
  49. |> 
  50. |> gives the number in the next row, i.e., L elements away from
  51. |> the number in this row of `m'.
  52. |> 
  53. |>                 --Blair
  54. |>                   "Isn't this just a little basic?"
  55.  
  56. Haven't we seen this kind of wrong explanation only few weeks ago in
  57. comp.lang.c?
  58. How shall the poor compiler know, after defining
  59.  
  60.     double **m;
  61.  
  62. that m[x][y] means an element, which is x*M+y elements away from the
  63. origin of m?  What you need is either to declare
  64.  
  65.     double (*m) [M];
  66.  
  67. which doesn't give you the freedom to have variable dimensions except
  68. for L, or
  69.  
  70.     double *m;
  71.     # define access(matrix,x,y,dim) ((matrix) [(x) * (dim) + (y)])
  72.  
  73. which has to be used like:
  74.  
  75.     m = malloc (L * M * sizeof (double));
  76.  
  77.     ... access (m, x, y, M) ...
  78.  
  79. Of course, you may want to ``hide'' ``m'' and ``M'' inside access.
  80. In ANSI C you can write
  81.  
  82.     # define m(x,y) (m[(x) * M + (y)])
  83.  
  84. because the ANSI C preprocessor will not go into a recursion on m.
  85. With this kludge you can use ``m'' almost like ever:
  86.  
  87.     ... m (x, y) ...
  88.  
  89. instead of
  90.     ... m [x][y]
  91.  
  92. Enjoy!
  93. -Matthias
  94.