When to Pass Parameters by Value

The main reason to pass parameters by value is to improve performance, which is somewhat of a paradox, because this is also one of the main reasons to pass by reference.

The truth is that either method can improve performance?it just depends on the situation. Passing a parameter by reference avoids copying all the data members onto the stack, which could take a lot of resources if you’ve got a lot of members or you’re going to be calling the recipient function frequently. It also saves the time and memory required to allocated pointer members; allocating on the heap is much slower than pushing onto the heap.

If the parameter being passed is small, it’s possible that the compiler will place the entire value in a register for its whole lifetime. This is especially the case for built-in types or classes that contain one or two members that are also built-in types.

If you pass a register variable by value, depending on the processor and the compiler, the parameter may be passed in a register, which is very fast. If you pass it by reference, the compiler will have to store the object in RAM so that its address may be taken?references are usually implemented as RAM addresses. Essentially they are pointers that are automatically dereferenced for you, although I don’t think the ISO standard requires they be implemented in any particular way.

Suppose an object could have been stored in a register, and you pass it as a reference or pointer. This causes the compiler to copy the register to the stack, pass the stack address, and then possibly copy it back into a register after the call. This definitely slows you down.

Again, this is only permissible if slicing is not a problem. The recipient function expects the exact same type as the type you are passing?not its base class. This is often the case for simple classes like geometric points or numerical values.

If the copy constructor for the class you are passing is inlined, it may be faster to pass it by value.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS