Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is a reference different from a pointer in implementation? [duplicate]

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.

10:48 AM 11/20/2021

Reference is from the semantic perspective.

Pointer is from the implementation perspective.

It's kind of like the relation between what and how.

like image 714
smwikipedia Avatar asked Feb 24 '10 02:02

smwikipedia


People also ask

What is the difference between a reference and a pointer?

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.

What is the difference between a pointer value and a pointer 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.

What is the difference between references and pointers Mcq?

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.

Are references implemented as pointers?

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.


4 Answers

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.

like image 152
dirkgently Avatar answered Sep 17 '22 15:09

dirkgently


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).

like image 39
Prasoon Saurav Avatar answered Sep 17 '22 15:09

Prasoon Saurav


Use references when you can, and pointers when you need to. Reasons you'd need to use a pointer:

  1. There might not be an object for it to point at (null pointer, no null references).
  2. You might need to refer to different objects during its lifetime.
  3. You might need to refer to a whole array of objects (but 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.

like image 32
Jerry Coffin Avatar answered Sep 17 '22 15:09

Jerry Coffin


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

  • Reference always points to an object (cannot be NULL)
  • Reference points to one object only (not to an array like a pointer might)
  • Reference must be initialized initially (otherwise you get a compile error)
  • Reference cannot be modified after initialization to point somewhere else
  • Deletion of the object pointed to by a reference, while the reference variable stays alive, is Undefined Behavior (but not a compile error)
like image 44
Tronic Avatar answered Sep 16 '22 15:09

Tronic