Pointers Versus Keys

First, go read WhatIsaPointer.

Advantages of keys (where "keys" means a triple of <database, table, CandidateKey>; or possibly just <table, CandidateKey> if the database is implicit; or possibly just <CandidateKey> if both the database and table are implicit).


[Note: The following is an attempt to refactor the above into a side by side comparison with any evaluation of benefits removed. Please feel free to edit to improve objectivity as needed. This was not an easy task and I added and edited text as I felt necessary for the comparison, but no bias was intended. Fix as needed.]


To me the difference is that keys usually have an ascii (or at least "printable") representation that is tied to a model of something external to the machine. For example, an employee ID is tied to an employee, not a slot in RAM. One talks about the key as if it labels some physical object or at least something that is part of the domain. -- top


If keys are identifiers for members of sets, then a pointer would be a key to the set 'memory'. If the use of a key is to allow access to an individual member of a set, then a process for retrieving the data stored at the key could be -

 Data = Get.Row.Value(Set.Name, Key)
and the syntax for retrieving it from a pointer could be -
 Data = Get.Variable.Value(Variable.Name)
but this syntax could easily be envisaged as -
 Data = Get.Row.Value("Current.Scope", Variable.Name)
So I think keys and pointers are the same. The difference is only in syntax, where the implicit operation is conveniently reduced to the naming of the pointer variable in code. -- PeterLynch


Isn't the biggest difference that keys are persistent while pointers are transient? I can write down a key on a piece of paper and use it a year later. A pointer may not be consistent between application runs nor even within a single application run. Keys and pointers each reflect the transience of the data they refer to.

Everything is transient.

[Perhaps the paragraph above should state, "The choice of key vs pointer may reflect the degree of transience (which may be dependent on the environment) of the data to which it refers."]


As PeterLynch stated above, conceptually a pointer is simply an auto-generated key belonging to a domain of memory addresses -- keeping in mind that what we mean by "memory address" may vary from environment to environment. However, there is a significant difference: Since a pointer is typically an index into an linear array of machine memory slots, retrieval of referenced data is direct. Or, at least it is relatively direct, because the pointer may represent a virtual memory address that has to be translated to a physical address in order to retrieve the associated data.

A key references data indirectly via lookup from a map of key/pointer pairs, because internally the machine only references data via pointers. Thus, dereferencing a key involves the overhead of a key lookup to obtain the associated pointer, plus the pointer dereference (and any associated translation, as noted above) to obtain the data.

In general, pointers offer better speed and storage efficiency at the expense of portability and environment independence. Keys offer better portability and environment independence at the expense of speed and storage efficiency. Choose one or the other depending on the speed/storage/portability/independence requirements of the problem at hand.

-- DaveVoorhis


See also: PointersAndReferencesAreTheSameThing, AutoKeysVersusDomainKeys

MayZeroSix


CategoryComparisons CategoryPointer


EditText of this page (last edited November 8, 2014) or FindPage with title or text search