0

The linked question spells out two possible ways to implement a reference: as an alias or as a pointer. One reply says that there is no difference between the two from the programmer's perspective. It seems to me that this is not true:

  • if reference is internally an alias, then passing by reference should have zero overhead (in which case, I would wonder why not pass an int by const reference instead of passing it by value).
  • if reference is internally a pointer, then passing by reference costs copying the address (and passing an int by const reference would both cost copying the address and introduce an indirection).

Is my analysis correct? So what is a reference internally, alias or pointer?

AlwaysLearning
  • 7,257
  • 4
  • 33
  • 68
  • It's implementation defined. – Some programmer dude Nov 15 '19 at 10:09
  • 1
    This is the wrong approach to thinking about optimizations. C++ compilation doesn't work out as "it's a reference, which must be copied, hence there is overhead". As an example, there are tons of references, objects and iterators passed around when you work with a `std::vector`, but the compiler inlines and optimizes everything to really good machine code where none of this abstraction exists any more. If you care about performance, profile (and, if you want, inspect assembly code). Don't theorize over "there might be overhead in copying a pointer" - that's for the compiler to worry about. – Max Langhof Nov 15 '19 at 10:10
  • 2
    What do you mean by "zero overhead" in the first bullet? You need to pass _something_ to the function somehow. – Mat Nov 15 '19 at 10:11
  • Please see my comment to the reply: https://stackoverflow.com/a/58874813/2725810 – AlwaysLearning Nov 15 '19 at 10:24

1 Answers1

0

One reply says that there is no difference between the two from the programmer's perspective.

This is true.

There is no portable way to tell the difference whether references are implemented as pointers or in some other way. The difference you consider is irrelevant from the programmers point of view. Passing a reference to a function is never completely free, so calling passing a pointer "overhead" is a bit too optimistic.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Why can't the compiler hardcode the memory location of the referenced object (e.g. the int value of the passed by const reference int variable) instead of generating code for copying something (address or whatever else)? – AlwaysLearning Nov 15 '19 at 10:20
  • 1
    @AlwaysLearning: presumably you'd want your functions to be callable from different contexts with different input arguments. You can't "hardcode" the address of the parameters. (Though after optimization, especially inlining, you might get a "zero-cost" function call - but that doesn't change anything from the language point of view. The "as if" rule is very strong.) – Mat Nov 15 '19 at 10:26
  • Could you please give an example whereby a const reference parameter cannot be optimized away? – AlwaysLearning Nov 15 '19 at 10:29
  • @AlwaysLearning "an example whereby a const reference parameter cannot be optimized away?" how would it make any difference if references are implemented as pointers or not? – 463035818_is_not_an_ai Nov 15 '19 at 10:37
  • @AlwaysLearning What do you mean with "optimized away"? Is passing the pointer in a register considered "optimized away"? Is passing a value according to calling convention overhead? Does dereferencing a pointer count as overhead? And what does all this matter if the compiler inlined and optimized it away? Please look at this assembly code and tell us which part you consider overhead: https://godbolt.org/z/2tk8qD – Max Langhof Nov 15 '19 at 10:43
  • @AlwaysLearning In practice, no parameter can ever be optimised away, except in the case the function call has been expanded inline. In that case parameters can be optimised away in exactly the same manner as other variables can be. – eerorika Nov 15 '19 at 10:50
  • Regarding the three last comments. Max, by optimizing away, I mean that the compiler would hardcode the memory location of the referenced object (e.g. the int value of the passed by const reference int variable) instead of generating code for copying something (address or whatever else). @eerorika, why would this optimization never happen? Formlyknownas, if such an optimization can always happen, then the compiler would never need to substitute reference by a pointer. Also, my question about the reason for passing int by value instead of by const reference would come back. – AlwaysLearning Nov 15 '19 at 11:23
  • @AlwaysLearning If you pass int at address 1 to the function, then pass int at address 2 to the function. What address should the compiler hard code so that both calls work? – eerorika Nov 15 '19 at 11:31
  • 1
    @eerorika You are right. I missed this embarrassingly simple point. A clear sign that I need to take a nap... – AlwaysLearning Nov 15 '19 at 12:57