home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / sys / mac / programm / 20088 < prev    next >
Encoding:
Text File  |  1992-12-21  |  3.0 KB  |  100 lines

  1. Path: sparky!uunet!olivea!spool.mu.edu!uwm.edu!rpi!deweeset
  2. From: deweeset@ptolemy3.rdrc.rpi.edu (Thomas E. DeWeese)
  3. Newsgroups: comp.sys.mac.programmer
  4. Subject: Re: Think C 32K segment
  5. Message-ID: <90p2z+f@rpi.edu>
  6. Date: 19 Dec 92 20:15:23 GMT
  7. References: <1go3ohINN4pq@network.ucsd.edu> <chuck-181292154535@choffman.gte.com>
  8. Distribution: usa
  9. Organization: Rensselaer Polytechnic Institute, Troy, NY
  10. Lines: 87
  11. Nntp-Posting-Host: ptolemy3.rdrc.rpi.edu
  12.  
  13. In article <chuck-181292154535@choffman.gte.com> chuck@gte.com (Chuck Hoffman) writes:
  14. >In article <1go3ohINN4pq@network.ucsd.edu>, sah@coast.ucsd.edu (Scott A.
  15. >Herndon) wrote:
  16.  
  17.  He Want's
  18. >>     int Height[129][129];
  19.  
  20. >I would try an approach using NewHandle, to get the memory you need at
  21. >execution time, instead of at compile time.  Then initiailize the array
  22. >after you get it.
  23.  
  24.  OK I agree with you so far. Continue
  25.  
  26. >typedef    struct    heightStruct
  27. >    {    int    h1;
  28. >        int h2;
  29. >    }    **heightStructH;
  30. WHAT are you doing defining a structure?
  31.  
  32. >myHeightStructH =
  33. >        (heightStructH) NewHandle(129 * sizeof(struct heightStruct));
  34.  
  35. well this is equvalent to:
  36. struct    heightStruct arry[129];  //Not what he wanted int [129][129]
  37.  
  38. >/* later, to get info from element 5 ("j") of the structure:    */
  39. >j = 5;
  40. >workH2 = (*(*myHeightStructH + j)).h2;
  41. Well a much more intuitive way would be:
  42. workH2 = (*myHeightStructH)[j].h2;  //Although I still don't know why
  43.                     //you have a structure defined.
  44.  
  45. >/* remember about locking/unlocking, and, when done, disposing    */
  46.  
  47. Well there are two "good" ways of doing this.  The transparent way and
  48. the easy way.
  49. for the transparent way what you want to do is:
  50.  
  51. int **arry;
  52. void main(void)
  53. {
  54.   int i,x;
  55.  
  56.   arry = NewPtr(sizeof(int *)*129);
  57.   for (i=0;i<130;i++)
  58.     arry[i] = NewPtr(sizeof(int)*129);
  59.   
  60.   //Now since we used Ptr's you don't have to worry about locking unlocking.
  61.   //and a reference would be as follows.
  62.  
  63.   x = arry[15][25];
  64. }
  65.  
  66. The Good Way(TM) to do this would be
  67.  
  68. int **arry;
  69.  
  70. void main(void)
  71. {
  72.   int i;
  73.  
  74.   arry = NewHandle(sizeof(int *)*129*129);  //now everything is one block
  75.   
  76.   //an access would be done as follows
  77.  
  78.   x = (*arry)[15*129+25]; //we need the 15*130 to offset the rows
  79.                           //Note that if we were to pass any addresses
  80.                   // you would have to lock the memory down
  81. }
  82.  
  83. a macro might make this nicer such as
  84.  
  85. #define index(a,x,y) (*a)[x*129+y]
  86.                             ^^^ note hardcoded array row size!
  87.  
  88. so an access would be:
  89.   x = index(arry,15,25);
  90.  
  91. BTW you are now using a language that starts arrays at 0.  This makes
  92. indexing, and generating offsets so much nicer.  I know that Fortran always
  93. starts at 1 (and you have to create global arrays, no dynamic mem alloc
  94. until recently).  But one thing you will learn is that while you might think
  95. it is easier to start indexing at 1, you really are better off getting used
  96. to indexing at 0. (so your array would by int arry[128][128]) assuming you
  97. really want and array of 128 elements.
  98.                           Thomas DeWeese
  99. deweeset@rdrc.rpi.edu
  100.