Dynamic multi-dimensional arrays.

  • Follow


I'm sure I saw this mentioned somewhere, but I can't find it.  How can I 
dynamically allocate a multi-dimensional array in C++?

My next question, if this gets figured out, is if I should use the delete 
[] command to remove it, or would that be delete [][]?


-- 
"What are the possibilities of small but movable machines?  They may or
may not be useful, but they surely would be fun to make."
    -- Richard P. Feynman, 1959
0
Reply glhansen (396) 6/3/2004 1:59:54 AM

"Gregory L. Hansen" <glhansen@steel.ucs.indiana.edu> wrote...
> I'm sure I saw this mentioned somewhere, but I can't find it.  How can I
> dynamically allocate a multi-dimensional array in C++?

This is covered in the FAQ Lite (http://www.parashift.com/c++-faq-lite/)

> My next question, if this gets figured out, is if I should use the delete
> [] command to remove it, or would that be delete [][]?

Read the FAQ.

V


0
Reply v.Abazarov (13255) 6/3/2004 3:07:54 AM


glhansen@steel.ucs.indiana.edu (Gregory L. Hansen) wrote in message news:<c9m0mq$6rq$3@hood.uits.indiana.edu>...
> I'm sure I saw this mentioned somewhere, but I can't find it.  How can I 
> dynamically allocate a multi-dimensional array in C++?

In general you shouldn't do this for raw arrays, but rather use STL
containers like std::vector or perhaps std::valarray ... std::valarray
has some associated helper classes that make it easier to "slice" a
1-D storage array to make it behave like an N-D array.  An even better
choice would be to use a third party library .. check out the
Object-Oriented Numberics page for ideas:
http://www.oonumerics.org

However, if you have your heart set on doing it with raw pointers,
here is an example for a 3D matrix.

// Routine to dynamically allocate memory for a 3D array or double
// Preconditions: m==0, d1,d2,d3 > 0
// Postconditions: m points to a zero-initialized d1xd2xd3 array of
doubles
void allocate_3D(double &***m, int d1, int d2, int d3) {
  if (m!=0) {
    // Yikes, pointer is not NULL ... don't want to double-allocate
    // emit error message and return
    return;\
  }
  m=new double** [d1];
  for (int i=0; i<d1; ++i) {
    m[i]=new double* [d2];
    for (int j=0; j<d2; ++j) {
      m[i][j]=new double [d3];
      for (int k=0; k<d3; ++k) 
        m[i][j][k]=0;
    }
  }
}

Note the pass by reference above ... otherwise you would assign
everything you have allocated to a temporary and it would cause a
memory leak, since the temporary would be destroyed on exit without
deallocating the memory.

One thing you might want to consider is the difficulties inherent in
using a 3D array implemented in this manner.  For example, what if you
want to extract a 2D slice using the first and third dimensions?  This
takes some thought to accomplish, which is why you should avoid
re-inventing the wheel and use an existing library.

> My next question, if this gets figured out, is if I should use the delete 
> [] command to remove it, or would that be delete [][]?

No ... I'll leave writing the associated deallocation routine as an
exercise for you, if I haven't already convinced you that doing this
with raw pointers is tricky and generally unadvisable.

HTH, Dave Moore
0
Reply dtmoore (74) 6/3/2004 7:50:30 AM

In article <306d400f.0406022350.15c22fdf@posting.google.com>,
Dave Moore <dtmoore@rijnh.nl> wrote:
>glhansen@steel.ucs.indiana.edu (Gregory L. Hansen) wrote in message
>news:<c9m0mq$6rq$3@hood.uits.indiana.edu>...
>> I'm sure I saw this mentioned somewhere, but I can't find it.  How can I 
>> dynamically allocate a multi-dimensional array in C++?
>
>In general you shouldn't do this for raw arrays, but rather use STL
>containers like std::vector or perhaps std::valarray ... std::valarray
>has some associated helper classes that make it easier to "slice" a
>1-D storage array to make it behave like an N-D array.  An even better
>choice would be to use a third party library .. check out the
>Object-Oriented Numberics page for ideas:
>http://www.oonumerics.org
>
>However, if you have your heart set on doing it with raw pointers,
>here is an example for a 3D matrix.
>
>// Routine to dynamically allocate memory for a 3D array or double
>// Preconditions: m==0, d1,d2,d3 > 0
>// Postconditions: m points to a zero-initialized d1xd2xd3 array of
>doubles
>void allocate_3D(double &***m, int d1, int d2, int d3) {
>  if (m!=0) {
>    // Yikes, pointer is not NULL ... don't want to double-allocate
>    // emit error message and return
>    return;\
>  }
>  m=new double** [d1];
>  for (int i=0; i<d1; ++i) {
>    m[i]=new double* [d2];
>    for (int j=0; j<d2; ++j) {
>      m[i][j]=new double [d3];
>      for (int k=0; k<d3; ++k) 
>        m[i][j][k]=0;
>    }
>  }
>}

Wow.  I don't even know what three stars in a row mean.  C++ lets you 
define fixed multi-dimensional arrays, so I'd assumed it would let you 
define them dynamically with something similar to the usual notation.  

I knew what vectors and some of that other stuff were, at one time.  I'll 
have to find out again.  It's a lot of stuff to keep in one head.
-- 
"A good plan executed right now is far better than a perfect plan
executed next week."
                       -Gen. George S. Patton
0
Reply glhansen (396) 6/3/2004 4:27:33 PM

Gregory L. Hansen wrote:
> [...]  It's a lot of stuff to keep in one head.

One needs a really big head for that. :-)
0
Reply v.Abazarov (13255) 6/3/2004 5:09:46 PM

