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
1) Passing array as pointer to pointer( int **arr)Using new operator we can dynamically allocate memory at runtime for the array. New operator returns the address of the space allocated . This method Passes array reference as double pointer to the function along with rows and columns.
Bounded-size dynamic arrays and capacityElements can be added at the end of a dynamic array in constant time by using the reserved space, until this space is completely consumed. When all space is consumed, and an additional element is to be added, then the underlying fixed-size array needs to be increased in size.
To pass an entire array to a function, only the name of the array is passed as an argument. result = calculateSum(num); However, notice the use of [] in the function definition. This informs the compiler that you are passing a one-dimensional array to the function.
void func(int **board) { for (int i=0; i<boardsize; ++i) { board[i] = new int [size]; } } func(board);
Actually, the two first ideas pass the array by address and 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.
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*
.
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.
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.
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
static void test(bool *&arr)
For dynamic if you want to change location.
Example: http://liveworkspace.org/code/bd03b214cdbe7c86c4c387da78770bcd
But, since you write on C++ - use vectors, instead of raw dynamic arrays.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With