Newbie: Allocate 2 dimentional-array

  • Thread starter Thread starter Hai Ly Hoang
  • Start date Start date
H

Hai Ly Hoang

Hi,
With these code:
int a[3][5];
we are allocated (static) an array 3-by-5 (but continous) elements.

But how to allocated dynamically an 3x5 array ?
The way i know is:
a = new int *[3];
a[0] = new int[5];
a[1] = new int[5];
a[2] = new int[5];
Too complex for a simple task ! and the allocated space is not continous !
Anyone know the way to allocated dynamic an array with continous space in
C++ ? (in pascal, it's quite easy to do such thing ).

Thanks in advance !
 
But, why would you want to allocate memory dynamically, if you know the
upperbounds of the array you need at design time itself.
 
"Jagadeesh":
But, why would you want to allocate memory dynamically, if you know the
upperbounds of the array you need at design time itself.

If 3x5 size of the array is MxN which M and N are variable. We have no
choice but allocated dynamicly ! However, i respect your attention and your
help !

Any one can give me other suggest ?
Thanks
 
Hai Ly Hoang said:
"Jagadeesh":

If 3x5 size of the array is MxN which M and N are variable. We have no
choice but allocated dynamicly ! However, i respect your attention
and your help !

Any one can give me other suggest ?
Thanks

If the number of columns is a compile time constant, then you can do it as
follows:

const int const_N = 10;
int M = 5;
int (*matrix)[const_N] = new int[M][const_N];

If, however, the number of columns is not a compile-time constant, then you
have a problem. To define a pointer to an array, the length of the array
must be a compile-time constant. That is a language restriction and there is
no way around it (that I know of). Accordingly, you cannot declare

int (*matrix)[N]

if N is not a compile-time constant.

This leaves two possibilities:

1. You can do more or less what you suggested in your original post. You can
have the variables stored contiguously if you want by use of placement new,
but you will still need one pointer per row.

2. You can use a class for multi-dimensional arrays rather than using what
is essentially a C-style array model. The Boost MultiArray class provides an
example of this.

http://www.boost.org/libs/multi_array/doc/index.html
 
Hi,
You can use a MultiArray from boost (www.boost.org) or allocate a one
dimensional array and do a little math.
Anyway, your code should not depend on the fact that the data MUST be in
continuous spaces, because it will make it not portable (and you should not
play with the compiler pack to achieve this.)
It is a common believe that, if data is in continuous spaces, it will be
accessed faster, but this is not always true.

Lucas/
 
Hai Ly Hoang said:
Hi,
With these code:
int a[3][5];
we are allocated (static) an array 3-by-5 (but continous) elements.

But how to allocated dynamically an 3x5 array ?
The way i know is:
a = new int *[3];
a[0] = new int[5];
a[1] = new int[5];
a[2] = new int[5];
Too complex for a simple task !

Indeed.

typedef std::vector<int> int_vec;
typedef std::vector<int_vec> int_vec_vec;

int_vec_vec ivv( 3, int_vec(5) );
and the allocated space is not continous !
Anyone know the way to allocated dynamic an array with continous space in
C++ ? (in pascal, it's quite easy to do such thing ).

Multi-dimensional arrays that store their
data in continious space aren't part of the
C++ std lib. So you either roll your own
or find one. (www.boost.org)
Thanks in advance !

Schobi

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
 
Back
Top