I'm coding in C++. If I have some function void foo(vector<int> test)
and I call it in my program, will the vector be passed by value or reference? I'm unsure because I know vectors and arrays are similar and that a function like void bar(int test[])
would pass test in by reference (pointer?) instead of by value. My guess is that I would need to pass the vector by pointer/reference explicitly if I wanted to avoid passing by value but I'm not sure.
-
5C++ has "pass by value" semantics, so it is passed by value. You can decide to pass by reference though. The choice really depends on what the function does. – juanchopanza Oct 30 '14 at 07:19
-
2use by reference, if the you dont want function to change vector contents make it const, other wise just pass it by reference. It will avoid un-wanted and costly (sometimes) copy – Ali Kazmi Oct 30 '14 at 07:22
-
3Also, a reference is not a pointer. – The Paramagnetic Croissant Oct 30 '14 at 07:23
-
2@AliKazmi Except if you want a copy in the body of the function. – juanchopanza Oct 30 '14 at 07:28
-
It depends on what you want to achieve. Using one of the standard smart pointers is sometimes useful, for example if you want to pass on ownership, then use unique_ptr. But usually, I pass std::vector by reference. – Erik Alapää Oct 30 '14 at 08:16
4 Answers
In C++, things are passed by value unless you specify otherwise using the &
-operator (note that this operator is also used as the 'address-of' operator, but in a different context). This is all well documented, but I'll re-iterate anyway:
void foo(vector<int> bar); // by value
void foo(vector<int> &bar); // by reference (non-const, so modifiable inside foo)
void foo(vector<int> const &bar); // by const-reference
You can also choose to pass a pointer to a vector (void foo(vector<int> *bar)
), but unless you know what you're doing and you feel that this is really is the way to go, don't do this.
Also, vectors are not the same as arrays! Internally, the vector keeps track of an array of which it handles the memory management for you, but so do many other STL containers. You can't pass a vector to a function expecting a pointer or array or vice versa (you can get access to (pointer to) the underlying array and use this though). Vectors are classes offering a lot of functionality through its member-functions, whereas pointers and arrays are built-in types. Also, vectors are dynamically allocated (which means that the size may be determined and changed at runtime) whereas the C-style arrays are statically allocated (its size is constant and must be known at compile-time), limiting their use.
I suggest you read some more about C++ in general (specifically array decay), and then have a look at the following program which illustrates the difference between arrays and pointers:
void foo1(int *arr) { cout << sizeof(arr) << '\n'; }
void foo2(int arr[]) { cout << sizeof(arr) << '\n'; }
void foo3(int arr[10]) { cout << sizeof(arr) << '\n'; }
void foo4(int (&arr)[10]) { cout << sizeof(arr) << '\n'; }
int main()
{
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
foo1(arr);
foo2(arr);
foo3(arr);
foo4(arr);
}

- 3,877
- 2
- 22
- 28
-
2Can anybody help me understand the last declaration foo4 function ? Why does it return the size of array instead of size of pointer as in other functions ? – abhishek_M May 03 '17 at 12:51
-
5@abhishek_M because the parameter type is a reference. A reference can be bound only to a expression of the exact same type. For this reason the array doesn't decay, because a reference to an array can't be bound to a pointer, it can only be bound to an array of the same type (in this case array of 10 integers) – bolov Aug 02 '17 at 13:38
-
1c++11 has "move semantics" and you can do it carefully and pass by value (with move semantics) without copy. Here is a link that helped me a lot: https://mbevin.wordpress.com/2012/11/20/move-semantics/ – fchen Aug 16 '18 at 20:02
-
1What is the purpose of/thinking behind `foo3`'s behavior from a language design point of view? Why is decay necessary in this context when the size of the array is known at compile time? – QuaternionsRock Aug 04 '21 at 03:19
-
@bolov Hey, in the foo3 function, I can also pass a parameter with foo3(int arr[0]) and still it is valid so what's the difference... – codosopher Apr 29 '23 at 09:06
-
1@codosopher `foo1` `foo2` and `foo3` are 100% equivalent. In all three functions the parameter `arr` is of type pointer. The fact that the declaration in `foo2` and `foo3` appears to be an array as well as the "array size" here are completely ignored. It's a quirk inherited from `C`. – bolov Apr 29 '23 at 09:14
-
-
@bolov Hey, what's the difference between &arr, *(&arr), arr, &arr[0] because all are giving same address but I think, the meaning is different... – codosopher Apr 29 '23 at 09:43
-
@codosopher don't use C arrays in C++. As for all those it depends if they are normal expressions or declarations and if they are parameter declaration. – bolov Apr 29 '23 at 09:59
-
@bolov I am just experimenting with the editor by printing those values inside the main function to understand the meaning of those values. So, those are just normal expressions. cout<<&arr<
– codosopher Apr 29 '23 at 10:09 -
Here `&` is the address of operator. `*` is the dereference operator. If the type passed to `cout` is an array type then it decays to a pointer to the first element of the array. `*(&arr + 1)` is Undefined behavior. Both the adress of an array and the adress of the first element of the array when printed show the same value (because both objects start at the same adress) – bolov Apr 29 '23 at 10:29
-
@bolov I think, &arr and *(&arr) are same. Likewise, (&arr + 1) and *(&arr + 1). &arr gives the address of an array arr[]. – codosopher Apr 29 '23 at 10:51
-
-
@bolov https://godbolt.org/z/985eKsPs8 can you also tell the meaning of this? – codosopher Apr 29 '23 at 13:22
-
1the comments are for clarifications on the question/answer. We are going way off topic here. You can ask a question. – bolov Apr 29 '23 at 22:34
A vector
is functionally same as an array. But, to the language vector
is a type, and int
is also a type. To a function argument, an array of any type (including vector[]
) is treated as pointer. A vector<int>
is not same as int[]
(to the compiler). vector<int>
is non-array, non-reference, and non-pointer - it is being passed by value, and hence it will call copy-constructor.
So, you must use vector<int>&
(preferably with const
, if function isn't modifying it) to pass it as a reference.

- 18,086
- 12
- 59
- 105
-
What is the syntax for passing an "array of vectors" by reference to a function? – ab123 Nov 09 '17 at 13:01
-
void foo(vector<int> test)
vector would be passed by value in this.
You have more ways to pass vectors depending on the context:-
1) Pass by reference:- This will let function foo change your contents of the vector. More efficient than pass by value as copying of vector is avoided.
2) Pass by const-reference:- This is efficient as well as reliable when you don't want function to change the contents of the vector.
when we pass vector by value in a function as an argument,it simply creates the copy of vector and no any effect happens on the vector which is defined in main function when we call that particular function. while when we pass vector by reference whatever is written in that particular function, every action will going to perform on the vector which is defined in main or other function when we call that particular function.

- 199
- 2
- 5