1

As I got stuck between whether shortening the question or clarifying it, I do not think I can be understood with the question sentence. I tried to explain myself with examples here:

Let's see a non-working void function for decreasing a variable by 1 :

#include <iostream>

using namespace std;

void decrease (int a)
{
    a--;
}

int main ()
{
    int x = 17;
    decrease (x);
    cout << x << endl;
}

As expected it prints 17. The reason is super clear because when a variable is passed to a function, actually what it is passed is a copy of the original variable (they hold same values, but they are in different addresses) and therefore all the modifications are done to the copy of that variable, and the copy is destroyed after function terminates.

What we need here is pointers. If we modify the above code piece like this :

#include <iostream>

using namespace std;

void decrease (int* a)
{
    (*a)--;
}

int main ()
{
    int x = 17;
    decrease (&x);
    cout << x << endl;
}

It works because what we passed in the line decrease (&x) is a copy of the x's address. The decrement is done to the value of our copy, and because the value of it is equivalent to x's address the decrement effects address' value, which is the value the original x holds.

So far all is understandable. But when we solve the issue by references like below :

#include <iostream>

using namespace std;

void decrease (int&a)
{
    a--;
}

int main ()
{
    int x = 17;
    int& y = x;
    decrease (y);
    cout << x << endl;
}

How is this situation explained? When we decrease the copy of the reference of x, isn't it the same with trying to decrease the copy of x? If so, what makes references work here? Why does it work when the first code piece doesn't work? How does it modify the actual value of x?

NOTE : I found a post of user @Angew is no longer proud of SO stating that whether a reference takes memory or not is unspecified. As he stated, if we consider that references take memory in this kind of implementation and references behave like pointers I think it gets explainable, but still confused with the usage.

EDIT : I was using C and I decided to move on with C++, that is why I can't get the logic of references.

  • 1
    "So copying the reference itself is just copying the original object." In the answer you linked to, the referenced object is copied not the reference itself. But references itself can be copied just like pointers. (The quote is misleading.) – idmean Sep 08 '20 at 17:40
  • @idmean Does it really change anything? Object is copied to the function, but how is it even modified? – nowayicandothis Sep 08 '20 at 17:42
  • Related: [https://stackoverflow.com/questions/18147038/passing-object-by-reference-in-c](https://stackoverflow.com/questions/18147038/passing-object-by-reference-in-c) – drescherjm Sep 08 '20 at 17:46
  • 1
    In this particular example, passing by reference is similar to passing by pointer, just different syntax. `void decrease (int& a); ... decrease (x);` is equivalent to `void decrease (int* a); ... decrease (&x);`. A reference is an *alias* for another object (but the compiler is *likely* implementing that reference using a pointer internally). What is being passed to the function is the **address** of `x`, and then the function decrements the value stored at that address. – Remy Lebeau Sep 08 '20 at 17:52
  • It's a copy of a reference to x... and the copy also references x. What's the problem? Same as pointers - when you copy a pointer to x, the copy also points to x. – user253751 Sep 08 '20 at 17:54
  • @user253751 but reference's value is not an address, its value is of `int` type (same value with x, which is 17) and the copy is actually NOT a pointer. Do I know anything wrong? – nowayicandothis Sep 08 '20 at 17:59
  • @Remy Lebeau and user253751 this is why I don't understand why pointers and references get equivalent at this point. Are references designed like that? Like whenever you pass it into a function it magically becomes pointer? But when you don't, it behaves as an alias? As I said I used to use C before C++, it just does not make sense. – nowayicandothis Sep 08 '20 at 18:06
  • @nowayicandothis If you think of references as almost like pointers but slightly different, it makes sense. They have some different trade-offs, but *fundamentally they do the same thing pointers do* – user253751 Sep 08 '20 at 18:07
  • 1
    @nowayicandothis a reference and a pointer are two different things in the C++ standard. So no, a reference is not the same thing as a pointer. A reference is just an alias. Anything you do to a reference, you do to the thing it is referring to. However, a reference can ACT LIKE a pointer in *some* contexts, like when passing parameters by reference vs by pointer. And as I said earlier, a compiler is likely to *implement* a reference *using* a pointer internally, but that is an *implementation detail* by the compiler vendor. – Remy Lebeau Sep 08 '20 at 18:21

1 Answers1

1

Lets make this simple:

The meaning of "reference" is to refer to a specific variable.

You tell the compiler: "I want to work with this variable under another name, not make a copy".

How the compiler accomplishes this, is up to whoever writes the compiler (that is what the question you linked tries to explain). But the meaning of the syntax is "use the variable it self".

As for pointers, they just hold an address. It can be any address, and it is really just a number. You can do math with it, you can reinterpret the data stored there in to different types, and so on.

The use of pointers you demonstrate is just one single use out of many possible ones.

Another way to think about it is when a variable is passed to the function by reference, it is as if the variable was global, and the function could just reach an touch it but using the name of the parameter (as an alias) instead of the original variable name.

Lev M.
  • 6,088
  • 1
  • 10
  • 23
  • That indeed simplified. Because it didn't make sense of references behaving like pointers (i.e. modification of variables via addresses). Instead your explanation upon scope-extending is way more logical. As I understand, the key point is that there references don't do the exact thing what pointers do, but they both serve the same functionality.Thank you. – nowayicandothis Sep 08 '20 at 18:58
  • You are welcome. One point I want to add: The inventor of C++ does not like raw pointers because they are considered unsafe. This is true to a point because using a pointer you can manipulate any memory area and thus create bugs by accidentally overwriting different values. This is likely one of the main reasons references were added to C++ - to allow moving variables without copying data in a safe and controlled way. – Lev M. Sep 08 '20 at 19:26