0

When i use a string as a parameter to a method the changes to the string within the function are not not reflected back in the calling function. Simple reason Passing by value.

But when a use a vector the changes to the vector are reflected back in the calling function. So my question here is does passing vectors is like call by reference where we are sending the address in memory rather than the variable hence any changes within the functions re reflected back in calling function

------------------------Update--------------------------------

so vectors have to be passed by reference only? i can't do it normally?

of course you can pass them normally (that is pass them by value). but when you pass objects by value what happens is that a new object is created and then the object you pass is copied to the new object. this, for standard types (char, int, float etc) is ok, because standard types are small in size (char is one byte, short is 2, int is 2 or 4, long is 4 etc...). a vector however is not that small. Try doing this:

1
2
3
4
5
6
7
8
9
10
11
12

#include <iostream>
#include <vector>
using namespace std;

int main()
{
     vector<int> v;
     cout << sizeof(v) << endl;

     system("pause");
     return 0;
}

in my pc I get 12 as an output. that's 12 bytes. so instead of passing by value, I pass it by reference which has a cost of 4 bytes (the size of the variable holding the address of my vector)

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Maclean Pinto
  • 1,075
  • 2
  • 17
  • 39

3 Answers3

5

But when a use a vector the changes to the vector are reflected back in the calling function.

This is because of passing by reference. If you pass a std::string by reference, you would get the same effect.

The inverse is true as well: you can pass a std::vector by value if you omit the ampersand in the parameter declaration. This may carry a high cost, though, because the entire content of the vector will be copied.

so vectors have to be passed by reference only?

They don't have to be passed by reference, but in many cases passing by value is simply too expensive, so it's rare for people to do it. As far as the sizeof(v) is concerned, you get only a small footprint of the vector that's allocated with the vector object itself (in this case, it's on the stack). There is also a dynamic portion pointed to by the pointers inside the vector, which is not accounted for by the sizeof operator.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • +1, for "They(std::vectors) don't have to be passed by reference, but in many cases passing by value is simply too expensive" especially. If I have a vector and need some object to be able to use it, I'll usually just pass the vector by reference once and keep a handle to it. Even if it's infrequent, I'll probably avoid passing by value, as I'm not going to copy something that may be 50mb even. – Austin J. Feb 26 '14 at 05:15
1

I wanted to take the time to point out that String is a special case. String is a final class which means it is immutable and cannot be modified.

1

Assuming this is really a Java question:

Both String and Vector types are passed by value (but the value passed is actually a reference). Consider:

void foo1 (String s) {
    s = new String("abcde");
}

void foo2 (Vector<Integer> v) {
    v = new Vector<Integer> ();
}

In neither case is the caller's parameter affected. Both of these will modify a local copy of the object reference that was passed, but the don't do anything to the object that was referred to.

If you do something that modifies the vector, though:

void foo3 (Vector<Integer> v) {
    v.add (10);
}

this modifies the object that v points to. If there were a method that modified a String, like this:

void foo4 (String s) {
    s.setChar(0, '!');  // does not exist in Java
        // but pretend that it changes the first character of the string
} 

then that would also modify the object that the caller passed to foo4. But I can't give a real example of this, because String objects are immutable in Java.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
ajb
  • 31,309
  • 3
  • 58
  • 84