Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory usage of pass by value vs. pass by reference

For the past few days am trying to learn if pass by value and pass by reference impact the memory differently. Googling this query, people kept repeating themselves about a copy being created in terms of pass by value and how the original value is affected in terms pass by reference. But I was wondering if someone could zero in on the memory part.

like image 209
humble_learner Avatar asked Jan 18 '26 11:01

humble_learner


1 Answers

This question actually depends heavily on the particular language as some allow you to be explicit and define when you want to pass a variable by value and when by reference and some do it always the same way for different types of variables.

A quite popular type of behavior is to use passing by value (by default) for simple times: like int, string, long, float, double, bool etc.

Let us show the memory impact on a theoretical language:

int $myVariable = 5;

at this moment you have created a one variable in memory which takes the size required to store an integer (let us say 32 bits).

Now you want to pass it to a function:

function someFunction(int parameter)
{
    printOnScreen(parameter);
}

so your code would look like:

function someFunction(int $parameter)
{
    printOnScreen($parameter);
}

int $myVariable = 5; //Position A
someFunction($myVariable); //Position B
...rest of the code //Position C

Since simple types are passed by value the value is copied in memory to another storage place - therefore:

during Position A you have memory occupied by ONE int (with value 5); during Position B you have memory occupied by TWO ints (with values of 5) as your $myVariable was copied in memory during Position C you have again memory occupied by ONE int (with value of 5) as the second one was already destroyed as it was needed only for the time of execution of the function

This has some other implications: modifications on a variable passed by value DO NOT affect the original variable - for example:

function someFunction(int $parameter)
{
    $parameter = $parameter + 1;
    printOnScreen($parameter);
}

int $myVariable = 5; //Position A
someFunction($myVariable); //Position B
printOnScreen($myVariable); //Position C

During position A you set value of 5 under variable $myVariable. During position B you pass it BY VALUE to a function which adds 1 to your passed value. YET since it was a simple type, passed by value, it actually operates on a LOCAL variable, a COPY of your variable. Therefore position C will again write just 5 (your original variable as it was not modified).

Some languages allow you to be explicit and inform that you want to pass a reference and not the value itself using a special operator -for example &. So let us again follow the same example but with explicit info that we want a reference (in function's arguments - note the &):

function someFunction(int &$parameter)
{
    $parameter = $parameter + 1;
    printOnScreen($parameter);
}

int $myVariable = 5; //Position A
someFunction($myVariable); //Position B
printOnScreen($myVariable); //Position C

This time operation and memory implications will be different.

During Position A an int is created (every variable is always consisted of two elements: place in memory and a pointer, an identifier which place is it. For ease of the process let us say that pointer is always one byte). So whenever you create a variable you actually create two things:

  • reserved place in memory for the VALUE (in this case 32 bits as it was an int)
  • pointer (8 bits [1 byte])

Now during position B, the function expects A POINTER to a memory place. Which means that it will locally, for itself create only a copy of the pointer (1 byte) and not copy the actual reserved place as the new pointer WILLL POINT to the same place as the original one. This means that during operation of the function you have:

TWO POINTERS to an int in memory ONE place reserved for VALUE of the int Both of those pointer POINT to the same VALUE

Which means that any modification of the value will affect both.

So looking at the same example position C will not print out also 6 as inside the function we have modified the value under the SAME POINTER as $myVariable.

For COMPLEX TYPES (objects) the default action in most programming environments is to pass the reference (pointer).

So for example - if you have a class:

class Person {
   public string $name;
}

and create an instance of it and set a value:

$john = new Person();
$john->name = "John Malkovic";

and later pass it to a function:

function printName(Person $instanceOfPerson) 
{
   printOnScreen($instanceOfPerson);
}

in terms of memory it will again create only a new POINTER in memory (1 byte) which points to the same value. So having a code like this:

function printName(Person $instanceOfPerson) 
{
   printOnScreen($instanceOfPerson);
}

$john = new Person(); // position A
printName($john); // position B
...rest of the code // position C

during position A you have: 1 Person (which means 1 pointer [1 byte] to a place in memory which has size to store an object of class person)

during position B you have: 2 pointers [2 bytes] but STILL one place in memory to store an object of class person's value [instance]

during position C you have again situation from position A

I hope that this clarifies the topic for you - generally there is more to cover and what I have mentioned above is just a general explanation.

like image 52
Bartosz Pachołek Avatar answered Jan 21 '26 08:01

Bartosz Pachołek