home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / sys / mac / programm / 18846 < prev    next >
Encoding:
Text File  |  1992-11-24  |  2.5 KB  |  60 lines

  1. Newsgroups: comp.sys.mac.programmer
  2. Path: sparky!uunet!nih-csl.dcrt.nih.gov!FAXCSL!FIXER
  3. From: fixer@faxcsl.dcrt.nih.gov (Chris Spiral Catfish Tate)
  4. Subject: Re: Large multidimensional arrays in C (THINK/MPW)???
  5. Message-ID: <1992Nov24.010106.9360@alw.nih.gov>
  6. Sender: postman@alw.nih.gov (AMDS Postmaster)
  7. Reply-To: fixer@faxcsl.dcrt.nih.gov
  8. Organization: Computer Systems Laboratory, DCRT, NIH
  9. References: <1992Nov23.161608.11712@nic.funet.fi>,<1992Nov23.203639.7607@newssun.med.miami.edu>
  10. Date: Tue, 24 Nov 1992 01:01:06 GMT
  11. Lines: 47
  12.  
  13. In article <1992Nov23.203639.7607@newssun.med.miami.edu>, jack@umbio.med.miami.edu (Jack Herrington) writes:
  14. >
  15. >newHand = NewHandle(sizeof(theStruct)*4000);
  16. >
  17. >But that will fail because Think is going to think of both of those as shorts
  18. >even though the product will be more than a short.
  19.  
  20. At least in THINK C 5, this will *not* fail; sizeof(theStruct) is treated
  21. as an unsigned long.  What *will* fail is something along these lines:
  22.  
  23.      short  size;
  24.  
  25.      size = sizeof(theStruct);
  26.      newHand = NewHandle(size * 4000);     // use 4000L to force a long mult.
  27.  
  28. Getting back to the original poster's question (how to dynamically allocate
  29. two-dimensional arrays), another approach I haven't seen is to use intermediate
  30. typedefs to set up the desired references:
  31.  
  32.      typedef short short_array[100];
  33.      typedef short_array big_array[100];
  34.  
  35.      for (i=100; i--; ) big_array[i] = (short_array *)
  36.           NewPtr(sizeof(short_array));  // is this the right cast?
  37.  
  38.      (*bigArray[x])[y] = someValue;
  39.  
  40. The drawback is that it takes an additional dereference to access a location.
  41. I myself would probably do this:
  42.  
  43.      short *short_array;      // simulate 100x100 array...
  44.      long  x, y;              // note that these are long!
  45.      
  46.      short_array = (short *) NewPtr((long) num_elements * sizeof(short));
  47.      *(short_array + x + 100*y) = someValue;
  48.  
  49. The only drawback is that here, you need to make sure the factor within the
  50. address calculation is correct (which presumably you *do* know... :-).  Also,
  51. it's important that the multiplications and additions in the address
  52. calculation be done as long arithmetic, to avoid generating negative offsets
  53. off of short_array.
  54.  
  55. ------------------------------------------------------------------------------
  56. Christopher Tate             | The Leadfoot Collection, Continued:
  57. Management System Designers  |     * Heavy Fuel (Dire Straits)
  58.                              |     * Last Scene in September (Preston Reed)
  59. fixer@faxcsl.dcrt.nih.gov    | Because driving fast is a cathartic experience.
  60.