16

This is a question from an exam in an advanced course in OOP, taught in C++ (in TAU university, this semester):

Q: What is the difference between a C++ pointer and a reference?

A.  A reference is the entire object while a pointer is only the address of it.
B.  The same meaning, and difference is only in syntax and usage.
C.  The syntax used to access the object.
D.  Pointers are simple address to the object while a reference uses the virtual table.

Which is the correct answer?

The course teacher claims that A is the correct one, and that a reference to an object is, in fact, the object itself. Is that correct? I realize that accessing the reference is equivalent to accessing the object itself, however, when destructing a reference, we do not destruct the object itself. A reference is an alternative name for the object, but saying that reference==object true?

BTW, the lecturer gave the following link to an faq as support for his claim, a quote:

"Important note: Even though a reference is often implemented using an address in the underlying assembly language, please do not think of a reference as a funny looking pointer to an object. A reference is the object. It is not a pointer to the object, nor a copy of the object. It is the object."

But still, I believe this is incorrect.

seaotternerd
  • 6,298
  • 2
  • 47
  • 58
amitlicht
  • 2,868
  • 6
  • 23
  • 27
  • 29
    All the answers are wrong. B is vague enough to be acceptable by the know-nothings that set these kind of questions, I guess. –  Jul 11 '10 at 18:19
  • 1
    I believe this quote should not be taken too literally. Its meaning is that in most ways it behaves like an object, but surely it does not mean it is an object itself. – EFraim Jul 11 '10 at 18:23
  • TAU is Tel-Aviv University? Inquiring minds want to know. – Heath Hunnicutt Jul 11 '10 at 18:23
  • Yes, TAU = tel aviv uni. – amitlicht Jul 11 '10 at 18:24
  • @Heath: Yes, it is. However I've seen this kind of garbage in exams from different universities... – EFraim Jul 11 '10 at 18:24
  • It's nice to know that even more developped countries have universities with mediocre teachers... IMHO the lecturer shouldn't have given OP any "link as support of his claim", but should be able to argue by himself. – jpalecek Jul 11 '10 at 18:31
  • 6
    Correct or incorrect doesn't matter, the question is useless as it neither discriminates between those who understand and those who don't nor does it cause the student to gain new insight into the concept. You may point your instructor at this web reference to prove it. – msw Jul 11 '10 at 18:37
  • A reference can be thought of an alias to a value, while a pointer is a value that holds the address of another value. That's it. – GManNickG Jul 11 '10 at 18:38
  • @msw - I gave the lecturer a link to this thread. His response was: "I send you definitions from the FIRST people in the worls, and you send me back unswers from people that not me not you even don't know who are they." – amitlicht Jul 11 '10 at 19:21
  • @ami: Why don't you tell your professor to open his copy of the standard and read for himself what it says. My answer is supported by notes in the standard, where are his standard quotes? – GManNickG Jul 11 '10 at 19:27
  • 1
    @Gman - well, if he says that what's-his-name from his link is a famous and trust-worthy persona, who am I to question it? /= – amitlicht Jul 11 '10 at 19:29
  • 4
    @ami: Another human? I'd argue there are many people on this very site (indeed there are) that are more knowledgeable than the person behind the FAQ. He is knowledgeable, no doubt, but he's just another person like you or me. Some people on this site are on the C++ standards committee, or are very close to it (and many of us pay quite close attention to current and modern C++ issues.) Again, who cares who we are: ask your professor to point to the quotes from the standard that support his position. The standard defines the language, not peoples beliefs. – GManNickG Jul 11 '10 at 19:46
  • 2
    @GMan But if it was litb saying it? Ok, Ok - joke! –  Jul 11 '10 at 20:05
  • 1
    @Neil: Haha, then it takes three of us to argue back. – GManNickG Jul 11 '10 at 20:07
  • 4
    The solution to this question is to insert an `'M'`. Then you'd be studying at __TAMU__ and your instructor would be Bjarne Stroustrup. – sbi Jul 12 '10 at 08:11

11 Answers11

40

They're all wrong.

A reference is essentially a synonym for another object. Internally, it is often implemented as a pointer, but it has the syntax as if it were the object it refers to.

