Possible Duplicate:
Difference between pointer variable and reference variable in C++
I am reading about the book "Inside the C++ Object Model" by Stanley Lippman. What puzzles me is the difference between a "reference" of an object and a "pointer" to an object. I know that a reference must be initialized when declared, while a pointer could be left for later initialization. But I want to know the physical implementation difference between them.
Why should there be the "reference" mechanism; isn't it overlapping the function of a pointer? Under what circumstance should we use reference other than pointer? Many thanks.
Reference is from the semantic perspective.
Pointer is from the implementation perspective.
It's kind of like the relation between what and how.
References are used to refer an existing variable in another name whereas pointers are used to store address of variable. References cannot have a null value assigned but pointer can. A reference variable can be referenced by pass by value whereas a pointer can be referenced by pass by reference.
A reference variable provides a new name to an existing variable. It is dereferenced implicitly and does not need the dereferencing operator * to retrieve the value referenced. On the other hand, a pointer variable stores an address. You can change the address value stored in a pointer.
7. What is the difference between references and pointers? Explanation: References are an alias/another name for a variable whereas pointer stores the address of a variable.
Although internally, reference may be implemented as a pointer. But this is an implementation detail which does not change the fact that references cannot be interchanged with pointers. And one cannot write code assuming references are implemented as pointers.
A reference can be thought of as an implicitly de-referenced constant pointer (note this). Once a reference, always a reference. It allows for ease of writing code. Unless of course, you bring in move semantics and r-value references. The standard doesn't mandate how references should be implemented just as it does not mandate how pointers should be implemented. Most of the time though, pointers are synonymous with addresses of objects.
Most references are implemented using a pointer variable i.e. a reference usually takes up one word of memory. However, a reference that is used purely locally can - and often is - eliminated by the optimizer. For example:
struct S { int a, int b[100]; };
void do_something(const vector<S>& v)
{
for (int i=0; i<v.size(); ++i) {
int*& p = v[i].b;
for (int j=0; j<100; ++j) cout <<p[j];
}
In this case, p needs not be stored in memory (maybe it just exists in a register, maybe it disappears into the instructions).
Use references when you can, and pointers when you need to. Reasons you'd need to use a pointer:
std::vector
is usually better).There are, however, cases where the usage of the two doesn't really overlap at all, and you simply can't substitute one for the other. For one obvious example, consider the following code:
template <class T, size_t N>
size_t size(T(&matrix)[N]) {
return N;
}
This allows you to find the size of an array:
int array1[some_size];
int array2[some_other_size];
size_t n = size(array1); // retrieves `some_size`
size_t m = size(array2); // retrieves `some_other_size`
...but it simply won't compile if you try to pass a pointer to it:
int *x = new int[some_size];
size_t n = size(x); // won't compile
At very best it seems to make little sense to write the code to receive a pointer when part of its point is to reject being passed a pointer.
References are in most cases internally pointers (especially when stored or passed to functions). They operate the same way as a pointer would, as long as only operations valid for both are used.
The extra checks and syntactic sugar for references is strictly a compile-time feature, much like the type system (most information about data types is lost during compilation).
The important differences are
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With