Thread: A question about passing arguments

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    13

    Question A question about passing arguments

    Hi all, I just start learning c++ and I'm a little bit confused with passing parameters to function. hope you can help me out

    Check the example:
    Code:
    //definition of a function
    vector<int> sub_vec( const vector<int> &vec, int val )
    {
           vector<int> local_vec( vec );   
            //do operations on local_vec,
           ......
    }
    to get a local copy of vec which is passed from the caller function, can I just instead write
    Code:
    vector<int> sub_vec( vector<int> local_vec, int val) 
    {   
            //do operations on local_vec;
            ......
    }
    in the second case, what is passed? an address? the whole object "
    vec"? when passing parameters, what is actually processed? an assignation (then second case is illegal, cuz local_vec may not be big enough) or just an initialization?

    Thanks for help

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    In the second case a copy of the vector is made. In the first, a reference to the vector is passed.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    13
    Thanks.
    so the result of the second case is exactly same as the result of the first one?

  4. #4
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    This is better to do
    Code:
    //definition of a function
    vector<int> sub_vec( const vector<int> &local_vec, int val )
    {
            //do operations on local_vec,
           ......
    }
    ...I think

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by AntiScience View Post
    Thanks.
    so the result of the second case is exactly same as the result of the first one?
    In both cases, a copy of the vector is made. The difference is WHEN the copy is made. In the first version (pass by reference), you make the copy manually, once you've already entered the function. In the second version (pass by value), the copy is made automatically when the argument is passed, BEFORE you enter the function.

    I'd say the two versions are equivalent. But I would prefer to pass by reference for the following reason: your code may change so that you no longer need to operate on a copy of the vector. In the pass-by-reference case, you simply stop making the copy. But in the pass-by-value case, the copy is still made, even if you don't need it. And at that point, if you change the function to take a reference instead of a value, you break the interface for all users of that function (not at a compile-level -- it would still compile. But users may have come to rely on the pass-by-value semantics in some way)

    I would only pass by value if the object was sufficiently "simple" -- a vector isn't.
    Last edited by brewbuck; 10-16-2007 at 02:18 PM.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The two code snippets you posted are functionally identical. In both cases, local_vec is a member-by-member copy of the original vector.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Both code snippets make a copy, and they do the same thing. Either choice will be fine.

    You can use the first one (with the reference) in case you might change the implementation of the function later so that it doesn't require a copy. If you make it a reference from the start, calling code won't have to be recompiled for the change.

    You can use the second one if you know you will always make a copy and you want to document that by doing the copy directly on the parameter.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by AntiScience View Post
    btw, the first definition is an example in lippman's book essential c++. what he wanted is to change the local_vec without changing the original vec. I posted this thread because I thought there must be something wrong with the second definition for which he didn't use the shorter one in the book....
    There's nothing really wrong with it, it is just less robust against changes in the implementation. "Brittle," if you will.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by AntiScience View Post
    Thanks.
    so the result of the second case is exactly same as the result of the first one?
    Depends on exactly what you mean by "exactly same"

    Creating a copy of a large object, such as a vector with a considerable number of elements that each consume significant amounts of memory, imposes a performance and memory penalty. That can translate into the second form introducing performance penalties in comparison with the first: it can run slower or, if the code is executing on a machine with limited memory resources, the copying operation can fail.

    The second form also relies on being able to create a copy. That means objects contained in the vector must support suitable copy semantics (copy constructor, assignment operator) to ensure the elements are copied as expected. That is normally a requirement when using a vector, but does actually depend on how the class of objects is implemented.

    In practice, you are better off passing a const reference for reasons like these: fewer potential penalties on performance and/or memory usage and lower likelihood of encountering a run-time error if machine resources are limited.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    13
    Quote Originally Posted by brewbuck View Post
    There's nothing really wrong with it, it is just less robust against changes in the implementation. "Brittle," if you will.
    Thanks. your answer is really helpful
    and Thanks to all too

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> This is better to do ...I think
    >> In the second case a copy of the vector is made. In the first, a reference to the vector is passed.
    >> In practice, you are better off passing a const reference for reasons like these.

    In both cases a copy is made, the question was whether it is better to do that in the function parameter list or inside the body of the function.


    BTW, another reason to do it inside the body is that you might add code later to exit the function early on some error condition. If you can do that before making the copy then you can gain the performance benefit of skipping the copy only if the copy is inside the function body.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Values Question... (continue)
    By tinkerbell20 in forum C++ Programming
    Replies: 4
    Last Post: 06-29-2005, 01:51 AM
  2. Passing Values Question...
    By tinkerbell20 in forum C++ Programming
    Replies: 9
    Last Post: 06-27-2005, 11:33 AM
  3. passing arguments to functions
    By Micko in forum C Programming
    Replies: 4
    Last Post: 07-18-2004, 10:59 AM
  4. Question about Templates and passing arguments
    By supaben34 in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2002, 01:32 AM
  5. Variable Arguments Question
    By moonwalker in forum C Programming
    Replies: 8
    Last Post: 08-04-2002, 09:08 AM