home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18530 < prev    next >
Encoding:
Text File  |  1992-12-30  |  1.5 KB  |  51 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!microsoft!wingnut!pauljo
  3. From: pauljo@microsoft.com (Paul Johns)
  4. Subject: Re: Newing a pointer to a pointer
  5. Message-ID: <1992Dec30.035200.21332@microsoft.com>
  6. Date: 30 Dec 92 03:52:00 GMT
  7. Organization: Microsoft Corp., Redmond, Washington, USA
  8. References: <1992Dec29.174843.20250@ncsu.edu> 
  9. Lines: 40
  10.  
  11. A pointer to a pointer is not the same as a pointer to
  12. an array in C++ (nor C).
  13.  
  14. Your design needs at least two memory allocations:  one for
  15. the data to be stored, and one for the array of pointers
  16. to the beginning of each row.
  17.  
  18. lspace would point to the array of pointers (one for each
  19. row); each pointer in the array would point to the beginning
  20. of its row.  The rows could be allocated in one block, or
  21. they could each be allocated separately.  They can also be
  22. of different lengths.
  23.  
  24. In any case, it's your responsibility to:
  25.  
  26. 1.  Allocate the array of pointers.
  27.  
  28. 2.  Allocate the data memory.
  29.  
  30. 3.  Set each pointer in the array to point to the appropriate
  31.     memory.
  32.  
  33. K & R has a lot more on this.  
  34.  
  35. If you're willing to live with a fixed row length, you can 
  36. use a design very similar to yours in which lspace is a 
  37. pointer to an array:
  38.  
  39.     const int LCOL = ???;    // at file scope
  40.  
  41.     list (*lspace)[LCOL];
  42.  
  43. If you think about what the computer has to do to index
  44. into a two-dimensional array stored in one-dimensional
  45. memory, it becomes more clear why this is so.
  46.  
  47. Or you can write a dynamic matrix class that would do what
  48. you want.
  49.  
  50. // Paul Johns
  51.