Cutting to the chase: a "pointer" is a machine address, unless the context makes it clear that the term is being used more metaphorically. The term "reference" is often, but not always, synonymous in both senses. CeePlusPlus
is somewhat unusual in having both "references" and "pointers" in the language with somewhat different semantics. PassByReference
, however, means something very specific that is not language-dependent or context-dependent.
What is a pointer? Everyone thinks they know; but it's a term with a lot of different meanings in ComputerScience
, some of them rather loaded. Frequently, "pointer" is used as a dirty word--a low-level abstraction that no respectable code/programmer/system/language would touch with a ten-foot pole. Many creative alternate names for pointers have been coined over the years. For instance, early marketing literature and hype for JavaLanguage
was full of silly claims like "Java has no pointers". Hogwash. While the object references of JavaLanguage
may lack some of the nastier properties of CeeLanguage
for instance), they're still pointers--a fact betrayed by the presence of NullPointerException
in the Java library. :)
- SmugJavaWeenies? are fond of saying that that just proves that NullPointerException should be renamed, but in fact, Java does use pointers, whether you call them "pointers", "references", or whatever, and although it incorporates them into the language in a far safer way than C/C++.
So, what is
a pointer, exactly? At its basest level, it's a mechanism for doing indirection; for saying "that guy over there" without having to have that guy present. And, the pointer should provide efficient access/navigation to
the object being referenced. (For example; both an IP address and a MAC address can uniquely identify a network node anywhere in the world. An IP address is a pointer of sorts; I can use it to try and connect with any computer anywhere. On the other hand, a MAC address is not; the only way I could use a MAC address to find another computer would be to query each computer in the world (or a database containing this information) to find which computer, if any, had the matching MAC address).
However, there are many varieties of pointers; each with their own features, foibles, and faults.
A pointer, depending on the context, may be:
- A raw machine address into the memory space of some computer (or a logical address into some process space). It might point to a valid object; it might be null, it might point to garbage. See WildPointer. As such such a pointer is inherently temporary, as earlier or later the machine (or process) will die and with it the meaning of the address.
- An object reference or ObjectIdentifier; an opaque thing that corresponds to exactly one (or possibly none, in the case of nulls) object; via which the methods and attributes of the object may be accessed. An ObjectIdentifier might be implemented as a raw address, or as some higher-level abstraction.
- A ForeignKey. RelationalWeenies hate to hear this (the word "pointer" has an especially pejorative connotation in the relational world, it seems), but this is how the RelationalModel handles indirection. ForeignKeys are certainly more well-behaved than encoding physical addresses in relational tables (and the RDBMS will, or should, reject any transaction that results in a ForeignKey which doesn't correspond to a matching CandidateKey--the relational equivalent to a WildPointer), and ForeignKeys can be used for joins and the like (whereas ordinary pointers probably cannot); but the primary purpose of the ForeignKey is indirection.
- IpAddress?es, SymbolicLinks, UniformResourceIdentifiers, and such are pointers of a sort.
Pointers rule. -- ThreeStarProgrammer