References and Indirection

a function that takes a pointer adds a level of indirection to the object it points to.

does a function that takes a reference adds a level of indirection to the object it references?
I don't think so. A reference simply make's a new name for an object, that's local to the function.
closed account (zb0S216C)
There's no indirection with references. Using the parameter would be exactly the same as working with the object that's bound to the reference. For example:

1
2
3
4
5
6
7
8
9
10
11
12
void Function(int &AnInt);

int main()
{
    int Integer(0);
    Function(Integer);
}

void Function(int &AnInt)
{
    AnInt = 10; // Same as 'Integer = 10'
}

There maybe a level of indirection inside the implementation of the reference, but that's beyond the scope of this thread, and is an implementation detail.

Wazzak
Last edited on
I get the high-level theory of it, but I was curious about how it breaks down to assembly which I reckon is compiler-specific. I'm mostly trying to figure out if using references is faster when used in loops that are executed every frame (as in a game engine). consider the following pseudo code:

1
2
3
4
5
6
7
8
9
10
11
12
13
void function ( Object *ptr ) {
if ( !ptr )
  return;

for ( ... ) {
 ptr->doSomething();
 ptr->doSomethingElse();
 ptr->doSomethingA();
 ptr->doSomethingB();
 ptr->doSomethingC();
 ptr->doSomethingD();
}
}

vs
1
2
3
4
5
6
7
8
9
10
void function ( Object &ptr ) {
for ( ... ) {
 ref.doSomething();
 ref.doSomethingElse();
 ref.doSomethingA();
 ref.doSomethingB();
 ref.doSomethingC();
 ref.doSomethingD();
}
}

or even the following, for if we NEED to take a pointer for some other purpose
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Object *entity_looking_at; // global

void function ( Object *ptr ) {
if ( !ptr )
  return;

entity_looking_at = ptr;

Object &ref = *ptr;

for ( ... ) {
 ref.doSomething();
 ref.doSomethingElse();
 ref.doSomethingA();
 ref.doSomethingB();
 ref.doSomethingC();
 ref.doSomethingD();
}
}
Last edited on
closed account (zb0S216C)
Brandon Captain wrote:
"I was curious about how it breaks down to assembly which I reckon is compiler-specific. I'm mostly trying to figure out if using references is faster when used in loops that are executed every frame (as in a game engine)."

Yes, the implementation of a reference is implementation defined. Some compilers will implement references as a pointer with automatic dereferencing, or will simply be an alias for the referent. Given the two possible implementations, it's difficult to determine which implementation type is actually be used by your compiler.

If the compiler implements the reference as an alias, and not a pointer, there shouldn't be anything to push onto function()'s stack frame, because it doesn't allocate memory. If the compiler implements the reference as a pointer, then the reference's internal pointer will have to be pushed onto function()'s stack frame.

I recommend using the second code example (the one that accepts an Object by reference), because there's no possibility of a null-pointers (which eliminates a null check), there's a possibility of ptr not having to be pushed onto the stack, and there's a possibility of no dereference overhead.

Wazzak
Last edited on
Topic archived. No new replies allowed.