Thread: Is c++ pass by refrence or value?

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    132

    Is c++ pass by refrence or value?

    I passed a std::vector object to a function that searched for a particular element and then deleted it. After much trouble, I realized c++ doesn't work the way I thought it did, and needed to add & to the functions signature. Why?

    Say you declare
    Code:
    std::vector<int> first;
    technically speaking is first an object, or pointer to an object? I assumed it was a pointer to an object and that's why even if c++ is pass by value then then when the function is called, the value (i.e. address) of the object is still the same.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Start with a really simple class, and attempt to pass instances of it to functions which
    - accept by value
    - accept by reference.

    The class you want to implement has constructors and destructors which print when they are invoked.
    Code:
    class foo {
      public:
        foo (  ) {
            cout << "ctor called for instance " << reinterpret_cast<void*>(this) << endl;
        }
    };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by c_weed View Post
    I passed a std::vector object to a function that searched for a particular element and then deleted it. After much trouble, I realized c++ doesn't work the way I thought it did, and needed to add & to the functions signature. Why?
    Arguments are passed by value in C++ by default, which means that a copy is passed.

    With a std::vector<whatever> is passed as an argument to a function, a copy of the whole vector is constructed, received by function, and guaranteed to be destroyed in code executed after the function returns.

    Passing by reference requires specific provision in the argument list (e.g. ampersand to pass by reference, asterisk to pass as a pointer).

    Quote Originally Posted by c_weed View Post
    Say you declare
    Code:
    std::vector<int> first;
    technically speaking is first an object, or pointer to an object? I assumed it was a pointer to an object and that's why even if c++ is pass by value then then when the function is called, the value (i.e. address) of the object is still the same.
    You've come from a C programming background, obviously.

    A std::vector<int> is an object in its own right. It is not a pointer. In fact, it is a data structure that might contain pointers, or it might not. Creating a copy of a std::vector<int> creates copies of everything relevant to the behaviour of that vector. The object is copied, as is the data contained by the object.

    Incidentally, the fact you are asking this question also demonstrates that your understanding of C is incomplete. An array in C is not a pointer either (although it is converted to a pointer when passed as an argument to a function). It is a common mistake - made by too many students and also teachers of C - to believe that an array and a pointer are equivalent. They are not.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Hmm, if you wouldn't mind taking the time to explain, grumpy, why is an array different than a pointer?

    Eh, I'll just go google it, I guess.

    But I'm not sure exactly why they're different. A naked pointer is rather bare but an array is a stack declared contiguous section of memory, with its zeroth index denoted by the name of the array.

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    A naked pointer is rather bare but an array is a stack declared contiguous section of memory, with its zeroth index denoted by the name of the array.
    O_o

    That is why they are different.

    An array isn't a pointer.

    If you want to know how they are different, write some example code using `sizeof'.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  6. #6
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by MutantJohn View Post
    Hmm, if you wouldn't mind taking the time to explain, grumpy, why is an array different than a pointer?

    Eh, I'll just go google it, I guess.

    But I'm not sure exactly why they're different. A naked pointer is rather bare but an array is a stack declared contiguous section of memory, with its zeroth index denoted by the name of the array.
    It's because an array is allocated memory in it's declaration. Also, a pointer can take the address of another variable. An array can access outside of it's bounds, but I don't think you can assign the address of another variable to it (ignoring if the array contains pointers).
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by MutantJohn View Post
    Hmm, if you wouldn't mind taking the time to explain, grumpy, why is an array different than a pointer?
    They are different because they are completely different things.

    An array is a set of elements of some type, stored contiguously. A pointer is a type of variable that, if initialised appropriately, contains the address in memory of something else. There is nothing in common between those definitions.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Any refrence
    By slow++ in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2009, 06:48 AM
  2. pass by refrence help.
    By Harman in forum C Programming
    Replies: 2
    Last Post: 05-11-2005, 08:27 PM
  3. refrence to a pointer
    By curlious in forum C++ Programming
    Replies: 3
    Last Post: 12-29-2003, 01:02 AM
  4. refrence to an array of int?
    By curlious in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 10:34 AM
  5. map pass by refrence?
    By curlious in forum C++ Programming
    Replies: 2
    Last Post: 09-28-2003, 10:11 AM

Tags for this Thread