tags:

views:

89

answers:

3

Just wonder how is it implemented actually across different compilers and debug/release configurations. Does standard somehow provides recommendations on its implementation? Does it differ anywhere?

I tried to run a simple program where i have been returning non-const references and pointers to local variables from functions but it worked out the same way. So is it true that reference internally is just a pointer?

A: 

I can't say this is right for sure, but I did some Googling and found this statement:

The language standard does not require any particular mechanism. Each implementation is free to do it in any way, as long as the behavior is compliant.

Source: Bytes.com

Mike
+2  A: 

The natual implementation of a reference is indeed a pointer. However, do not depend on this in your code.

Peter G.
Of course, no way for this to happen.
Keynslug
Peter G.
Martin York
Missed a not from original comment: -> As you can't do anything with a reference knowing its implementation will not hurt. You can **NOT** get the address of a reference you can **NOT** modify the reference itself (only what it refers to). There is no way to interact with a reference.
Martin York
There is no standard-conforming way to directly depend on it. That is clear also to me. What I meant so say in my above comment was that the size of the storage of references is observable and has an effect. When you have a struct containing a reference member a and another struct that has a pointer member a instead and no padding or alignment issues the size of the struct will change if references had a different size. ...
Peter G.
... Perhaps I have an allocator that is much more efficient for 12 byte structs than for 16 byte structs. The difference in performance then is observable.
Peter G.
The size of storage necessary for a reference is situation-dependent. Even if you knew that a `T` you couldn't say for certain that it would take 4 bytes in `struct B;`
MSalters
+4  A: 

Just to repeat some of the stuff everyone's been saying, lets look at some compiler output:

#include <stdio.h>
#include <stdlib.h>

int byref(int & foo)
{
  printf("%d\n", foo);
}
int byptr(int * foo)
{
  printf("%d\n", *foo);
}

int main(int argc, char **argv) {
  int aFoo = 5; 
  byref(aFoo);
  byptr(&aFoo);
}

We can compile this with LLVM (with optimizations turned off) and we get the following:

define i32 @_Z5byrefRi(i32* %foo) {
entry:
  %foo_addr = alloca i32*                         ; <i32**> [#uses=2]
  %retval = alloca i32                            ; <i32*> [#uses=1]
  %"alloca point" = bitcast i32 0 to i32          ; <i32> [#uses=0]
  store i32* %foo, i32** %foo_addr
  %0 = load i32** %foo_addr, align 8              ; <i32*> [#uses=1]
  %1 = load i32* %0, align 4                      ; <i32> [#uses=1]
  %2 = call i32 (i8*, ...)* @printf(i8* noalias getelementptr inbounds ([4 x i8]* @.str, i64 0, i64 0), i32 %1) ; <i32> [#uses=0]
  br label %return

return:                                           ; preds = %entry
  %retval1 = load i32* %retval                    ; <i32> [#uses=1]
  ret i32 %retval1
}

define i32 @_Z5byptrPi(i32* %foo) {
entry:
  %foo_addr = alloca i32*                         ; <i32**> [#uses=2]
  %retval = alloca i32                            ; <i32*> [#uses=1]
  %"alloca point" = bitcast i32 0 to i32          ; <i32> [#uses=0]
  store i32* %foo, i32** %foo_addr
  %0 = load i32** %foo_addr, align 8              ; <i32*> [#uses=1]
  %1 = load i32* %0, align 4                      ; <i32> [#uses=1]
  %2 = call i32 (i8*, ...)* @printf(i8* noalias getelementptr inbounds ([4 x i8]* @.str, i64 0, i64 0), i32 %1) ; <i32> [#uses=0]
  br label %return

return:                                           ; preds = %entry
  %retval1 = load i32* %retval                    ; <i32> [#uses=1]
  ret i32 %retval1
}

The bodies of both functions are identical

TokenMacGuy