samedi 3 octobre 2015

C++: Is new int[][] a valid thing to do?

I have come across some code which allocates a 2d array with following approach:

auto a = new int[10][10];

Is this a valid thing to do in C++? I have search through several C++ reference books, none of them has mentioned such approach. Normally I would have done the allocation manually as follow:

int  **a = new int *[10];
for (int i = 0; i < 10; i++) {
    a[i] = new int[10];
}

If the first approach is valid, then which one is preferred?

Aucun commentaire:

Enregistrer un commentaire