delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/04/15:32:03

From: Erik Max Francis <max AT alcyone DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: New and multidimensional array
Date: Sun, 04 Jan 1998 12:08:12 -0800
Organization: Alcyone Systems
Lines: 32
Message-ID: <34AFEC2C.1E3A421C@alcyone.com>
References: <34ae5551 DOT 4918923 AT 195 DOT 10 DOT 154 DOT 8>
NNTP-Posting-Host: newton.alcyone.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Aleksi Kallio wrote:

> What's the easiest way to reserve memory for multidimensional array
> with "new"?

Probably the most straightforward way is to allocate an array of
pointers, then then allocate space for each pointer.

For instance, if you want the array to be m x n, do something like this:

    char **pp;

    pp = new char *[m];

    for (int j = 0; j < m; j++)
        pp[j] = new char[n];

Now you can reference each element as pp[i][j] (0 <= i < m, 0 <= j < n).
When deleting, be sure to delete each subarray before deleting the main
array of pointers.

You can also just allocate a contiguous block of data and use operator
overloading and intermediate classes to allow usage of the pp[i][j]
notation.

-- 
         Erik Max Francis, &tSftDotIotE / mailto:max AT alcyone DOT com
                       Alcyone Systems / http://www.alcyone.com/max/
  San Jose, California, United States / icbm://+37.20.07/-121.53.38
                                     \
                       "Life may be / the product of imperfections."
                                   / (Marclo Gleiser)

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019