Is It Counter-Productive to Pass Primitive Types by Reference

Pass int by const reference or by value , any difference?

For primitive types, passing by value is much better than passing by reference. Not only is there no indirection, but with a reference, the compiler has to worry about potential aliasing, which can ruin optimization opportunities.

Finally, pass-by-reference causes lvalues to become odr-used, which can actually cause linker errors. And this final issue doesn't go away if the call gets inlined.

Reasons to not pass simple types by reference?

If you create a reference, it's:

pointer to memory location -> memory location

If you use a value, it's:

memory location

Since a value has to be copied either way (the reference or the value), passing by reference does not improve performance; one extra lookup has to be made. So it theoretically "worsens" performance, but not by any amount you'll ever notice.

Is it better to pass by value or by reference for basic datatypes?

Standard doesn't have constraints about implementation of references, however usually they're implemented as autodereferenced pointers (actually with some exceptions). As you probably know, on 32 bit system pointer size is 4 bytes, that means that passing chars, shorts (types with sizeof() less than 4 bytes) by reference maybe considered as somewhat overkilling - using 4 bytes instead of 1 (char) or 2 (short).
In general it depends on whether the rigisters or stack is used for passing parameters: you can save a bit of stack when passing basic types by value, but in case of registers even for chars, 4 bytes will be used, so there's no point in trying to optimize something with ptrs/refs.

Javascript closures: primitive vs reference behaviour

Primitive types contain the actual data, while reference types contain only a memory address (a pointer to the memory location where the object's data resides).

So when you want to check the length field of your array (which is a reference type) you first need to find what is the memory address where the object's data resides (you look into y variable), you go to that address and finally you look at the data.

Now, when you call a function, for each parameter, a copy of their value is made and is stored in a local variable, available in your function scope. So every time you pass your array as a parameter, a copy of the memory address where the data resides is stored in a local variable (only the memory address is copied, not the whole object).

So, to answer your question: no, primitive and reference types are not treated differently when passed to a function. A copy is made in both cases, except the primitives contain the actual data, while references do not, they contain an address (a pointer to the actual data). When you follow the address you get the data, but the data might have been modified between the time you made a copy of the address and the time you checked the data.

Isn't Java pass by reference for non-primitive types

What confuses you is that you think

aC.a += "a";

modifies the state of the String object referenced by aC.a. That's not the case. Strings are immutable, and the above line of code creates a new String object and assigns it to aC.a, leaving all the other references to the original object as is.

Make the same test with a StringBuilder, and call

aC.a.append("a");

and you'll get a different result.



Related Topics



Leave a reply



Submit