Dynamic multi-dimensional arrays.

  • Thread starter Gregory L. Hansen
  • Start date
G

Gregory L. Hansen

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 [][]?
 
D

Dave Moore

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=new double* [d2];
for (int j=0; j<d2; ++j) {
m[j]=new double [d3];
for (int k=0; k<d3; ++k)
m[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
 
G

Gregory L. Hansen

(e-mail address removed) (Gregory L. Hansen) wrote in message
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=new double* [d2];
for (int j=0; j<d2; ++j) {
m[j]=new double [d3];
for (int k=0; k<d3; ++k)
m[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.
 
H

Havatcha

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.
 
S

Steven T. Hatton

Havatcha said:
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 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top