468,753 Members | 1,140 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,753 developers. It's quick & easy.

Dynamic multi-dimensional arrays.


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
Jul 22 '05 #1
6 9285
"Gregory L. Hansen" <gl******@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
Jul 22 '05 #2
gl******@steel.ucs.indiana.edu (Gregory L. Hansen) wrote in message news:<c9**********@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
Jul 22 '05 #3
In article <30**************************@posting.google.com >,
Dave Moore <dt*****@rijnh.nl> wrote:
gl******@steel.ucs.indiana.edu (Gregory L. Hansen) wrote in message
news:<c9**********@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
Jul 22 '05 #4
Gregory L. Hansen wrote:
[...] It's a lot of stuff to keep in one head.


One needs a really big head for that. :-)
Jul 22 '05 #5


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/resour...dex.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.


Jul 22 '05 #6
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/resour...dex.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
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Leo J. Hart IV | last post: by
8 posts views Thread by novus | last post: by
1 post views Thread by Tim Silva | last post: by
reply views Thread by alexandre.bergel | last post: by
reply views Thread by Alexandre Bergel | last post: by
4 posts views Thread by mbrunell | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.