472,982 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,982 software developers and data experts.

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 9476
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Roel Wuyts | last post by:
CALL FOR CONTRIBUTIONS International Workshop on Revival of Dynamic Languages http://pico.vub.ac.be/~wdmeuter/RDL04/index.html (at OOPSLA2004, Vancouver, British Columbia, Canada, October...
5
by: Terri | last post by:
I have a form with a multi-select combo. I dynamically build a SELECT statement, open a report, and set the recordsource to my dynamic SELECT statement. I count the records returned in the report...
3
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which...
8
by: novus | last post by:
Hi, In ASP.net 2.0 I make a control which add the same controls dynamically. In the oninit event I add the controls to the controls collection. After that the loadviewstate event fills in the...
1
by: Tim Silva | last post by:
SDX Modeling, Simulation and Numerical Computing Environment for science and engineering has now been released. Major features include: * A unified simulation environment for modeling virtually...
9
by: Jimbo | last post by:
Hello, I have a user request to build a form in an Access database where the user can check off specific fields to pull in a query. For example, let's say I have 10 fields in a table. The user...
0
by: alexandre.bergel | last post by:
Dear colleges, You might want to consider Dyla'07 as a good venue to present your work and your favourite programming language. Regards, Alexandre ...
0
by: Alexandre Bergel | last post by:
Dear colleague, Please, note that after the workshop, best papers will be selected, and a second deadline will then be set regarding preparation of the Electronic Communications of the...
4
by: mbrunell | last post by:
Hi, Under AIX, I have created a script which is checking URL status. Problems come when I want to check HTTPS URL status... To have my script working with HTTPS URLs, I must set the LIBPATH...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.