473,327 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Allocating Four Dimensional Dynamic Arrays...

Actually, how would I go about allocating a four-dimensional dynamic
array?

I only know how to make two dimensional dynamic arrays:
double **v;
v = new double*[C];
for (int i=0; i<C; i++)
{
v[i] = new double[n];
}

Well my plans have changed a bit and I need to make an array to store
matrices "v" (like the one I allocated above with dynamic dimensions C*n).
I'm not going to use the code above... I want to allocate all four
dimensions in one snippet. It needs to be done in a similar manner because
I'm not sure that the memory will be contiguous.

What I want to do is have an array going a variable number of levels deep,
and the row after the previous will have "n" times the amount of matrices
"v".

For example... if n was 3:

On the first row there would contain 1 matrix "v".
On the second row there would contain 3 matrix "v".
On the third row there would contain 9 matrix "v".

In the program I am writing it would be convenient to access these members
like this:

example:
[1, 2, 1, 2]
This would correlate to the matrix v found on the second row and third
column of this matrix. It would access the second row and third column of
the member v that was seleced by the first two parameters)

Jul 22 '05 #1
11 5184

"fivelitermustang" <fi**************@shaw.ca> wrote in message
news:26******************************@localhost.ta lkaboutprogramming.com...
Actually, how would I go about allocating a four-dimensional dynamic
array?

I only know how to make two dimensional dynamic arrays:
double **v;
v = new double*[C];
for (int i=0; i<C; i++)
{
v[i] = new double[n];
}

Well my plans have changed a bit and I need to make an array to store
matrices "v" (like the one I allocated above with dynamic dimensions C*n).
I'm not going to use the code above... I want to allocate all four
dimensions in one snippet. It needs to be done in a similar manner because
I'm not sure that the memory will be contiguous.

What I want to do is have an array going a variable number of levels deep,
and the row after the previous will have "n" times the amount of matrices
"v".

For example... if n was 3:

On the first row there would contain 1 matrix "v".
On the second row there would contain 3 matrix "v".
On the third row there would contain 9 matrix "v".
In the program I am writing it would be convenient to access these members
like this:

example:
[1, 2, 1, 2]
This would correlate to the matrix v found on the second row and third
column of this matrix. It would access the second row and third column of
the member v that was seleced by the first two parameters)

A big issue is whether the number of dimensions is a constant known at
compile time, or whether you want to vary that at run time. Most of the time
you say four dimensions but then you say 'a variable number of levels deep'.

If the former then look at multi_array from boost,
http://www.boost.org/libs/multi_array/doc/index.html.

If the later then its not easy. One problem is how you access the
multi-dimensional array when you need a variable number integers to specify
an element. You would have to overload operator() with a variable number of
parameters I think.

In any case it sounds a very unwieldy data structure, I doubt that you
really need to do this, but then I don't know what problem you are actually
trying to solve.

john
Jul 22 '05 #2
The amount of levels is specified at run time by the user. Instead of
variable, I meant to say dynamic as the memory would be allocated at
run-time when the user specifies it.

Actually, I've looked at the problem and I've already coded out my
pseudo-code for this problem to work with this type of strucutre. This
"pyramid"-like structure would be the best option to use in my case.

I was wondering what the syntax would be to make this and how would I go
about deallocating the memory and so forth.

Jul 22 '05 #3

"fivelitermustang" <fi**************@shaw.ca> wrote in message
news:ce******************************@localhost.ta lkaboutprogramming.com...
The amount of levels is specified at run time by the user. Instead of
variable, I meant to say dynamic as the memory would be allocated at
run-time when the user specifies it.

Actually, I've looked at the problem and I've already coded out my
pseudo-code for this problem to work with this type of strucutre. This
"pyramid"-like structure would be the best option to use in my case.

I was wondering what the syntax would be to make this and how would I go
about deallocating the memory and so forth.


I'm not sure exactly what you requirements are, maybe if you showed me the
pseudo code.

But in any case the answer is the usual one, you write a class that defines
the interface you need and then you go about writing the implementation. The
big issue seems to be that you require variable numbers of arguments in the
constructor and in some sort of element access method.

class MultiArray
{
public:
MultiArray(int rank, ...);
double& operator()(int first_index, ...);
};