> 
> 
> Wow.  I don't even know what three stars in a row mean.  C++ lets you 
> define fixed multi-dimensional arrays, so I'd assumed it would let you 
> define them dynamically with something similar to the usual notation.  
> 


Yeah, that would be lovely. You should consider using the Standard 
Template Library stuff 
(http://www.msoe.edu/eecs/cese/resources/stl/index.htm#WhatIs) for this 
kind of thing.
A raw array of pointers like the implementation described is very fast, 
but can make life difficult.

> I knew what vectors and some of that other stuff were, at one time.  I'll 
> have to find out again.  It's a lot of stuff to keep in one head.

0
Reply nospam239 (160) 6/4/2004 10:28:59 AM

Havatcha wrote:

>> Wow.  I don't even know what three stars in a row mean.  C++ lets you
>> define fixed multi-dimensional arrays, so I'd assumed it would let you
>> define them dynamically with something similar to the usual notation.
>> 
> 
> 
> Yeah, that would be lovely. You should consider using the Standard
> Template Library stuff
> (http://www.msoe.edu/eecs/cese/resources/stl/index.htm#WhatIs) for this
> kind of thing.

It depends what your objectives are.  Things such as vector, of vector of
vector, are easier to implement than multidimensional mappings over
valarray.  OTOH, valarray is designed to support maximum efficiency and
will most likely run faster than any of the other alternatives from the
Standard Library. Various people have proposed a real array template for
the standard.  That is, a 'vector' of fixed size. 

Check out the last three examples from this page:
http://www.research.att.com/~bs/3rd_code.html

Somewhere on Josuttis's site there is an example of an implementation of a
multidimensional array.
http://www.josuttis.com/libbook/index.html

> A raw array of pointers like the implementation described is very fast,
> but can make life difficult.

There are a lot of different matrix and multidimensional array
implementations such as are found at www.boost.org, Blitz++, MTL, etc.

>> I knew what vectors and some of that other stuff were, at one time.  I'll
>> have to find out again.  It's a lot of stuff to keep in one head.

I had taken a course in C++ just before templates came into the mainstream. 
When I picked up C++ again this year, I thought it was going to be
relatively easy to get up to speed.  I have never worked so hard at
learning anything in all my life, and I've read the
_Feynman_Lectures_on_Physics_ and worked the problems in the exercise
manual.

I should add that part of the reason it's been tough going is because I am
learning by using the latest gcc and the CVS images of KDevelop.  It's not
called 'bleeding edge' for naught.

-- 
STH 
Hatton's Law: "There is only One inviolable Law"
KDevelop: http://www.kdevelop.org  SuSE: http://www.suse.com
Mozilla: http://www.mozilla.org
0
Reply susudata (549) 6/4/2004 2:17:15 PM

6 Replies
19 Views

(page loaded in 0.104 seconds)


Reply: