Mail Archives: djgpp/1998/01/04/15:32:03
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 -