rank is the number of dimensions of your array, and it is expected to be
followed by the size of each dimension (also int's). operator() is expected
to be called with an int for each dimension to access an individual element.

Note there is no safety here, there is no way for C++ or you to check that
the constructor or the operator() is called with the correct number of
arguments. This is why this sort of thing is rarely done.

To access the variable number of parameter you need to use the macros in
<stdarg.h>. Here's a snippet of what the constructor might look like

#include <stdarg.h>

MultiArray::MultiArray(int rank, ...) : my_rank(rank)
{
va_list arg_ptr;
va_begin(arg_ptr, rank);

// calculate total size
int size = 1;
for (int i = 0; i < rank; ++i)
size *= va_arg(arg_ptr, int);
...
va_end(arg_ptr);
}

That's just to give you an idea. I think you have quite a lot of work to do
to make this work.

john
Jul 22 '05 #4
I haven't covered classes yet so I'm not quite familiar with the workings
of the code snippet you have posted.

I have the code written to iterate through a static four-dimensional
array. The array is initialized in the pyramid structure and the remaining
values are zero. It works properly and moves through them and I get the
proper results. I just need to get this dynamically allocated.

Since I'm not really that familiar with classes isn't it possible to
initialize this array with loops and making pointers?

If that is possible could you just show me the proper syntax to make a 4D
square/rectangular array using the method that I was using to make that 2D
dynamic array?

Thanks for your patience.

Adam

Jul 22 '05 #5

"fivelitermustang" <fi**************@shaw.ca> wrote in message
news:50******************************@localhost.ta lkaboutprogramming.com...
I haven't covered classes yet so I'm not quite familiar with the workings
of the code snippet you have posted.

I have the code written to iterate through a static four-dimensional
array. The array is initialized in the pyramid structure and the remaining
values are zero. It works properly and moves through them and I get the
proper results. I just need to get this dynamically allocated.

Since I'm not really that familiar with classes isn't it possible to
initialize this array with loops and making pointers?

If that is possible could you just show me the proper syntax to make a 4D
square/rectangular array using the method that I was using to make that 2D
dynamic array?


I'm confused about what you want, but here some code that allocates a 4D
array. It's the same as your 2D code but expanded to 4 dimensions. I'm not
sure what dimensions you actually want (some combination of C and n in your
original post I think) so I've used D1, D2, D3 and D4 for the dimensions,
you can substitute the values you want.

double**** v;
v = new double***[D1];
for (int i = 0; i < D1; ++i)
{
v[i] = new double**[D2];
for (int j = 0; j < D2; ++j)
{
v[i][j] = new double*[D3];
for (int k = 0; k < D3; ++k)
{
v[i][j][k] = new double[D4];
}
}
}

But this doesn't work if you want a variable number of dimensions, which is
what I thought you wanted. Nor does it allocate memory in a single block,
which is another thing I thought you wanted. So I'm a bit confused.

There's nothing special about classes, anything you can do inside a class
you can also do outside a class. What classes do however is wrap up all your
code in an easy to use package, something that might be quite useful here.

john
Jul 22 '05 #6

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2h************@uni-berlin.de...
|
| "fivelitermustang" <fi**************@shaw.ca> wrote in message
| news:50******************************@localhost.ta lkaboutprogramming.com...
| > I haven't covered classes yet so I'm not quite familiar with the workings
| > of the code snippet you have posted.
| >
| > I have the code written to iterate through a static four-dimensional
| > array. The array is initialized in the pyramid structure and the remaining
| > values are zero. It works properly and moves through them and I get the
| > proper results. I just need to get this dynamically allocated.
| >
| > Since I'm not really that familiar with classes isn't it possible to
| > initialize this array with loops and making pointers?
| >
| > If that is possible could you just show me the proper syntax to make a 4D
| > square/rectangular array using the method that I was using to make that 2D
| > dynamic array?
| >
|
| I'm confused about what you want, but here some code that allocates a 4D
| array. It's the same as your 2D code but expanded to 4 dimensions. I'm not
| sure what dimensions you actually want (some combination of C and n in your
| original post I think) so I've used D1, D2, D3 and D4 for the dimensions,
| you can substitute the values you want.
|
| double**** v;
| v = new double***[D1];
| for (int i = 0; i < D1; ++i)
| {
| v[i] = new double**[D2];
| for (int j = 0; j < D2; ++j)
| {
| v[i][j] = new double*[D3];
| for (int k = 0; k < D3; ++k)
| {
| v[i][j][k] = new double[D4];
| }
| }
| }
|
| But this doesn't work if you want a variable number of dimensions, which is
| what I thought you wanted. Nor does it allocate memory in a single block,
| which is another thing I thought you wanted. So I'm a bit confused.
|
| There's nothing special about classes, anything you can do inside a class
| you can also do outside a class. What classes do however is wrap up all your
| code in an easy to use package, something that might be quite useful here.

The OP may be much better off with nested vectors, wrapped
up in an class that provides suitable accessors. This would
allow for a variable number of dimensions, not to mention
ease the pain and danger of memory management on your own.

Cheers.
Chris Val
Jul 22 '05 #7
"fivelitermustang" <fi**************@shaw.ca> wrote:
Actually, how would I go about allocating a four-dimensional dynamic
array?


class Matrix4
{
std::vector<double> storage;
int bb;
int cc;
int dd;
public:
Matrix4( int a, int b, int c, int d ):
storage( a*b*c*d ), bb( b ), cc( c ), dd( d ) { }
double& operator()( int a, int b, int c, int d ) {
return storage.at( bb*cc*dd*a + cc*dd*b + dd*c + d );
}
};

You use it like this:

Matrix4 m( 1, 2, 3, 5 );

m( 0, 1, 0, 4 ) = 5.0;
assert( m( 0, 1, 0, 4 ) == 5.0 );
Jul 22 '05 #8
Daniel T. posted:

class Matrix4
{
std::vector<double> storage;
int bb;
int cc;
int dd;
public:
Matrix4( int a, int b, int c, int d ):
storage( a*b*c*d ), bb( b ), cc( c ), dd( d ) { }
double& operator()( int a, int b, int c, int d ) {
return storage.at( bb*cc*dd*a + cc*dd*b + dd*c + d );
}
};

You use it like this:

Matrix4 m( 1, 2, 3, 5 );

m( 0, 1, 0, 4 ) = 5.0;
assert( m( 0, 1, 0, 4 ) == 5.0 );

genius
-JKop

Jul 22 '05 #9
Thanks John... I tailored that snippet of code to suit my needs a little
bit better.

double**** highv;
v = new double***[n];
for (int i = 0; i < n; ++i)
{
v[i] = new double**[(i+1)*n];
for (int j = 0; j < (i+1)*n; ++j)
{
v[i][j] = new double*[C];
for (int k = 0; k < C; ++k)
{
v[i][j][k] = new double[n];
}
}
}

That above snippet should allocate a tree of matrices "v" which have a
dimension C*n. The tree will be n rows and each proceding row will have n
times what the row before had. Is my code correct?

How do I deallocate that memory too?

Thanks a lot for the ample help here... I'm gonna save this information
about classes. I'll then read up on that after I get the whole program
with my current method of allocating matrice. Then I'll attempt to clean
up the code with these classes.

I'm reading "C++ Programming: From Problem Analysis to Program Design" by
D.S. Malik by the way... seems to have some good information on classes.

Jul 22 '05 #10
Thanks John... I tailored that snippet of code to suit my needs a little
bit better.

double**** highv;
v = new double***[n];
for (int i = 0; i < n; ++i)
{
v[i] = new double**[(i+1)*n];
for (int j = 0; j < (i+1)*n; ++j)
{
v[i][j] = new double*[C];
for (int k = 0; k < C; ++k)
{
v[i][j][k] = new double[n];
}
}
}

That above snippet should allocate a tree of matrices "v" which have a
dimension C*n. The tree will be n rows and each proceding row will have n
times what the row before had. Is my code correct?

How do I deallocate that memory too?

Thanks a lot for the ample help here... I'm gonna save this information
about classes. I'll then read up on that after I get the whole program
with my current method of allocating matrice. Then I'll attempt to clean
up the code with these classes.

I'm reading "C++ Programming: From Problem Analysis to Program Design" by
D.S. Malik by the way... seems to have some good information on classes.

Jul 22 '05 #11

"fivelitermustang" <fi**************@shaw.ca> wrote in message
news:e7******************************@localhost.ta lkaboutprogramming.com...
Thanks John... I tailored that snippet of code to suit my needs a little
bit better.

double**** highv;
v = new double***[n];
for (int i = 0; i < n; ++i)
{
v[i] = new double**[(i+1)*n];
for (int j = 0; j < (i+1)*n; ++j)
{
v[i][j] = new double*[C];
for (int k = 0; k < C; ++k)
{
v[i][j][k] = new double[n];
}
}
}

That above snippet should allocate a tree of matrices "v" which have a
dimension C*n. The tree will be n rows and each proceding row will have n
times what the row before had. Is my code correct?
I don't see why you have (i + 1)*n, just this I think.

v[i] = new double**[n];
for (int j = 0; j < n; ++j)

How do I deallocate that memory too?

Just reverse the order of the new's, make sure the loop limits are the same
as when you allocated.

for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
for (int k = 0; k < C; ++k)
{
delete[] v[i][j][k];
}
delete[] v[i][j];
}
delete[] v[i];
}
delete[] v;
Thanks a lot for the ample help here... I'm gonna save this information
about classes. I'll then read up on that after I get the whole program
with my current method of allocating matrice. Then I'll attempt to clean
up the code with these classes.


