+ 2
How to make 2d c-style arrays with unique_ptr?
To create a simple array i'd do!! std::unique_ptr<int[]> arr(new int[100]); How can i create a 100*100 2d array using a unique ptr??
1 Odpowiedź
+ 3
Like you would do with classic heap arrays:
int ** a = new int*[100];
for (int i = 0; i < 100; i++)
{
a[i] = new int[100];
}
the unique_ptr just acts like your regular old pointer (just with fancy syntax)
std::unique_ptr<std::unique_ptr<int[]>[]> arr(new std::unique_ptr<int[]>[100]);
for (int i = 0; i < 100; i++)
{
arr[i] = std::unique_ptr<int[]>(new int[100]);
}
Follow up question: why tho?