0

If I create a reference to a variable inside the scope of a function like that :

{
    int x = 5;
    int & ref = x;
}

Will it always create an implicit pointer ? Creating a pointer is needed if the reference is a function parameter, but in this case, it is the same as using x directly.

  • implementation details that you shouldnt worry about aside, there is no pointer. A reference is a reference. – 463035818_is_not_an_ai Aug 26 '20 at 09:06
  • Variables (even member variables) don't need to exits at a memory adress. The compiler is free to do what ever it wants as long as the code behaves according to the specification. As of that the value of a variable could stay solely in a register. – t.niese Aug 26 '20 at 09:31
  • There is no requirement that memory be allocated to represent `ref` in this case. After the definition of `ref`, any usage of `ref` can simply be turned directly into an access of `x`. This works, since there is no way to detect if the reference exists - `&ref` will give the same result as `&x`. In fact, if `ref` and `x` are not used at all within the block where they are defined, most modern compilers are capable of eliminating both `x` and `ref` out of existence, so it is AS IF they never existed. – Peter Aug 26 '20 at 09:57

3 Answers3

3

Not necessarily. How your compiler implements references is down to it, so long as it follows the C++ standard.

Remember that the compiler will adopt the as-if rule. You program the intended behaviour. The compiler generates the code. A good compiler will miss out your code snippet entirely since it has no observable effect.

See What exactly is the "as-if" rule?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

That's an unspecified implementation detail. (Function parameters might be passed in registers, which would mean no pointer either.)

But in this (automatic) scope, ref is just an alias for x, so no pointer is needed for the compiler.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
0

Others have given already the formally correct answer. I am trying with a more practical perspective. In principle, yes, the compiler will "always" create an implicit pointer. Frequently, that a pointer is the only way a reference can be implemented.

However, the compiler employs many optimization strategies and hence, frequently, the implicit pointer can and will be optimized away.

Some examples:

In your example above, since the variables are never used, everything even the variable x will be optimized away.

If you pass the reference to a function that cannot be inlined, the reference most likely will be kept. If the function can be inlined, the references probably can be optimized away as well.

void swap(int &a, int &b) {
  int c=a; a=b; b=c;
}

If the above function is typically equivalent to using pointers. If you ask your compiler to produce the assembly code, except for some minor differences, it will produce the same code. In many cases the function can be inlined which means your call to the function swap will be replaced by what the function is doing. As a consequence, the references will probably optimized away (same would be the case if you had been using pointers).

If your question goes deeper and is whether there is a difference in using pointers versus references, they are equally expensive. A reference cannot magically replace the necessity for a pointer. On the other hand, even though they are the same, references are not redundant from a code readability point of view.

In the end, as the others have explained use whatever makes your program more readable and don't worry about the difference.

Edit: removed vector<int&> sample - thanks idclev 463035818

Tom
  • 749
  • 4
  • 16
  • This argument can be extended to the idea that `int x;` declares an implicit pointer because I could write `&x` to the get rvalue of that implicit pointer at any time. – Stephen M. Webb Aug 26 '20 at 11:34
  • afaik `vector` is not possible – 463035818_is_not_an_ai Aug 26 '20 at 13:04
  • @idclev463035818 indeed, thanks for pointing this out, removed. – Tom Aug 26 '20 at 14:29
  • You can have a `std::vector>` though. `std::reference_wrapper` does indeed use a pointer. Oh the irony! – Bathsheba Aug 26 '20 at 20:19
  • @idclev463035818 I think we should wory abot the difference, for performance and because we have to undersstand how it works, thzt is the point of using a lol level language like C++. – linternetsansfil Aug 27 '20 at 07:06
  • @linternetsansfil it is correct that it is good to know the implications of ones choices on the performance of an application. The good news, in this case, there is none, it really depends on what is more readable. However, before optimizing the use of language feature x versus language feature y, it is always best to think about the runtime complexity of the algorithm one is using and whether there is a better algorithm. – Tom Aug 27 '20 at 21:18