Sounds reasonable.

john
Jul 22 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Gregory L. Hansen | last post by:
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...
4
by: kak3012 | last post by:
Hi, After my question today, I am not sure if this is the right group to ask but I have a simple question I guess. I have multi dimensional array called cover what I do is this iT GIVES AN...
15
by: Paul Morrison | last post by:
Hi all, I need to come up with some differences between arrays in Java and C, I have searched Google and so far all I have found is the following: Arrays in Java are reference types with...
60
by: Peter Olcott | last post by:
I need to know how to get the solution mentioned below to work. The solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below: >...
22
by: spam.noam | last post by:
Hello, I discovered that I needed a small change to the Python grammar. I would like to hear what you think about it. In two lines: Currently, the expression "x" is a syntax error. I suggest...
8
by: per9000 | last post by:
Hi all, I have a two-dimensional array of data, f.x int's. We can imagine that the array is "really large". Now I want the data in it and store this in a one-dimensional array. The obvious...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
2
by: erwinroy | last post by:
who to use the 2 dimensional array, dynamic arrays?? syntax??
11
by: gianluca | last post by:
Hy list, I've to declare a 3D matrix in C . I tried with that code: double ***mat; mat=(double***)G_malloc(ndimension*(sizeof(double)); but the program send me a run time error.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.