home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / sys / mac / programm / 21324 < prev    next >
Encoding:
Text File  |  1993-01-12  |  2.5 KB  |  57 lines

  1. Newsgroups: comp.sys.mac.programmer
  2. Path: sparky!uunet!cs.utexas.edu!hermes.chpc.utexas.edu!news.utdallas.edu!corpgate!crchh327!crchh447!brunner
  3. From: brunner@crchh447.bnr.ca (James Brunner)
  4. Subject: Re: C array question
  5. Sender: news@news.rich.bnr.ca (news server)
  6. Message-ID: <C0rHtq.92E@news.rich.bnr.ca>
  7. Date: Tue, 12 Jan 1993 22:15:26 GMT
  8. Distribution: usa
  9. Reply-To: brunner@crchh447.bnr.ca (James Brunner)
  10. References:  <C0prG1.AsC@plato.ds.boeing.com>
  11. Nntp-Posting-Host: crchh447
  12. Organization: BNR, Inc.
  13. Lines: 42
  14.  
  15. In article <C0prG1.AsC@plato.ds.boeing.com>, housen@plato.ds.boeing.com (Kevin housen/.na) writes:
  16. |> I am making a jump from Pascal to C and have a simple question
  17. |> regarding creation of a 2D array at runtime.  I want to create
  18. |> an array of floats r rows by c columns.  A (non-Mac) friend
  19. |> suggests
  20. |> 
  21. |>         float **m        /* this will be the 2D array */
  22. |>         int r, c, count
  23. |> 
  24. |>         m = (float **) malloc (r*sizeof(float *));
  25. |>         for (count=0, count < r, count++)
  26. |>             m[count] = (float *) malloc (c * sizeof(float) );
  27. |> 
  28. |> 1.  If, instead of malloc, I want to use the Mac memory manager
  29. |>    routines, does it matter if I use NewPtr or NewHandle (assuming
  30. |>    I lock handles at the appropriate times)?  Does malloc just
  31. |>    call NewPtr?
  32.  
  33. What this is doing is allocating an array of pointers and then for each pointer
  34. in the array, allocating another array.  For example, for a 2x3 array, this is
  35. allocating one array of 2 pointers AND 2 arrays of 3 elements.  Note that this
  36. is VERY different from declaring "int x[2][3]" which allocates one contiguous
  37. block of 6 ints (but can be doubly indexed).
  38.  
  39. malloc is generally eqivalent to NewPtr.  Can you do this with handles?  Well,
  40. yes you can, but it gets a bit ugly.  You would have a handle to an array of
  41. handles to arrays.  If this is localized code or you just do this once for the
  42. program, I wouldn't hesitate to use NewPtr.  If, however, you allocate and
  43. deallocate these things like mad all over your program (and in random order),
  44. I would consider the handle approach.
  45.  
  46. |> 2.  Can this be generalized to a 3D array?
  47.  
  48. A three dimensional array could be done as:
  49.     an array of pointers for the first dimension
  50.     an array of pointers for each element of the second dimension
  51.     an array of items of third level elements
  52. -- 
  53. ---------------------------------------------------------------------------
  54. Jim Brunner - (brunner@bnr.ca)
  55. All opinions are my own and have nothing whatsoever to do with BNR, NT,
  56. NTI, Bell Canada, or any of the BCE corporations or affiliates.
  57.