Till now most of the time I was using functions that return pointer to array, but now I started using void function with reference to array, so I am wondering which one of below code is better to use and why?
void doSomething(int** &ary)
{
ary = new int*[3];
for (int i = 0; i < 3; ++i)
ary[i] = new int[3];
ary[0][0] = 1;
ary[0][1] = 2;
}
int** ary=NULL;
doSomething(ary);
or this
int** doSomething1()
{
int **ary = new int*[3];
for (int i = 0; i < 3; ++i)
ary[i] = new int[3];
ary[0][0] = 1;
ary[0][1] = 2;
return ary;
}
int **ary1=doSomething1();
std::vectoruntil you have a real reason to do otherwise. Then, switch to C++ 11 move semantics.