Performance cost of passing by value vs. by reference or by pointer?

It depends on what you mean by “cost”, and properties of the host system (hardware, operating system) with respect to operations. If your cost measure is memory usage, then the calculation of cost is obvious – add up the sizes of whatever is being copied. If your measure is execution speed (or “efficiency”) then the … Read more

Is Java really passing objects by value? [duplicate]

Java always passes arguments by value, NOT by reference. In your example, you are still passing obj by its value, not the reference itself. Inside your method changeName, you are assigning another (local) reference, obj, to the same object you passed it as an argument. Once you modify that reference, you are modifying the original … Read more

What exactly is the difference between “pass by reference” in C and in C++?

There are questions that already deal with the difference between passing by reference and passing by value. In essence, passing an argument by value to a function means that the function will have its own copy of the argument – its value is copied. Modifying that copy will not modify the original object. However, when … Read more

C++ – passing references to std::shared_ptr or boost::shared_ptr

I found myself disagreeing with the highest-voted answer, so I went looking for expert opinons and here they are. From http://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2011-Scott-Andrei-and-Herb-Ask-Us-Anything Herb Sutter: “when you pass shared_ptrs, copies are expensive” Scott Meyers: “There’s nothing special about shared_ptr when it comes to whether you pass it by value, or pass it by reference. Use exactly the … Read more