tags:

views:

182

answers:

7

In C++ you can easily allocate one dimensional array like this:

T *array=new T[N];

And you can delete it with one statement too:

delete[] array;

The compiler will know the magic how to deallocate the correct number of bytes.

But why can't you alloc 2-dimensional arrays like this?

T *array=new T[N,M];

Or even like this?

T *array=new T[N,M,L];

If you want a multidimensional you have to do it like this:

T **array=new T*[N];
for(int i=0;i<N;i++) array[i]=new T[M];

If you want a fast program that uses matrices (matrix operations, eigenvalue algorithms, etc...) you might want to utilize the cache too for top performance and this requires the data to be in the same place. Using vector<vector<T> > is the same situation. In C you can use variable length arrays on the stack, but you can't allocate them on the heap (and stack space is quite limited), you can do variable length arrays in C++ too, but they won't be present in C++0x.

The only workaround is quite hackish and error-phrone:

T *array=new T[N*M];
for(int i=0;i<N;i++)
   for(int j=0;j<M;j++)
   {
       T[i*N+j]=...;
   }
+1  A: 

Because multidimensional array is something different then array of arrays/pointers.

Let_Me_Be
A: 

Because the comma is an operator.

int a = (3, 5, 7, 9);

The program will evaluate 3, discard the result, evaluate 5, discard the result, evaluate 7, discard the result, evaluate 9, and assign it to a.

Hence the syntax you are looking for can't be use, and retain backward compatibility to c.

EvilTeach
The comma operator has the lowest precedence of all the operators, so this is a syntax error. You'd need to say `int a = (3, 5, 7, 9);`.
Adam Rosenfield
The same syntax is used for function calls. If that can work, why not for array subscripts?
casablanca
@casablanca: Because function calls and array subscripts are different things, and the C++ designers chose not to allow it.
Dave Hinton
@Dave Hinton: Of course the question itself is about *why* the designers chose not to have such a feature.
casablanca
Ya. Thank you Adam.
EvilTeach
+1  A: 

use std::vector

mcuw
A: 

Why can't a multidimensional array be allocated with one new call in C++?

Because when the ISO wrote the C++ language standard, they didn't decide to add that feature to the language. I don't know why they decided not to.

If you don't like that, you can create helper functions to allocate/free multidimensional arrays, or you can switch to a language like C# or Java that does support easily allocating multidimensional arrays.

Adam Rosenfield
A: 

What you can do, however, is allocate an object containing a two-dimensional array off the heap. I would just write a wrapper class for it.

DeadMG
+7  A: 

Your workaround of doing T *array=new T[N*M]; is the closest you can get to a true multi-dimensional array. Notice that to locate the elements in this array, you need the value of M (I believe your example is wrong, it should be T[i*M+j]) which is known only at run-time.

When you allocate a 2D array at compile-time, say array[5][10], the value 10 is a constant, so the compiler simply generates code to compute i*10+j. But if you did new T[N,M], the expression i*M+j depends on the value of M at the time the array was allocated. The compiler would need some way to store the value of M along with the actual array itself, and things are only going to get messy from here. I guess this is why they decided not to include such a feature in the language.

As for your workaround, you can always make it less "hackish" by writing a wrapper class that overloads operator (), so that you could do something like array(i, j) = ....

casablanca
+1 for writing a wrapper class with appropriate operator overloading. This is what I did.
notJim
Oh, the () operator I forgot that I can overload it too. thx.
Calmarius
A: 

I was thinking about this question last night, and this solution came to me.

T * raw = new T[N*M];
T ** array = new T*[N];

for(int i=0; i<N; i++)
    array[i] = raw + i * M;

Now "array" acts just like a contiguous static sized two dimensional array. You just have to take care of deleting both the raw array, and the multi-dimensional array.

PigBen