answersLogoWhite

0

Pass by reference is preferable when we need to pass an object that is too large to fit into a CPU register. Typically this means any object of a type larger than a pointer. When we pass large objects by value, a new object is copy constructed from the object's value, and that can have a serious impact on performance. Passing by reference ensures that no copy is made; we're simply passing the object's memory address, which is guaranteed to fit in a CPU register.

Ideally we should pass by constant reference because users do not expect functions to have side-effects. If a function needs to alter the value of its argument, then it should use the pass by value semantic. If we need to return the modified value back to the caller, it should use the return value. With appropriate move semantics, returning objects by value has minimal impact and eliminates all side-effects:

class X {

public:

X (const X&); // copy construct

X& operator= (const X&); // copy assign

X (X&&); // move construct X& operator= (X&&); // move assign

// ...

};

X use (X obj) { // pass by value (invokes copy constructor)

// modify obj...

return obj; // return by value (invokes move constructor)

}

X x1;

x1 = use (x1);

Passing by non-const reference to achieve the same end can have a (slight) performance advantage, however passing by value ensures the caller is wholly responsible for any side-effects and not the function. If a function has side-effects, we need to know what they are, but we don't always have access to the source code, so passing by value or by const reference ensures there aren't any side-effects.

User Avatar

Wiki User

8y ago

What else can I help you with?

Related Questions

What do you call it in literature when you make a reference to something historical?

maybe you should do more reserch to find the answer


What is the practical values of zero sequence impedance?

zero sequence value of 110 kv XLPE is more than its reference value what is the cause


What does call-by-Value and call-by-Reference mean?

When we swap values in function, that time "Call by value" swaps copied value not the exact values. so it doesn't reflect on main function. But in case of "Call by reference", we swap actual value which is available at that reference or address.


What do you call anything you own that has value?

only when havce more


Why should one go for call by reference in c?

Not possible, in C you can use only call-by-value. Except, of course, that you can pass pointers by value, which is exactly equivalent to call-by-reference. There are many possible situations, but one of the most common is when passing a struct. It is not only more convenient and more efficient to pass a pointer to the structure than to pass a copy of it, it also allows functions to modify the structure and/or attach the structure to other data structures. This is particularly true when doing pseudo-object-oriented programming in C; pointers to structures (equivalent to objects) are passed to many different functions (equivalent to methods) which may need to modify the members of the structures. This is just the substitution of call-by-reference, not the thing itself. If you know Pascal or C++, you know what the real c-b-r is.


Will a background check by a prospective employer find out that you had been fired from your previous job?

Nope, but a reference check will do the trick. If they call your past employers for a reference check, they'll more than likely find out.


What is difference between 'call by value' and 'call by reference' in the book-computer in programming language-author -vrajaramani?

With call by value semantics, the value of the variable is passed to the function. With call by reference semantics, the memory address of the variable is passed to the function. Pass by reference is typically used to pass large and complex types to functions because there is no performance penalty in passing objects by reference. Passing by reference also makes it possible for the function to modify the argument. If the function does not need to modify the reference, the reference should be declared constant. This assures the caller that the non-mutable members of the object being passed will remain in an unmodified state. Pass by value should only be used to pass primitive or built-in data types. That is, any type that is small enough to fit into a CPU register. This is because pass by value makes a copy of the variable and copying large or complex variables can be an expensive operation in terms of both performance and memory consumption. Imagine passing an array with 1 million elements, each of which must be copy constructed! Copying a single memory address is as cheap as it gets and that's precisely what pass by reference means. Some languages do not support a native reference type and therefore default to pass by value semantics. C is a typical example. However, C supports native pointer types which are simply variables that store memory addresses. Thus you can enable pass by reference semantics by passing a pointer by value. The pointer itself is copied, not the value being pointed at. Many high-level languages do not support native pointers and therefore default to pass by reference semantics. However, this is achieved through resource handles; there is an underlying pointer being passed (by value) behind the scenes but it is (usually) inaccessible to the programmer. Java is a prime example of this. If you wish to use pass by value semantics you must explicitly pass a copy of the object. Defaulting to pass by reference semantics simply encourages efficient coding. C++ is one of a few languages that supports both pointers and references and either method can be used to enable pass by reference semantics. The main differences between a pointer and a reference are that pointers are variables which may be null whereas references are aliases (alternate names for existing objects) which can never be null. More specifically, the address of a reference is the address of the object being referenced and that object must exist (a null reference is an error). A pointer refers to the address stored in the pointer itself (the value of the pointer) thus the address of a pointer is separate from the address of the object being pointed at. This makes it possible for pointers to refer to other pointers, thus making it possible to pass pointers by reference.


What is the uses of call by value in c?

You pass by value when you do not want the original value to change and the value being passed is no more than 4 bytes long (or 8 bytes on a 64-bit system). If the value is greater than 4 bytes, you will incur a copy overhead, which can be expensive if the value is an array or other complex structure or object. If the value is passed as a const, the function will not alter its copy of the value. If the value is passed as non-const, the function can alter its copy of the value. Either way, the original value is unaffected. Pointers are always passed by value. That is, the pointer is copied by the function, leaving the original pointer unaffected. However, since the copied pointer points to the same memory as the original pointer, it's as if you had passed a reference to that memory, without copying that memory. If the pointer is passed as a const, the immutable members of the memory being pointed at will be unaffected by the function call. If it is non-const, it is expected that the memory being pointed at will be changed by the function call. If you do not wish the original memory to be altered, you must copy that memory and pass a pointer to the copy instead. Naturally, this will incur a performance penalty. Passing by reference passes the actual value being referenced, without copying. However, the compiler implements references as pointers so the same rules apply as for pointers. If the reference is non-const and you do not wish the value to be altered, copy the value and pass a reference to the copy instead. Passing a pointer by reference, rather than by value, is achieved by passing a pointer to the pointer, rather than a copy of the pointer. This allows the function to change the value of the original pointer. That is, the original pointer can be altered by the function to point at another memory location. When dealing with dynamic, multi-dimensional arrays, pointers to pointers (to pointers!) are fairly common, if only to avoid the costly copy overhead.


What is a good what do you call a tree joke?

tree jokes are basically jokes with some reference of tree in it , but you have to be more precise about your question to find an exact answer.


What is the age and value of a Beretta 380 SN 639788?

You will have to call Beretta to find out. Value could be 100-1000 or more depending on specifics


What is difference between call by value and pass by value?

When you pass by value, the function's parameter is assigned the value of the argument that was passed. When you pass by reference, the function's reference parameter is assigned the address of the argument. In other words, pass by value copies the value, pass by reference passes the variable itself.Pass by reference is always the preferred method of passing arguments to functions when those arguments are complex objects. If the argument is a primitive data type then the cost in copying the value is minimal, but copying a complex object is expensive in terms of performance and memory consumption. If the function parameter is declare constant, you can be assured the object's immutable members will not be affected by the function. If it is non-constant and you do not wish your object to be altered, you can either copy the object and pass the copy, or pass the object by value if the function is overloaded to cater for this eventuality (good code will provide both options).


What is byvalue for java keywords?

There is no such keyword in Java. In general: whether an argument is passed by value or by reference is determined by whether the argument is a primitive (by value) or an Object (by reference). In reality, it's a little more complicated. It seems to be that the actual reference you send as an argument will not change, but the data it refers to will. // This method will not cause a change in the original value. void changeArg(int[] ints) { ints = null; } // This method will. void changeArg(int[] ints) { ints[0] = 0; }