home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sys.mac.programmer
- Path: sparky!uunet!nih-csl.dcrt.nih.gov!FAXCSL!FIXER
- From: fixer@faxcsl.dcrt.nih.gov (Chris Spiral Catfish Tate)
- Subject: Re: Large multidimensional arrays in C (THINK/MPW)???
- Message-ID: <1992Nov24.010106.9360@alw.nih.gov>
- Sender: postman@alw.nih.gov (AMDS Postmaster)
- Reply-To: fixer@faxcsl.dcrt.nih.gov
- Organization: Computer Systems Laboratory, DCRT, NIH
- References: <1992Nov23.161608.11712@nic.funet.fi>,<1992Nov23.203639.7607@newssun.med.miami.edu>
- Date: Tue, 24 Nov 1992 01:01:06 GMT
- Lines: 47
-
- In article <1992Nov23.203639.7607@newssun.med.miami.edu>, jack@umbio.med.miami.edu (Jack Herrington) writes:
- >
- >newHand = NewHandle(sizeof(theStruct)*4000);
- >
- >But that will fail because Think is going to think of both of those as shorts
- >even though the product will be more than a short.
-
- At least in THINK C 5, this will *not* fail; sizeof(theStruct) is treated
- as an unsigned long. What *will* fail is something along these lines:
-
- short size;
-
- size = sizeof(theStruct);
- newHand = NewHandle(size * 4000); // use 4000L to force a long mult.
-
- Getting back to the original poster's question (how to dynamically allocate
- two-dimensional arrays), another approach I haven't seen is to use intermediate
- typedefs to set up the desired references:
-
- typedef short short_array[100];
- typedef short_array big_array[100];
-
- for (i=100; i--; ) big_array[i] = (short_array *)
- NewPtr(sizeof(short_array)); // is this the right cast?
-
- (*bigArray[x])[y] = someValue;
-
- The drawback is that it takes an additional dereference to access a location.
- I myself would probably do this:
-
- short *short_array; // simulate 100x100 array...
- long x, y; // note that these are long!
-
- short_array = (short *) NewPtr((long) num_elements * sizeof(short));
- *(short_array + x + 100*y) = someValue;
-
- The only drawback is that here, you need to make sure the factor within the
- address calculation is correct (which presumably you *do* know... :-). Also,
- it's important that the multiplications and additions in the address
- calculation be done as long arithmetic, to avoid generating negative offsets
- off of short_array.
-
- ------------------------------------------------------------------------------
- Christopher Tate | The Leadfoot Collection, Continued:
- Management System Designers | * Heavy Fuel (Dire Straits)
- | * Last Scene in September (Preston Reed)
- fixer@faxcsl.dcrt.nih.gov | Because driving fast is a cathartic experience.
-