I originally thought that using a reference to access an object is
more efficient than using a pointer.
Given an simple example like this,
int i = 12;
int& r = i;
int* p = &i;
the memory lay-out would look like,
|................ |
address of i and r |12 |
|.................|
|.................|
address of p |address of i |
|.................|
In order to access the value of i (12) through pointer p, you would have to
1. get the value of variable p;
2. use the value of p as the address of a memory chunk;
3. get the value of that memory address.
This would take at least two cpu clock ticks.
If reference r is used to access the value of i (12), you would only need to
1. get the value of variable r, since r is the same as i.
Surprisingly, when I wrote a short program to verify this, I got
totally reversed result. The pointer is faster than reference!
My test is based on GCC 3.2 on a W2K system.
Replies are appreciated.
-calvin