vendredi 15 novembre 2019

Is there a way to initialize a 3D array like this in C++?

I was trying to initialize memory in c++ with chain of pointers.But when I tried something different the code crashed.I don't understand why won't this work. Please someone could explain this. In the main() I write something like,

double ***dArr = NULL;
dArr = new double**[5];
for(int i=0;i<5;i++)
{
    arr[i] = new double*[5]);
    for(int j=0;j<5;j++)
    {
        arr[i][j] = new double[5]);
    }
}
//But if I do this it crashes
typedef double*** D;
D arr;
arr = *(new D[5]); 
for(int i=0;i<5;i++)
{
    arr[i] = **(new D[5]);    //why cant I do this?Compiler is OK with this but asserts.
    for(int j=0;j<5;j++)
    {
        arr[i][j] = ***(new D[5]);
    }
}

Aucun commentaire:

Enregistrer un commentaire