Are structs ‘pass-by-value’?

It is important to realise that everything in C# is passed by value, unless you specify ref or out in the signature. What makes value types (and hence structs) different from reference types is that a value type is accessed directly, while a reference type is accessed via its reference. If you pass a reference … Read more

Java is NEVER pass-by-reference, right?…right? [duplicate]

As Rytmis said, Java passes references by value. What this means is that you can legitimately call mutating methods on the parameters of a method, but you cannot reassign them and expect the value to propagate. Example: private void goodChangeDog(Dog dog) { dog.setColor(Color.BLACK); // works as expected! } private void badChangeDog(Dog dog) { dog = … 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

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

Function Overloading Based on Value vs. Const Reference

The intent seems to be to differenciate between invocations with temporaries (i.e. 9) and ‘regular’ argument passing. The first case may allow the function implementation to employ optimizations since it is clear that the arguments will be disposed afterwards (which is absolutely senseless for integer literals, but may make sense for user-defined objects). However, the … Read more

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

tech