Passing Arguments By Address In C++

For reference, I shall now refer to pass by address as PSA

Let’s just get something out-of-the-way. Passing a parameter by address involves using pointers. Yes, those horrible little things that you hated/hate whilst you learnt/are learning C, C++.

The idea of pointers is hard to understand and the most common thing novices will say is “Why learn pointers?”

Well, this is one of those reasons to learn them. PBA relies completely on pointers and by using this, the performance of your application can improve massively, so yeah, this is why pointers are important.

So what makes by PBA different to pass by value? Well, as mentioned above, pass by value literally copies the parameter into the function. PBA, on the other hand, simply takes a pointer to a variable as a parameter and copies it into the function.

Therefore, any changes we make to the variable in the function (or, more specifically the pointer) are changes were really making to the original variable that the pointer points to. Confused? Quite possibly. To try to make some sense of this, take a gander at the code below:

#include <iostream>

using namespace std;

void passMe(int x){
    x = 42;
}

void passBy(int *x){
    *x = 42;
}

int main(){
    int through = 10;
    int by = 10;

    passMe(through);
    passBy(&by);

    cout << through << “\n”;
    cout << by << “\n”;

    return 0;
}

Have a go at predicting the output of this program first. Then go ahead and compile the program. You should get the following output:

10

42

What you predicted? Great you understand pointers! If not, let’s have a look at what happened.

We have two functions in this program (three if you count main()). The first one is calledpassMe and takes a standard parameter of (int x). This is a pass by value function and is pretty normal, this is probably what you’ve been doing in the past to pass an argument to a function.

Inside the function we set the variable x to = 42. Pretty simple. As you should know, that variable will remain limited to the scope of that function. The parameters value wont change.

Now have a look at the second function called passBy. This takes the argument of (int *x). Notice the asterisk. This signifies that the parameter we pass will be the address to a variable, since the variable x is a pointer. Looking inside the function we find that we set the pointer x to 42. This means that the variable that we pass to x will change since all that pointer x does is point to that variable. This is pass by address. Lets see how we implemented that into the application.

Inside the main function, when we call the passMe function, we pass a parameter of the variable through which is 10. This gives x the value of 10 too inside the passMe function. However, when we change the value of x. The value of through remains the same, as is shown later on in main when we print the value of through which is still 10. With the passByfunction however, we pass the argument &by.

This means we pass the address of the variable by (which also equals 10). We therefore say that the pointer x (in the passByfunction) points to the address of the variable by. Therefore, any changes we make to x, we are really making to the variable by hence why when we go on to print the variable by the value is 42.

Now this may seem somewhat pointless and on a small-scale like this it is. However, when we pass by address, we don’t copy any parameter to the function, so if we have a particularly large parameter, say a struct, passing the address of the struct isn’t going to cause an exponential increase in the memory usage of our application SO our application will run faster.

This isn’t the case in pass by value functions, as the entire parameter is copied to the function, increasing memory usage and slowing down our application substantially. That’s essentially the main advantage of using pass by address. The disadvantage of doing such a thing is obvious though.

We can change the value of a variable which, at first glance, appears to be outside of the scope of the function. Obviously this can have massive side effects so it’s really important that you only use pass by address when you really need to. At the same time though. Don’t be afraid to use it.

Read More Post

STAY CONNECTED

Most Popular

Why Serviced Apartments Are the Perfect Choice for Long-Term Stays in Singapore – Everything You Need to Know
17 Dec

Why Serviced Apartments Are the Perfect Choice for Long-Term Stays in Singapore – Everything You Need to Know

If you’re heading to Singapore for a few weeks or many months, maybe for work or study or just to

Manifesting Your Dream Career: Finding Your Passion And Purpose
01 Aug

Manifesting Your Dream Career: Finding Your Passion And Purpose

Are you passionate about finding your dream career? Manifesting can help you align your career with your passions and purpose.

Revolutionizing Your Ride: Expert Auto Service At Carolina Auto Service
27 Feb

Revolutionizing Your Ride: Expert Auto Service At Carolina Auto Service

Finding a reliable auto service provider is paramount in the bustling world of vehicle maintenance and repair. That's where Carolina

Floratam: The Robust Grass Solution From Council Growers SOD
25 Jan

Floratam: The Robust Grass Solution From Council Growers SOD

Floratam grass, a popular St. Augustine variety, has become a go-to choice for homeowners and landscapers seeking a lush, durable

Carolina Mobile Autoservice: Your Go-To For Expert Brake Service Repair
25 Jan

Carolina Mobile Autoservice: Your Go-To For Expert Brake Service Repair

For vehicle owners, the braking system is among the most critical components for safety and performance. Carolina Mobile Autoservice, a

Safeguarding Your Property: Expert Water Damage Services By MBC Capital
24 Jan

Safeguarding Your Property: Expert Water Damage Services By MBC Capital

Water damage can significantly impact homes and businesses, often leaving a trail of destruction and disruption in its wake. Understanding