2

When I started programming, I learned what Pointers are in C++. In Java, references are variables which hold the address of an object. So conceptually it's a pointer, without the ability to directly manipulate it.

Later I applied that concept to a lot of other languages, and it all seemed to make perfect sense.

So the concept is, that you have a variable that holds a memory address / an identifier and you can assign it to other variables, or pass it to functions ...

But there is also this other concept of references, in the sense of an alias for another variable. So you would not use a variable to store an address, but rather have that address directly applied to the new variable. This could happen during compiling, interpreting, in a symbol table, etc. If the assignment operator would use aliases, you could get the same result as with reference variables.

So those two concepts confuse me now. Am I mixing stuff totally up now? Is one of those concepts technically preferred / best-practise? Which is implemented more often?

5
  • 5
    Would you mind to give an example for an alias in one of your favorite programming lannguages?
    – Doc Brown
    Nov 12, 2022 at 15:49
  • I guess I primarily wanted to know if the first mentioned concept is the more "common" one. As for Alias - I gues PHP references come close $a = 4; $b = &$b; Or C++ references, but as far as I know they are actually pointers.
    – tweekz
    Nov 12, 2022 at 18:29
  • 1
    Object references in languages like Java, C#, Python, Ruby, etc. introduce one layer of indirection. C, C++, PHP, etc. let you introduce further layers of indirection. They're still just pointers when it comes down to the hardware level: pointers to pointers to objects.
    – Alexander
    Nov 12, 2022 at 20:55
  • @Alexander they all allow arbitrarily deep layers of indirection. The difference is some let you create identifiers that know deep they can go. Nov 13, 2022 at 4:22
  • @tweekz in C++ pointers and references might be implemented in similar ways (and there are times when they aren't), but they are conceptually very different. A pointer is an object in it's own right, whereas a reference isn't.
    – Caleth
    Nov 14, 2022 at 15:35

2 Answers 2

1

References and aliases are two different but overlapping concepts:

  • a reference refers to an object to allow to access the object;
  • an alias provides another name for an object.

The references and alias semantics vary depending on the language and even the language implementation. I'll illustrate my point with C++:

A reference can be an alias, when you create a named reference to refer to an object in memory. In C++ for example int &r=i; creates a reference r that refers to the variable i. At the same, this reference also creates an alias for i, because we could use the name r or i interchangeably in the source code.

However some references are not an alias: for example in C++, an rvalue reference (e.g. int &&i) may refer to a temporary object, which does not have another name. So it's not an alias. And conversely, an alias is not necessarily a reference: for example, a preprocessor substitution #define my_a my_i would define an alias my_a at the preprocessor level that can be used for an object my_i within the scope than my_i.

Interestingly, the C++ specifications as example leave it to the compiler to decide if the references are implemented internally with a pointer stored somewhere, with some pointers kept in CPU registers, or with an alias:

[dcl.ref]/4: It is unspecified whether or not a reference requires storage

1
  • I'd still call int &&i an alias even if the object it names has no other names, just like auto lamb = [](){}; using lamb_t = decltype(lamb); creates a type alias to an unnamed type.
    – Caleth
    Nov 14, 2022 at 15:38
1

One of the most confounding problems on this topic is how heavily overloaded the word "reference" has become.

Another vexatious concept is the idea of "aliases" in which two (or more) names within the same scope can refer to the same storage.

Not only is it highly questionable why such same-scope aliasing would be necessary, but many explanations of it do not even attempt to reconcile it with the concept of pointer indirection.

In my view, a reference is a one-level pointer with implied address-taking and dereferencing. It is not an "alias" for storage at all. One name (declared in the standard way) is the "direct" name for the storage - any others (declared as references) are indirect pointers to the storage, not aliases.

The syntactic equivalence between direct access and referential access - an equivalence of appearances which is by design - does not mean the syntax is causing identical computational operations to occur in each case.

What makes this so dreadful in C++, is that references are a retrofit to a language that already had, and continues to have side-by-side, the more general (and more explicit) pointer syntax. The result is conceptually discordant - I presume it was supplied to ease certain programming workloads, where there would be limited or no mixed use of references and pointers together.

What is even worse is that many explanations I've seen do not describe "references" as a special kind of pointer. Instead they attempt to introduce a second conceptual layer of indirection between the names and the storage - this notion of "aliasing" - which is distinct from the layer of indirection provided by pointers.

This defies what appears to me to be a tenet of programming languages, which is that a declared name refers uniquely and immutably to a particular unit of storage. Any exceptions to this will prove the rule.

So in my view, this "aliasing" that some authors refer to does not in fact exist. It is merely a consequence of their own muddled understanding, which they spread to others in turn, hence our confusion about the matter.

Final point, the fact that the compiler, as an optimisation, may eliminate pointers by treating references as aliases, does not make the aliasing concept real within the programming language itself.

6
  • I'm not sure if this is an answer or a rant.
    – Simon B
    Nov 14, 2022 at 12:16
  • @SimonB, can it be both?
    – Steve
    Nov 14, 2022 at 17:55
  • My experience tells me that pointers (or pointer like constructs with a level of indirection) are more common. But why is that? What benefit does it have over Aliases? Is it more flexible? Because otherwise Aliases would seem to be the solution with less "overhead". PS: Which concept does JavaScript use?
    – tweekz
    Dec 3, 2022 at 18:05
  • @tweekz, because true aliases in the same scope would be regarded as an incompetent design. I've never written a program where I thought "gee, I wish I could refer to this same variable by two different names"!
    – Steve
    Dec 3, 2022 at 19:26
  • But why not use Aliases when passing a reference as a parameter in a function?
    – tweekz
    Dec 4, 2022 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.