Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are vectors passed to functions by value or by reference in C++

Tags:

c++

arrays

vector

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.

like image 532
Adam Avatar asked Oct 30 '14 07:10

Adam


People also ask

How do you pass a vector to a function?

Passing a 2D Vector to the function Here the size of each vector in the 2d vector can be dynamic and different, whereas in 2D array this is not possible. A 2D vector can be passed to the function in the same way as 1D vector, using both: Pass By Value. Pass By Reference.

How do vectors work in C?

A vector is a type of array you find in object-oriented languages like C++. Like arrays, they can store multiple data values. However, unlike arrays, they cannot store primitive data types. They only store object references – they point to the objects that contain the data instead of storing the objects themselves.

Are arrays passed by value or reference in C?

Passing arrays to functions in C/C++ are passed by reference. Even though we do not create a reference variable, the compiler passes the pointer to the array, making the original array available for the called function's use. Thus, if the function modifies the array, it will be reflected back to the original array.

Can a function return a vector in C?

C programming does not allow to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array's name without an index.


1 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); } 
like image 75
JorenHeit Avatar answered Oct 04 '22 17:10

JorenHeit