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函数引用数组,所以我想知道下面哪个代码更好用,为什么?
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();
2 个解决方案
#1
1
This is a question of opinion, however, here is mine:
这是一个意见问题,但是,这是我的:
I think the version that returns the pointer is better. Why? Simply because it makes the calling site less magic. With the return variant, you call the function like this:
我认为返回指针的版本更好。为什么?仅仅因为它使调用网站不那么神奇。使用返回变量,您可以像这样调用函数:
int** my2dArray = doSomething();
It is perfectly clear, that my2dArray
is initialized to point to some array that the function supplies. Without a single look at the function definition.
很明显,my2dArray被初始化为指向函数提供的某个数组。没有一看功能定义。
On the other hand, the call
另一方面,电话
int** my2dArray;
doSomething(my2dArray);
should always ring an alarm bell for the reader: It looks as if you are passing an uninitialized pointer to the function. Even after looking up the function definition, and seeing that the pointer is passed by reference, a reader still can't be certain that the function does not interpret the value that is passed in. It requires a close look at the code to make sure that this call is indeed legitimate.
应始终为读者敲响警钟:看起来好像是在向函数传递一个未初始化的指针。即使在查找函数定义并看到指针通过引用传递之后,读者仍然无法确定该函数不会解释传入的值。它需要仔细查看代码以确保这个电话确实合法。
So, for the sake of debugability, I avoid passing reference arguments. I pass by value, I pass by const
reference (which has the same semantic as pass by value), or I pass by explicit pointer. That way no function call can modify a value that is not explicitly visible at the calling site.
因此,为了可调试性,我避免传递引用参数。我传递值,我传递const引用(它具有与传递值相同的语义),或者我通过显式指针传递。这样,没有函数调用可以修改在调用站点上不明显可见的值。
#2
0
There is no big difference between the two examples, however I'd prefer the second example.
两个例子之间没有太大的区别,但是我更喜欢第二个例子。
In the first example, you take a reference to ary
, which means your program has to dereference all pointers each time it accesses an element somewhere in ary
. (A reference is equivalent to a pointer after the code has been compiled)
在第一个示例中,您引用了ary,这意味着您的程序每次访问ary中的某个元素时都必须取消引用所有指针。 (引用等同于代码编译后的指针)
In the second example, you only need to copy the pointer when the function returns.
在第二个示例中,您只需要在函数返回时复制指针。
First example:
第一个例子:
reference to pointer to array -> pointer to array -> element in array
引用指向数组的指针 - >指向数组的指针 - >数组中的元素
Second example:
第二个例子:
pointer to array -> element in array
指向数组的指针 - >数组中的元素
#1
1
This is a question of opinion, however, here is mine:
这是一个意见问题,但是,这是我的:
I think the version that returns the pointer is better. Why? Simply because it makes the calling site less magic. With the return variant, you call the function like this:
我认为返回指针的版本更好。为什么?仅仅因为它使调用网站不那么神奇。使用返回变量,您可以像这样调用函数:
int** my2dArray = doSomething();
It is perfectly clear, that my2dArray
is initialized to point to some array that the function supplies. Without a single look at the function definition.
很明显,my2dArray被初始化为指向函数提供的某个数组。没有一看功能定义。
On the other hand, the call
另一方面,电话
int** my2dArray;
doSomething(my2dArray);
should always ring an alarm bell for the reader: It looks as if you are passing an uninitialized pointer to the function. Even after looking up the function definition, and seeing that the pointer is passed by reference, a reader still can't be certain that the function does not interpret the value that is passed in. It requires a close look at the code to make sure that this call is indeed legitimate.
应始终为读者敲响警钟:看起来好像是在向函数传递一个未初始化的指针。即使在查找函数定义并看到指针通过引用传递之后,读者仍然无法确定该函数不会解释传入的值。它需要仔细查看代码以确保这个电话确实合法。
So, for the sake of debugability, I avoid passing reference arguments. I pass by value, I pass by const
reference (which has the same semantic as pass by value), or I pass by explicit pointer. That way no function call can modify a value that is not explicitly visible at the calling site.
因此,为了可调试性,我避免传递引用参数。我传递值,我传递const引用(它具有与传递值相同的语义),或者我通过显式指针传递。这样,没有函数调用可以修改在调用站点上不明显可见的值。
#2
0
There is no big difference between the two examples, however I'd prefer the second example.
两个例子之间没有太大的区别,但是我更喜欢第二个例子。
In the first example, you take a reference to ary
, which means your program has to dereference all pointers each time it accesses an element somewhere in ary
. (A reference is equivalent to a pointer after the code has been compiled)
在第一个示例中,您引用了ary,这意味着您的程序每次访问ary中的某个元素时都必须取消引用所有指针。 (引用等同于代码编译后的指针)
In the second example, you only need to copy the pointer when the function returns.
在第二个示例中,您只需要在函数返回时复制指针。
First example:
第一个例子:
reference to pointer to array -> pointer to array -> element in array
引用指向数组的指针 - >指向数组的指针 - >数组中的元素
Second example:
第二个例子:
pointer to array -> element in array
指向数组的指针 - >数组中的元素