A pointer is a separate object that stores the memory address of the object it points to (or 0 if it doesn't point to an object).

You could say that the reference is the object that it refers to (it certainly acts that way), but it is not. If a reference goes out of scope then the object it refers to is not destructed, so the reference is not the object.

Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
  • 7
    Also, the reference's static type is not the same as the object's static type. – Ben Voigt Jul 11 '10 at 18:39
  • 1
    +1 for a clear and concise definition of an often misunderstood (even by professors, it seems!) principle. – Abel Jul 26 '10 at 09:27
  • 1
    They become the same thing when you go a bit deeper. :D http://stackoverflow.com/questions/1640355/whats-the-low-level-difference-between-a-pointer-an-a-reference – Gordon Gustafson Aug 29 '10 at 14:43
7

There's a distinction between a reference and an object - you can have multiple references to the same object. An object has 'identity', while a reference itself doesn't really.

While the mechanics of a reference are quite different than those of a pointer, I'd say that conceptually, a reference is actually quite similar to a pointer.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • 4
    To clarify: the "identity" of an object is its address. You can use the address-of (`&`) operator on a reference, but you get the address of its referent, not of the reference itself. So references have no identity distinct from the object they refer to. – Ben Voigt Jul 11 '10 at 18:32
5

(B) is the closest, but still not exactly correct. A reference is syntactic sugar for a const pointer. Just like a const pointer, it must be bound to an lvalue at initialization, and can never be rebound. Just like a const pointer, it is polymorphic.

EDIT: Short proof that (A) is incorrect, since at least a couple people are defending it:

struct A { int x; int f() { return 1; } }
struct B : public A { int y; int f() { return 2; } }

B b;
A& a = b;

assert(sizeof(a) == sizeof(b)); // fail
assert(a.f() == b.f()); // fail again
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • const-references may be bound to r-values. `const int& x = 5;` is valid. The const-pointer equivalent is not. – Peter Alexander Jul 11 '10 at 18:28
  • 2
    @Ben Voigt: sometimes (many times) a compiler must use pointers to implement reference semantics. However, you're over-generalizing. The *meaning* of references (their semantics) means that the compiler can treat them completely differently than pointers. This answer relies far too heavily on a subset of implementation details. – Cogwheel Jul 11 '10 at 18:31
  • 1
    @EFraim: Well, they can if you really want to shoot yourself in the foot: int& a = \*(int\*)0; – SigTerm Jul 11 '10 at 18:31
  • @SigTerm: Is this really defined behavior? (I mean without referencing a, of course) – EFraim Jul 11 '10 at 18:36
  • Introduction of a nameless local variable is still syntactic sugar. And while it's illegal to bind a reference to NULL, it's not impossible, and a const pointer to NULL is equally useless. – Ben Voigt Jul 11 '10 at 18:36
  • @Ben: const pointer to NULL is *not* useless. Think of conditional initialization, depending on some condition. – EFraim Jul 11 '10 at 18:41
  • @Cogwheel: I don't know of any way in which a compiler can treat a reference differently from a const pointer. Both can cause aliasing, which limits optimization. Both are polymorphic. And either can be converted to the other at any time, using the address-of (`&`) and de-reference (`*`) operators. – Ben Voigt Jul 11 '10 at 18:41
  • @EFraim: I don't know. You can make reference from pointers, and you can cast things around. So I see no reason why it should be undefined. Also, you can easily check if it is null or not: "if (&a == 0){}" – SigTerm Jul 11 '10 at 18:44
  • 2
    In cases of dynamic polymorphism where references are being passed around various functions, they are indeed treated like pointers. However, that's not the only time we use references in the language. Some template techniques rely heavily on the fact that references can be collapsed into nothing during compilation. There are a few other uses where the compiler can perform optimizations that would be otherwise impossible with pointers. – Cogwheel Jul 11 '10 at 18:45
  • 2
    @Sig It is undefined because dereferencing a null pointer is always undefined behaviour. –  Jul 11 '10 at 18:45
  • @EFraim: Anything you can do with a const pointer to NULL, I can do with a reference. A const pointer to NULL has the following characteristics: It compares equal to other const pointers to NULL of the same type, different from all non-NULL pointers, and all other usage is undefined. `template T& null_reference() { static char t[sizeof (T)]; return *static_cast(t); }` Let's see... identity comparison to other null_reference values of the same type and different from all other values... check. All other operations are undefined behavior... check. – Ben Voigt Jul 11 '10 at 18:49
  • @Ben Voigt: "I don't know of any way in which a compiler can treat a reference differently from a const pointer." If you're talking about function parameters, then references are *probably* pointers. However, inside functions (i.e. for references that are not function parameters) compiler could easily simply access original variable instead each time you manipulate reference. I.e. it could easily store int in a register and access that register when you manipulate reference to that int. I.e. pointer must store address. Reference doesn't have to do it. – SigTerm Jul 11 '10 at 18:49
  • 2
    @Cogwheel: If you don't take an address of a non-volatile pointer (or any other non-volatile variable), it too can be optimized away. In fact, the unnamed locals introduced by references make optimization harder for references than for const pointers. – Ben Voigt Jul 11 '10 at 18:53
  • @SigTerm: Citation please. I'm unfamiliar with any clause in the standard that prohibits optimizing away pointer access in exactly the same way as references. – Ben Voigt Jul 11 '10 at 18:57
  • @Ben Voigt: I will not search for appropriate citation, because the discussion subject is not interesting enough for me. As far as I know, const pointer is guaranteed to be address. Reference is not. That's enough for me. – SigTerm Jul 11 '10 at 19:07
  • 1
    @SigTerm: A reference absolutely must be able to carry the address of the referent, because it's 100% legal to use the address-of operator (`&`) to get that address. If the reference/pointer is local and there's no pointer arithmetic, then storage of the address can be optimized away in both cases. – Ben Voigt Jul 11 '10 at 19:11
  • @Ben Voigt: Citation from C++ standard, please. – SigTerm Jul 11 '10 at 19:14
  • I quote footnote (4) of section 1.9 [intro.execution]: "Under the “as-if” rule an implementation is allowed to store two objects at the same machine address or not store an object at all if the program cannot observe the difference (1.9)." – Ben Voigt Jul 11 '10 at 19:22
  • That's the wording in the C++0x final committee draft, which is publicly available, but the *as-if* rule is present in C++03 as well. – Ben Voigt Jul 11 '10 at 19:22
4

Just to climb on my hobby-horse for a moment, Universities have no business in teaching people how to program in specific computer languages. The fact that they do is simply an indicator of how degraded they have become over the past 30 years. I worked for the University of London from 1979 to 1983 as a microbiology technician and programmer, and the microbiology students (note not CS students!) were expected to pick up how to use the computers and to program more or less on their own, which they did, as much as they needed to.

But nowadays even CS students seem to be spoonfed everything, and tested on that "knowledge" by almost impossible to fail tests like the one quoted by the OP.

Gah!!!

  • couldn't agree more. However, the above course is one of the most popular among choice courses in CS degree in TAU. – amitlicht Jul 11 '10 at 18:36
  • However things like references and pointers are fundamentals and should be taught (IMHO) by example of *some* programming language. – EFraim Jul 11 '10 at 18:38
  • 4
    @EFraim Yes, but the longer I live the more I understand why Knuth used an assembly language in his books, and an artificial one at that. –  Jul 11 '10 at 20:15
  • @Neil, You seem to be saying that students should be taught the general and use their own curiosity and ambition to apply those skills to the specific. It's equally valid and useful to teach students a wide variety of specific implementations and allow their curiosity and intelligence to lead them to recognizing and discerning the general principles of Computer Science. What's not acceptable is to teach them a few specifics and then call it a day. That's vocational training, not a CS education. – Jesse Dhillon Jul 12 '10 at 17:03
3

An important thing is to differentiate between:

  • Reference as an expression
  • Reference itself

The FAQ and the teacher seem to talk about the first point, but the teacher's question is formulated as if it were asking about the second point. To explain the point of view of the FAQ, consider what the Standard describes as the very first stage of expression processing

If an expression initially has the type "reference to T" (8.3.2, 8.5.3), the type is adjusted to "T" prior to any further analysis, the expression designates the object or function denoted by the reference, and the expression is an lvalue

After this transformation, a reference and the object or function it designates cannot be distinguished anymore using that expression. But that doesn't make a reference equivalent to an object. The former just refers to the latter. Even less so since references can also refer to functions.

Now, a reference itself is just that - an entity that happens to refer to an object or function but that doesn't store something on its own.

The same mistake in arguing is sometimes made by people when they say that arrays in C would be just pointers. What they really mean is that arrays in expressions are (mostly) just pointers. But that doesn't make both of them equal in their own right.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
1

All answers are incorrect, A is closest.

Pointer is address of object which is object itself.
"Object" is "something" somewhere in a memory. Class instance, int, float, etc.
Reference is an alternative way of accessing an object. It is reference to an object, but not the object itself. It may or may not be implemented as pointer. You may think about it as an alternative object name, but this isn't exactly right. The closest correct descriptions I can think of is "alternative interface for accessing/manipulating object"(unfortunately "interface" sounds misleading if you take OOP in account, although it is (IMO) most correct one).

SigTerm
  • 26,089
  • 6
  • 66
  • 115
1

Answering to whether the teacher is wrong: Simple logic. Strictly speaking, it is what the name (and the standard) say: It is a reference to (="a name of") the object, NOT the object itself. As said before, when the reference variable runs out of scope, the object's destructor is not called, therefore the reference is not the object.

#include <iostream>
class A {
public:
    ~A()
    {
            std::cout << "~A() called.\n";
    }
};
void f(A &a)
{
    // a running out of scope...
}
int main()
{
    A a;
    std::cout << "calling f()\n";
    f(a);
    std::cout << "done calling f()\n";
    return 0;
}
apriori
  • 1,260
  • 11
  • 29
1

One of the big differences between pointers and references that I haven't seen mentioned is that a pointer can be NULL while a reference can't. That doesn't mean the object a reference pointers to can't go out of scope resulting in the same types of issues people have with pointers, merely that there is no "no assigned" state.

wheaties
  • 35,646
  • 15
  • 94
  • 131
0

I believe your teacher is confusing a metaphor and a concrete statement. A reference definitely is not the actual object, and it is implemented as a "funny looking pointer" but the point of that statement is that you are to think of a reference as the actual object being referenced, you are not to think or program as if you are handling a pointer. Syntactically, accessing a reference to an object is identical to accessing the object itself, except in a few cases mentioned in the comments below.

Jesse Dhillon
  • 7,841
  • 1
  • 34
  • 34
  • Why did you mod me down? I said that objects and references behave identically. If we take your example code and remove the references, so it becomes A a = b; instead of A& a = b; you get the same result! All you're doing is a cast from a derived type to a base type, which you can do with a reference or an object. – Jesse Dhillon Jul 11 '10 at 22:14
  • First you said that accessing a reference is identical to accessing its referent. When I disproved that, you argue that a reference is the same as a copy (slice) of an object? I assure you that neither is the case. Would you like an example showing how `A& a = b;` behaves differently from `A a = b;`? – Ben Voigt Jul 11 '10 at 23:03
  • Yeah I actually would like to see that. – Jesse Dhillon Jul 11 '10 at 23:22
  • "I said that objects and references behave identically" they don't if you want to denote their identity. You cannot pass references as template non-type arguments, for example. But you can do so with objects, provided their name has external linkage. – Johannes Schaub - litb Jul 12 '10 at 17:56
  • I've amended my answer to allow for the exceptions that you guys have mentioned. I think, however, that the cases mentioned here are extreme corner cases, bordering on contrived. Nonetheless, they do prove that what I said was incorrect. – Jesse Dhillon Jul 17 '10 at 20:12
0

Let's evaluate one option at a time -

A. A reference is the entire object while a pointer is only the address of it.

There is no such a thing as "entire object"! it is just the object. Many references can point object and then cease to refer to it even though object itself continues to persist. Wrong Answer!

B. The same meaning, and difference is only in syntax and usage. It is NOT the same thing. For example, when you get &ref - you cannot do ref++ where as if you get *ptr, you can do ptr++. Pointer not only allow you to access objects but allows pointer arithmetic; thereby with pointers, you can pass not only 1 address (and modify that address) but the entire array of arbitrary location with the same syntax. You cannot do this with reference! Wrong Ans!

C. The syntax used to access the object. Didn't quite get how it constitutes difference between two concept. Wrong Ans!

D. Pointers are simple address to the object while a reference uses the virtual table. I don't thing there is anything called virtual table. Pointers are usually pointing in heap, where as reference is a pointer sitting inside the stack. Wrong Ans!

All are wrong...

Dipan.

Dipan Mehta
  • 2,110
  • 1
  • 17
  • 31
-1

The question isn't about objects and references, it's about pointers and references. The point is that pointers denote a memory location and references denote an object- a higher level semantic construct. Simple as that.

And the teacher already gave you the answer anyhow: A -> correct answer.

Good luck in your studies.

  • 5
    The fact that the instructor said that something is true does not necessarily make it true... – amitlicht Jul 11 '10 at 19:15
  • In the context of a class, I disagree. In a grade oriented value system the giver of grades is the ultimate arbiter of truth. That's what makes 'a' right, not my or your opinion of it. – Holland Whitten Jul 11 '10 at 19:24
  • 5
    The giver of grades is the ultimate arbiter of whether the diplomas are worth the paper they're printed on. IMHO that's slightly different from "truth". – Ben Voigt Jul 11 '10 at 19:32
  • 3
    Go to school with that attitude and you'll come away with something even less valuable. At any rate I choose not to argue with idiots because from a distance it's hard to tell who is who. Sayonara. – Holland Whitten Jul 11 '10 at 19:44
  • If it is difficult from a distance to know who is "who", how do you know with whom you are choosing not to argue? Specifically, why do you say these people may be "idiots"? I gave you a -1 for casting the word "idiot" at Ben, but as long as we are being frank, I was very disappointed in you for your sychophancic following of some random "University instructor." – Heath Hunnicutt Jul 11 '10 at 20:56
  • @Holland: If you are there to get a diploma and get out, then what the instructor says is right. If you are there to learn something, then what the instructor says may well be wrong, and you have the more difficult task of learning both what will pass the test and what's actually correct. And, personally, I do argue with idiots sometimes. – David Thornley Aug 17 '10 at 16:50