Assembly In
assembly language, it is typical to express references using either raw memory addresses or indexes into tables. These work, but are somewhat tricky to use, because an address tells you nothing about the value it points to, not even how large it is or how to interpret it; such information is encoded in the program logic. The result is that misinterpretations can occur in incorrect programs, causing bewildering errors.
Lisp One of the earliest opaque references was that of the
Lisp language
cons cell, which is simply a
record containing two references to other Lisp objects, including possibly other cons cells. This simple structure is most commonly used to build singly
linked lists, but can also be used to build simple
binary trees and so-called "dotted lists", which terminate not with a null reference but a value.
C/C++ The
pointer is still one of the most popular types of references today. It is similar to the assembly representation of a raw address, except that it carries a static
datatype which can be used at compile-time to ensure that the data it refers to is not misinterpreted. However, because C has a
weak type system which can be violated using
casts (explicit conversions between various pointer types and between pointer types and integers), misinterpretation is still possible, if more difficult. Its successor
C++ tried to increase
type safety of pointers with new cast operators, a
reference type &, and smart pointers in
its standard library, but still retained the ability to circumvent these safety mechanisms for compatibility.
Fortran Fortran does not have an explicit representation of references, but does use them implicitly in its
call-by-reference calling semantics. A
Fortran reference is best thought of as an
alias of another object, such as a scalar variable or a row or column of an array. There is no syntax to dereference the reference or manipulate the contents of the referent directly. Fortran references can be null. As in other languages, these references facilitate the processing of dynamic structures, such as linked lists, queues, and trees.
Object-oriented languages A number of object-oriented languages such as
Eiffel,
Java,
C#, and
Visual Basic have adopted a much more opaque type of reference, usually referred to as simply a
reference. These references have types like C pointers indicating how to interpret the data they reference, but they are typesafe in that they cannot be interpreted as a raw address and unsafe conversions are not permitted. References are extensively used to access and
assign objects. References are also used in function/
method calls or message passing, and
reference counts are frequently used to perform
garbage collection of unused objects.
Functional languages In
Standard ML,
OCaml, and many other functional languages, most values are persistent: they cannot be modified by assignment. Assignable "reference cells" provide
mutable variables, data that can be modified. Such reference cells can hold any value, and so are given the
polymorphic type α ref, where α is to be replaced with the type of value pointed to. These mutable references can be pointed to different objects over their lifetime. For example, this permits building of circular data structures. The reference cell is functionally equivalent to a mutable array of length 1. To preserve safety and efficient implementations, references cannot be
type-cast in ML, nor can pointer arithmetic be performed. In the functional paradigm, many structures that would be represented using pointers in a language like C are represented using other facilities, such as the powerful
algebraic datatype mechanism. The programmer is then able to enjoy certain properties (such as the guarantee of immutability) while programming, even though the compiler often uses machine pointers "under the hood".
Perl/PHP Perl supports hard references, which function similarly to those in other languages, and
symbolic references, which are just string values that contain the names of variables. When a value that is not a hard reference is dereferenced, Perl considers it to be a symbolic reference and gives the variable with the name given by the value.
PHP has a similar feature in the form of its $$var syntax. ==See also==