Path: utzoo!attcan!uunet!wuarchive!mit-eddie!bloom-beacon!eru!hagbard!sunic!news.funet.fi!funic!santra!fuug.fi!pena From: pena@fuug.fi (Olli-Matti Penttinen) Newsgroups: comp.lang.c++ Subject: Re: new on two dimension array Message-ID: Date: 24 Oct 90 16:17:04 GMT References: <1990Oct17.153438.12299@swbatl.sbc.com> Sender: news@santra.uucp (Cnews - USENET news system) Organization: Finnish UNIX Users Group, Finland Lines: 66 In-Reply-To: uucigj@gandalf.sbc.com's message of 17 Oct 90 15:34:38 GMT In article <1990Oct17.153438.12299@swbatl.sbc.com> uucigj@gandalf.sbc.com (Gregg Jensen (5-3531)) writes: >Novice question: how do you use new to allocate space for a two >dimension array? >char **matrix = new ??????; It's tempting to assume that char **matrix = new char[XMAX][YMAX]; would do the trick. However, the real problem lies elsewhere. When you use a real multidimensional array, you only need room for the actual elements, since the compiler knows how the calculate the offset of any element at run time. With dynamic arrays, it's a different thing: you either (a) explicitly calculate offsets char *matrix = new char[XMAX * YMAX]; ... matrix[i * YMAX + j] = ...; or (b) in addition to the actual data, allocate an extra vector of XMAX pointers (in this case char *) and initialize them to point at elements 0, YMAX, 2 * YMAX, ... , (XMAX -1) * YMAX, respectively: char *data = new char[XMAX * YMAX]; char *ptrs = new (char(*[XMAX])); for (int i = 0; i < XMAX; i++) matrix[i] = data + i * YMAX; ... matrix[i][j] = ...; This works because by definition a[i][j] is actually *(*(a + i)+j). Note, however that this seemless integration of dynamic multi-dimensional arrays doesn't necessarily work for class objects, whose [], *, &, etc. operators have different semantics from their mundane counterparts. Note also that this approach is readily extensible to 3, 4, ... dimensional cases, because every 3-D array can be considered a 1-D array whose elements happen to be 2-D arrays (or 1-D arrays of 1-D arrays, for that matter) ad infinitum. To make life easier, I wrote a couple of years ago a fairly portable K&R C function, that only uses malloc() once to allocate and initialize an arbitrary dimensioned array of any type. It is less than 100 lines of code, I think. If anyone is interested, let me know. I hope this helps, ==pena -- Olli-Matti Penttinen | "When in doubt, use brute force." | Brainware Oy | --Ken Thompson | Tekniikantie 17 +-----------------------------------+ 02150 ESPOO, Finland Tel. +358 0 4375 320 Fax. +358 0 4553 117 -- Olli-Matti Penttinen | "When in doubt, use brute force." | Brainware Oy | ==Ken Thompson | Tekniikantie 17 +-----------------------------------+ 02150 ESPOO, Finland Tel. +358 0 4375 320 Fax. +358 0 4553 117