50

What is the difference between a constant pointer and a reference?

Constant pointer as the name implies can not be bound again. Same is the case with the reference.

I wonder in what sort of scenarios would one be preferred over the other. How different is their C++ standard and their implementations?

cheers

Arnkrishn
  • 29,828
  • 40
  • 114
  • 128

5 Answers5

63

There are 3 types of const pointers:

//Data that p points to cannot be changed from p
const char* p = szBuffer;

//p cannot point to something different.  
char* const p = szBuffer;

//Both of the above restrictions apply on p
const char* const p = szBuffer;

Method #2 above is most similar to a reference.

There are key differences between references and all of the 3 types of const pointers above:

Andy
  • 3,997
  • 2
  • 19
  • 39
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
8

I assume that you mean a const-valued pointer (e.g. int* const ptr), not a pointer to const (e.g. int const* ptr).

  • Not initializing a reference is a compile error (avoids the problem of uninitialized pointers)
  • A pointer may also point to an array, or it can be NULL, where a reference always refers to exactly one object.
  • The syntax is very different
Tronic
  • 10,250
  • 2
  • 41
  • 53
  • 1
    You **can** reference an array element: `int& intRef(intArray[2]);`. A const pointer pointing to an array points to the address of an array element. I'm not sure the array part of your second bullet means much. Maybe you mean that you can use the `[]` operator on a pointer. – thebretness Feb 25 '10 at 18:55
8

When you should use each:

reference: Use these by default. It is very common for people to dereference NULL pointers. You eliminate that risk with a reference.

const pointer: When you want a reference, but can't make one. For example, you are writing a driver, and you'd like a pointer to the beginning of a memory map. A reference doesn't make as much sense in that case. Also, if you need an array of the things, a reference won't work (though an array of simple classes with reference members will).

In the next example, a const pointer checks an error that a reference can't check:

int addFour( int* register ){
  if(isNull(arg)){
    throw NullPointerException();
  }  

  // some stuff
  *register += 4;

  return register;
}

// This could be any function that does pointer math.
bool isNull(const int* ptr){
  return( NULL == ptr );
}
thebretness
  • 632
  • 3
  • 13
5

Almost all points have been covered by other answers, except this important one : It is possible to do arithmetics on pointers, but not on reference. E.g.

int a[3] = {39, 18, 97};
int * const b = a;
int c = *(b+1);  // sets c = 18
Gaurav Singh
  • 456
  • 6
  • 17
0

Some differences:

A const pointer can point to NULL.

A const point can point to an array of objects.

A const pointer can be bound again by casting away the constness.

Andrew Stein
  • 12,880
  • 5
  • 35
  • 43
  • A pointer can point to the first element of an array of objects.... You can equally have an array pointer type though and reference to such a type as well. – Brian R. Bondy Feb 25 '10 at 17:47
  • Const casting should be extremely rare, well justified, and well documented. A lot of casting mixed in with normal functionality is usually a symptom of a poor design. – thebretness Feb 25 '10 at 17:50
  • Casting away the constness and writing to a constant variable gives a segmentation fault here, so it is definitely not a good idea. Constants are on many systems stored on a separate read-only memory area, or their values may be substituted directly to calculations by the compiler, avoiding the read entirely. – Tronic Feb 25 '10 at 17:55