Passing vectors

This is a discussion on Passing vectors within the C++ Programming forums, part of the General Programming Boards category; Are they passed by reference or by value as the default?...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    241

    Passing vectors

    Are they passed by reference or by value as the default?

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Regarding functions, everything is passed by value in C++ unless you signify otherwise in the function signature (by passing via a reference or pointer, etc.)

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    241
    aren't arrays passed by reference as default?

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Korn1699
    aren't arrays passed by reference as default?
    Ah, yes, I should have thought to mention that. My mistake!
    I guess if you wanna look at it like that, then yeah, arrays can be considered passed by reference, though I guess it's kind of debatable to call it that. The reason why arrays are in effect passed it by reference is because the signature of passing an array of a certain length is just interpretted as meaning a pointer to the element type of the array. So when you pass an array using a function such as

    void Foo( int array[5] );

    You're really neither passing the array by value OR by reference, but rather you end up just passing the first element of the array by reference (which is a subtle difference).

    void Foo( int (&array)[5] );

    would be a function passing the array by reference.

    The difference comes into the fact that the first example has no length data (nor data even saying you have to pass an array at all, despite its syntax).

    void Foo( int array[5] );

    is actually the same as

    void Foo( int* array );

    So you could even do

    int a;
    Foo( &a );

    Anyways, I'm just rambling because I said something wrong and I don't wanna seem completely ignorant I should have mentioned array in the first reply

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Originally posted by Korn1699
    aren't arrays passed by reference as default?
    Strictly speaking arrays aren't passed at all. You can't actually pass an array (not directly, anyway).

    When you pass an array as a function parameter what gets passed is a pointer to the first element of the array. This is still essentially pass by value, the function receives a copy of a pointer to the first element.

    I make the distinction here because it's important to recognise that passing a pointer as a function parameter is still pass by value. A copy of the pointer gets passed, not the pointer itself. For example, consider the following code:

    Code:
    #include <iostream>
    using namespace std;
    
    void func(int* iPtr)
    {
    	iPtr++;
    	iPtr++;
    }
    
    int main()
    {
    	int array[5] = {10,20,30,40,50};
    	int* p = array;
    	func(p);
    	cout << *p << endl;
    }
    What value will be output by the above? 10? 12? 30? Some other value?

    Not everybody will get the correct answer, because not everybody appreciates that a copy of the pointer is passed, not the actual pointer. It's still pass by value, not pass by reference.

    When people talk about passing an array as a function parameter as being pass by reference, they're using the term loosely. No problem with that, as long as it's understood what is meant. In C all parameter passing is pass by value. In C++, it's pass by value unless you choose otherwise.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 09:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 03:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 11:35 AM
  4. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 01:08 PM
  5. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 08:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21