Another question about multi dimensional arrays

I heard that cint supports dim<=3, but I’m having problems with dim=3, writing it simple:

class x {
private:
Float_t y***;
}

constructor:
y = new Float_t [10][10][2];

error on compiling:
cannot convert double (*)[10][2]' to
double***’ in assignment

Thanks,
G

This is illegal C++:root [0] .L a.C+ Info in <TUnixSystem::ACLiC>: creating shared library /var/tmp/./a_C.so /var/tmp/./a.C: In constructor 'x::x()': /var/tmp/./a.C:10: error: cannot convert 'int (*)[10][2]' to 'int***' in assignmentis the error produced by g++.

Cheers,
Philippe

I’ve checked, and same is true for bi-dimensional arrays:

Float_t **x;
x=new Float_t[10][10];

Somebody know any way of dynamic allocating multi-dimensional arrays?

Thanks,
G

one can do:

float *x
x=new float
[2]
x[0]=new float[2]
x[1]=new float[2]

there is another way, smarter than that?

The best way is to use nested vector (vector<vector > ). However you will need to generate the dictionary for those types and/or compile your code (easy to do with ACLiC).

Cheers,
Philippe.

[quote]I’ve checked, and same is true for bi-dimensional arrays:

Float_t **x;
x=new Float_t[10][10];
Thanks,
[/quote]

Yes, because you programm is ill-formed.
expression new float[10][10] returns float ()[10] which is a pointer to array of ten floats, not a pointer to pointer to float.
new float[10][10][10] will return float(
)[10][10] - pointer to an array of 10 arrays of 10 floats.

I guess, you see the difference between float (*)[10] and float ** - these are completely different types.

Usually, two dimensional array with float ** created in two steps:

float ** ptr = new float *[10];
for(int i = 0; i < 10; ++i)
ptr[i] = new float[10];
//It’s just an example, it’s not good way to programm in C++.

When you write now

ptr[0][0] = 1.f;

this means

((ptr + 0) + 0) = 1.f; - i.e. you have a pointer to the 0th elementh of array of pointers, you dereference it - *(ptr + 0) or ptr[0] and this expression gives you 0th element in an array of pointers. This pointer points to 0th element of array of floats. After that, you dereference this pointer - ptr[0][0] or ((ptr + 0) + 0) and you get 0th element of array of floats.

Now:

float (*ptr)[10] = new float[10][10];

this is completely different situation.

ptr[0] gives you an array of 10 floats, ptr[1] will give you NEXT array of 10 floats etc.

So:

float ** ptr = ///code from the first sample

ptr holds address of 0th elementh of such array

[][][][][][][][][][] //sheme :slight_smile:
^ptr

float (*ptr)[10] = ///the second sample

[ [float][float][float][float][float][float][float][float][float][float] ][[.etc/
^ptr

PS AFAIK now cint cannot work correctly with multidimensional dynamic arrays created this way.

[quote]The best way is to use nested vector (vector<vector > ). However you will need to generate the dictionary for those types and/or compile your code (easy to do with ACLiC).

Cheers,
Philippe.[/quote]

I guess, modern physicist should use valarray and slices to model multy-dimensional arrays and get speed optiomization of valarrays:))