home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!microsoft!wingnut!pauljo
- From: pauljo@microsoft.com (Paul Johns)
- Subject: Re: Newing a pointer to a pointer
- Message-ID: <1992Dec30.035200.21332@microsoft.com>
- Date: 30 Dec 92 03:52:00 GMT
- Organization: Microsoft Corp., Redmond, Washington, USA
- References: <1992Dec29.174843.20250@ncsu.edu>
- Lines: 40
-
- A pointer to a pointer is not the same as a pointer to
- an array in C++ (nor C).
-
- Your design needs at least two memory allocations: one for
- the data to be stored, and one for the array of pointers
- to the beginning of each row.
-
- lspace would point to the array of pointers (one for each
- row); each pointer in the array would point to the beginning
- of its row. The rows could be allocated in one block, or
- they could each be allocated separately. They can also be
- of different lengths.
-
- In any case, it's your responsibility to:
-
- 1. Allocate the array of pointers.
-
- 2. Allocate the data memory.
-
- 3. Set each pointer in the array to point to the appropriate
- memory.
-
- K & R has a lot more on this.
-
- If you're willing to live with a fixed row length, you can
- use a design very similar to yours in which lspace is a
- pointer to an array:
-
- const int LCOL = ???; // at file scope
-
- list (*lspace)[LCOL];
-
- If you think about what the computer has to do to index
- into a two-dimensional array stored in one-dimensional
- memory, it becomes more clear why this is so.
-
- Or you can write a dynamic matrix class that would do what
- you want.
-
- // Paul Johns
-