1

In this article its says that references allow you to reduce the amount of copying behind the scenes. Does reference reduce any other operation compared to a variable?

Mark
  • 8,408
  • 15
  • 57
  • 81
  • Less copying implies less destruction too. What sort of thing did you have in mind? Obviously use of a reference doesn't inherently reduce the number of times you have to go around a loop before it's finished, or anything like that... – Steve Jessop Jun 10 '11 at 21:36
  • I am wondering if references have any other speed advantages over variables. – Mark Jun 10 '11 at 21:38

6 Answers6

1

Reference is a kind of a variable. You're asking about passing by reference instead of passing by value.

Passing by value creates a copy of the value being passed, while passing by reference means that the receiver will be able to change that same variable whose value you passed.

Both have its own benefits and shortcomings, and should be used when appropriate (for example, by reference would be used sometimes to save on copy operations or get return values, while by value would be used to pass data which shouldn't be locally changed but will be changed inside the called function).

littleadv
  • 20,100
  • 2
  • 36
  • 50
1

On most compiler, references are implemented using pointers, and thus, have the same exact costs as those that would have been implied if you were using a pointer instead of a reference.

Note that the standard does not force implementation of references to rely on pointers. In particular

It is unspecified whether or not a reference requires storage (3.7).

References allow you to pass arguments "by reference", as opposed to "passed by copy".

void f(int & r) { r = 1; }
void g(int const * p) { *p = 2; }
void h(int j) { j = 3; }
int main()
{
  int i = 0;
  f(i);  // i now equals 1
  g(&i); // i now equals 2 (the address of i was given to g)
  h(i); // i is copied and thus not modified (ie i == 2 after this line)
}

See also C++ faqlite: references.

log0
  • 10,489
  • 4
  • 28
  • 62
0

The idea of a reference as a variable like this:

 const int & r = 42;

is basically wrong. OK, you can create such things, but what references were originally designed for was to facilitate passing objects to functions and (to a lesser extent) returning them from functions. If you use them in any but these two contexts, unless you are much, much cleverer than me, or most other programmers, you are doing it wrong.

  • 1
    What you just said doesn't make much sense. – Arelius Jun 10 '11 at 21:44
  • 1
    So when should I pass and/or return reference? Could you give an example? – Mark Jun 10 '11 at 23:33
  • 1
    @Neil butterworth I don't have one. Do you know a book that explains this? – Mark Jun 11 '11 at 00:05
  • @Mark Then get one. See http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list. You cannot learn C++ from internet resources. –  Jun 11 '11 at 00:09
0

If you are concerned about memory usage, just think of passing by reference as passing by value with a pointer, with the dirty work done behind the scenes. The amount of memory used by the pointer depends on the platform/OS.

daalbert
  • 1,465
  • 9
  • 7
0

To answer your exact question, passing by reference only does one thing: it allows you to pass a variable into a function instead of a copy of that variable.

Depending on when this is used, the implications are huge. But that's the most basic answer I can think of to get you started.

Edit: I saw your concern was speed. It is always strongly recommended to pass by reference whenever it is an option, because the speed savings can be huge, especially when some huge class/struct is being passed into a function.

Allen
  • 179
  • 1
  • 7
0

No. Savings apply only to copying esp.when passing as argument to function and return value from the function.

Andrei
  • 8,606
  • 10
  • 35
  • 43