C++ 将动态数组传递给其他函数的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13006250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Proper way to pass dynamic arrays to other functions
提问by noko
What's the most "proper" way to pass a dynamically sized array to another function?
将动态大小的数组传递给另一个函数的最“正确”方法是什么?
bool *used = new bool[length]();
I've come up with a few ways that compile but I'm not too sure on what the correct way is.
我想出了几种编译方法,但我不太确定正确的方法是什么。
E.g.
例如
Would these pass by value?
这些会通过价值传递吗?
static void test(bool arr[])
static void test(bool *arr)
Would this one pass by reference?
这个会通过引用传递吗?
static void test(bool *&arr)
Thanks
谢谢
回答by alestanis
Actually, the two first ideas pass the array by addressand the third passes the array by reference. You can devise a little test to check this:
实际上,第一个想法通过地址传递数组,第三个想法通过引用传递数组。您可以设计一个小测试来检查这一点:
void test1(int* a) {
a[0] = 1;
}
void test2(int a[]) {
a[1] = 2;
}
void test3(int *&a) {
a[2] = 3;
}
int main() {
int *a = new int[3]();
a[0] = 0;
a[1] = 0;
a[2] = 0;
test1(a);
test2(a);
test3(a);
cout << a[0] << endl;
cout << a[1] << endl;
cout << a[2] << endl;
}
The output of this test is
这个测试的输出是
1
2
3
If a parameter is passed by value, it cannot be modified inside a function because the modifications will stay in the scope of the function. In C++, an array cannot be passed by value, so if you want to mimic this behaviour, you have to pass a const int*
or a const int[]
as parameters. That way, even if the array is passed by reference, it won't be modified inside the function because of the const
property.
如果参数是按值传递的,则不能在函数内部对其进行修改,因为修改将保留在函数范围内。在 C++ 中,数组不能按值传递,因此如果您想模仿这种行为,您必须将 aconst int*
或 aconst int[]
作为参数传递。这样,即使数组是通过引用传递的,它也不会因为const
属性而在函数内部被修改。
To answer your question, the preferred way would be to use a std::vector
, but if you absolutely want to use arrays, you should go for int*
.
要回答您的问题,首选方法是使用 a std::vector
,但如果您绝对想使用数组,则应该使用int*
.
回答by john
You're right. The first two are equivalent and pass a pointer by value. Stylistically the second is preferred as it describes the situation accurately, i.e. you are passing a pointer to your function. The first is a kind of hangover for people who can't quite believe that you can't pass arrays in C++. There is no way to pass an array by value in C++. The third passes a pointer by reference.
你是对的。前两个是等价的,并按值传递一个指针。从风格上讲,第二个是首选,因为它准确地描述了情况,即您正在传递一个指向您的函数的指针。对于那些不太相信你不能在 C++ 中传递数组的人来说,第一种是一种宿醉。在 C++ 中无法按值传递数组。第三个通过引用传递一个指针。
There's a confusion here in that in all cases the pointer 'refers' to your array. So when talking about pass by value or pass by reference you should be clear whether you are speaking about the pointer or the array it refers to.
这里有一个混淆,在所有情况下,指针都“引用”到您的数组。因此,在谈论按值传递或按引用传递时,您应该清楚您是在谈论指针还是它所指的数组。
回答by ForEveR
static void test(bool arr[])
static void test(bool *arr, size_t size)
For static/dynamic arrays, if you don't want to change location of this pointer.
对于静态/动态数组,如果您不想更改此指针的位置。
Example: http://liveworkspace.org/code/c5e379ebe2a051c15261db05de0fc0a9
示例:http: //liveworkspace.org/code/c5e379ebe2a051c15261db05de0fc0a9
static void test(bool *&arr)
For dynamic if you want to change location.
如果要更改位置,则为动态。
Example: http://liveworkspace.org/code/bd03b214cdbe7c86c4c387da78770bcd
示例:http: //liveworkspace.org/code/bd03b214cdbe7c86c4c387da78770bcd
But, since you write on C++ - use vectors, instead of raw dynamic arrays.
但是,由于您使用 C++ 编写 - 使用向量,而不是原始动态数组。
回答by Mark Garcia
Use this:
用这个:
void myFuncThatAcceptsDynamicArrays(bool* array, int size) {
// Do something (using the size as the size of the array)
}
It is up to the user of the function to provide a valid size (which can be very dangerous).
由函数的用户提供有效的大小(这可能非常危险)。
回答by AgA
I'd always use vector for dynamic sized arrays. In all cases arrays in C++ are passed by reference since their pointer address only is passed. There is no primitive way to pass by value in case of arrays.
我总是将向量用于动态大小的数组。在所有情况下,C++ 中的数组都是通过引用传递的,因为只传递它们的指针地址。在数组的情况下,没有按值传递的原始方法。