HOME

TheInfoList




In
computer science Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application. Computer science is the study of computation, automation, a ...
, a reference is a value that enables a program to indirectly access a particular
data Data (; ) are individual facts A fact is something that is truth, true. The usual test for a statement of fact is verifiability—that is whether it can be demonstrated to correspond to experience. Standard reference works are often used ...

data
, such as a variable's value or a record, in the
computer A computer is a machine that can be programmed to Execution (computing), carry out sequences of arithmetic or logical operations automatically. Modern computers can perform generic sets of operations known as Computer program, programs. These ...

computer
's
memory Memory is the faculty of the brain A brain is an organ Organ may refer to: Biology * Organ (anatomy) An organ is a group of Tissue (biology), tissues with similar functions. Plant life and animal life rely on many organs that co-exis ...
or in some other storage device. The reference is said to refer to the datum, and accessing the datum is called dereferencing the reference. A reference is distinct from the datum itself. Typically, for references to data stored in memory on a given system, a reference is implemented as the
physical address In computing Computing is any goal-oriented activity requiring, benefiting from, or creating computing machinery. It includes the study and experimentation of algorithmic processes and development of both computer hardware , hardware and softw ...
of where the data is stored in memory or in the storage device. For this reason, a reference is often erroneously confused with a '' pointer'' or ''
address An address is a collection of information, presented in a mostly fixed format, used to give the location of a building, apartment, or other structure or a plot of land, generally using political boundaries Borders are geographic Geogr ...
'', and is said to "point to" the data. However, a reference may also be implemented in other ways, such as the offset (difference) between the datum's address and some fixed "base" address, as an
index Index may refer to: Arts, entertainment, and media Fictional entities * Index (''A Certain Magical Index''), a character in the light novel series ''A Certain Magical Index'' * The Index, an item on a Halo megastructure in the ''Halo'' series ...
into an
array ARRAY, also known as ARRAY Now, is an independent distribution company launched by film maker and former publicist Ava DuVernay Ava Marie DuVernay (; born August 24, 1972) is an American filmmaker. She won the directing award in the U.S. dram ...
, or more abstractly as a
handle A handle is a part of, or attachment to, an object that allows it to be grasp A grasp is an act of taking, holding or seizing firmly with (or as if with) the hand A hand is a prehensile, multi- fingered appendage located at the end of t ...
. More broadly, in networking, references may be ''network'' addresses, such as
URL A Uniform Resource Locator (URL), colloquially termed a web address, is a reference to a web resource that specifies its location on a computer network A computer network is a set of computer A computer is a machine that can be pr ...

URL
s. The concept of reference must not be confused with other values (''
keys KEYS (1440 AM broadcasting, AM) is a radio station serving the Corpus Christi, Texas, Corpus Christi, Texas area with a talk radio, talk format. It broadcasts on AM broadcasting, AM frequency 1440 kHz and is licensed to Malkan AM Associates, L. ...
'' or ''
identifier An identifier is a name that identifies (that is, labels the identity of) either a unique object or a unique ''class'' of objects, where the "object" or class may be an idea, physical countable In mathematics Mathematics (from Greek: ) i ...
s'') that uniquely identify the data item, but give access to it only through a non-trivial
lookup In computer science Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application. Computer science is the study of compu ...

lookup
operation in some table data structure. References are widely used in programming, especially to efficiently pass large or mutable data as
arguments In logic Logic is an interdisciplinary field which studies truth and reasoning. Informal logic seeks to characterize Validity (logic), valid arguments informally, for instance by listing varieties of fallacies. Formal logic represents stat ...
to
procedures Procedure may refer to: * Medical procedure * Instructions or recipes, a set of commands that show how to achieve some result, such as to prepare or make something * Procedure (business), specifying parts of a business process * Standard operatin ...
, or to share such data among various uses. In particular, a reference may point to a variable or record that contains references to other data. This idea is the basis of
indirect addressing Addressing modes are an aspect of the instruction set architecture in most central processing unit (CPU) designs. The various addressing modes that are defined in a given instruction set architecture define how the machine code, machine language in ...
and of many
linked data structureIn computer science Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application. Computer science is the study of Algorith ...
s, such as
linked list In computer science Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application. Computer science is the study of , ...

linked list
s. References can cause significant complexity in a program, partially due to the possibility of dangling and
wild reference Dangling pointers and wild pointers in computer programming are data pointer, pointers that do not point to a valid object of the appropriate type. These are special cases of memory safety violations. More generally, dangling references and wild r ...
s and partially because the
topology In mathematics Mathematics (from Greek: ) includes the study of such topics as numbers (arithmetic and number theory), formulas and related structures (algebra), shapes and spaces in which they are contained (geometry), and quantities ...

topology
of data with references is a
directed graph In mathematics, and more specifically in graph theory, a directed graph (or digraph) is a Graph (discrete mathematics), graph that is made up of a set of Vertex (graph theory), vertices connected by directed Edge (graph theory), edges often called ...

directed graph
, whose analysis can be quite complicated.


Benefits

References increase flexibility in where objects can be stored, how they are allocated, and how they are passed between areas of code. As long as one can access a reference to the data, one can access the data through it, and the data itself need not be moved. They also make sharing of data between different code areas easier; each keeps a reference to it. The mechanism of references, if varying in implementation, is a fundamental programming language feature common to nearly all modern programming languages. Even some languages that support no direct use of references have some internal or implicit use. For example, the
call by reference In a programming language A programming language is a formal language In logic, mathematics, computer science, and linguistics, a formal language consists of string (computer science), words whose symbol (formal), letters are taken from an ...
calling convention can be implemented with either explicit or implicit use of references.


Examples

Pointers are the most primitive type of reference. Due to their intimate relationship with the underlying hardware, they are one of the most powerful and efficient types of references. However, also due to this relationship, pointers require a strong understanding by the programmer of the details of memory architecture. Because pointers store a memory location's address, instead of a value directly, inappropriate use of pointers can lead to
undefined behavior In computer programming Computer programming is the process of designing and building an executable computer program to accomplish a specific computing result or to perform a particular task. Programming involves tasks such as analysis, genera ...
in a program, particularly due to
dangling pointer Dangling pointers and wild pointers in computer programming Computer programming is the process of designing and building an executable computer program to accomplish a specific computing result or to perform a particular task. Programming i ...
s or
wild pointer Dangling pointers and wild pointers in computer programming Computer programming is the process of designing and building an executable computer program to accomplish a specific computing result or to perform a particular task. Programming i ...
s.
Smart pointer In computer science, a smart pointer is an abstract data type that simulates a Pointer (computer programming), pointer while providing added features, such as automatic memory management or bounds checking. Such features are intended to reduce bug ...
s are opaque data structures that act like pointers but can only be accessed through particular methods. A
handle A handle is a part of, or attachment to, an object that allows it to be grasp A grasp is an act of taking, holding or seizing firmly with (or as if with) the hand A hand is a prehensile, multi- fingered appendage located at the end of t ...
is an abstract reference, and may be represented in various ways. A common example are
file handle In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a unique identifier (Handle (computing), handle) for a file (computing), file or other input/output System resource, resource, such as a pipe (Unix), ...
s (the FILE data structure in the C standard I/O library), used to abstract file content. It usually represents both the file itself, as when requesting a
lock Lock(s) may refer to: Common meanings *, a mechanical device used to secure items of importance *, a device for boats to transit between different levels of water, as in a canal Arts and entertainment *, a 2016 Punjabi film *, a sentient machin ...
on the file, and a specific position within the file's content, as when reading a file. In
distributed computing Distributed computing is a field of computer science Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application. Comp ...
, the reference may contain more than an address or identifier; it may also include an embedded specification of the network protocols used to locate and access the referenced object, the way information is encoded or serialized. Thus, for example, a
WSDL The Web Services Description Language (WSDL ) is an XML Extensible Markup Language (XML) is a markup language #REDIRECT Markup language In computer text processing, a markup language is a system for annotation, annotating a document in a way ...
description of a remote web service can be viewed as a form of reference; it includes a complete specification of how to locate and bind to a particular
web service The term Web service (WS) is either: * a service offered by an electronic device to another electronic device, communicating with each other via the World Wide Web The World Wide Web (WWW), commonly known as the Web, is an information ...
. A reference to a live distributed object is another example: it is a complete specification for how to construct a small software component called a ''proxy'' that will subsequently engage in a peer-to-peer interaction, and through which the local machine may gain access to data that is replicated or exists only as a weakly consistent message stream. In all these cases, the reference includes the full set of instructions, or a recipe, for how to access the data; in this sense, it serves the same purpose as an identifier or address in memory.


Formal representation

More generally, a reference can be considered as a piece of data that allows unique retrieval of another piece of data. This includes
primary key In the relational model of databases, a primary key is a ''specific choice'' of a ''minimal'' set of attributes (Column (database), columns) that uniquely specify a tuple (Row (database), row) in a Relation (database), relation (Table (database), t ...
s in
database In computing Computing is any goal-oriented activity requiring, benefiting from, or creating computing machinery. It includes the study and experimentation of algorithmic processes and development of both computer hardware , hardware and sof ...

database
s and keys in an
associative array In computer science Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application. Computer science is the study of , ...
. If we have a set of keys ''K'' and a set of data objects ''D'', any well-defined (single-valued) function from ''K'' to ''D'' ∪ defines a type of reference, where ''null'' is the image of a key not referring to anything meaningful. An alternative representation of such a function is a directed graph called a reachability graph. Here, each datum is represented by a vertex and there is an edge from ''u'' to ''v'' if the datum in ''u'' refers to the datum in ''v''. The maximum out-degree is one. These graphs are valuable in
garbage collection Waste collection is a part of the process of waste management. It is the transfer of solid waste from the point of use and disposal to the point of list of waste treatment technologies, treatment or landfill. Waste collection also includes the ...
, where they can be used to separate accessible from inaccessible objects.


External and internal storage

In many data structures, large, complex objects are composed of smaller objects. These objects are typically stored in one of two ways: # With internal storage, the contents of the smaller object are stored inside the larger object. # With external storage, the smaller objects are allocated in their own location, and the larger object only stores references to them. Internal storage is usually more efficient, because there is a space cost for the references and
dynamic allocation In computer science Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application. Computer science is the study of , , ...
metadata, and a time cost associated with dereferencing a reference and with allocating the memory for the smaller objects. Internal storage also enhances
locality of reference In computer science Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application. Computer science is the study of comp ...
by keeping different parts of the same large object close together in memory. However, there are a variety of situations in which external storage is preferred: * If the data structure is recursive, meaning it may contain itself. This cannot be represented in the internal way. * If the larger object is being stored in an area with limited space, such as the stack, then we can prevent running out of storage by storing large component objects in another memory region and referring to them using references. * If the smaller objects may vary in size, it is often inconvenient or expensive to resize the larger object so that it can still contain them. * References are often easier to work with and adapt better to new requirements. Some languages, such as
Java Java ( id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 147.7 million people, Java is the world's List of ...
,
Smalltalk Smalltalk is an object-oriented programming, object-oriented, dynamically typed reflection (computer science), reflective programming language. Smalltalk was created as the language underpinning the "new world" of computing exemplified by "human ...

Smalltalk
,
Python Python may refer to: * Pythonidae The Pythonidae, commonly known as pythons, are a family of nonvenomous snakes found in Africa, Asia, and Australia. Among its members are some of the largest snakes in the world. Ten genera and 42 species ...
, and Scheme, do not support internal storage. In these languages, all objects are uniformly accessed through references.


Language support

In
assembly language In computer programming Computer programming is the process of designing and building an executable computer program to accomplish a specific computing result or to perform a particular task. Programming involves tasks such as analysis, gene ...
s, the first languages used, 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. One of the earliest opaque references was that of the
Lisp Lisp (historically LISP) is a family of programming language A programming language is a formal language In logic, mathematics, computer science, and linguistics, a formal language consists of string (computer science), words whose symbo ...
language
cons cell In computer programming Computer programming is the process of designing and building an executable computer program to accomplish a specific computing result or to perform a specific task. Programming involves tasks such as: analysis, gener ...

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 list In computer science Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application. Computer science is the study of , ...

linked list
s, but can also be used to build simple
binary tree In computer science Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application. Computer science is the study of , ...

binary tree
s and so-called "dotted lists", which terminate not with a null reference but a value. Another early language, Fortran, does not have an explicit representation of references, but does use them implicitly in its
call-by-reference Evaluation strategies are used by programming languages A programming language is a formal language In mathematics Mathematics (from Ancient Greek, Greek: ) includes the study of such topics as quantity (number theory), mathematical st ...
calling semantics. 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 In computer science Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application. Computer science is the study of c ...
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++ C++ () is a general-purpose programming language In computer software, a general-purpose programming language is a programming language dedicated to a general-purpose, designed to be used for writing software in a wide variety of application ...

C++
tried to increase
type safety In computer science, type safety is the extent to which a programming language discourages or prevents type errors. A type error is erroneous program behaviour caused by a discrepancy between differing data types for the program's constants, variabl ...
of pointers with new cast operators and smart pointers in its standard library, but still retained the ability to circumvent these safety mechanisms for compatibility. A number of popular mainstream languages today such as Eiffel,
Java Java ( id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 147.7 million people, Java is the world's List of ...
, C#, and
Visual Basic Visual Basic is a name for a family of programming languages from Microsoft. It may refer to: * Visual Basic (classic) The original Visual Basic (also referred to as Classic Visual Basic) is a third-generation programming language, third-gener ...
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.


Fortran

A
Fortran Fortran (; formerly FORTRAN) is a general-purpose, compiled language, compiled imperative programming, imperative programming language that is especially suited to numerical analysis, numeric computation and computational science, scientific com ...

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.


Functional languages

In all of the above settings, the concept of mutable variables, data that can be modified, often makes implicit use of references. In
Standard ML Standard ML (SML) is a general-purpose modular functional programming language with compile-time type checking and type inference. It is popular among compiler In computing, a compiler is a computer program that Translator (computing), trans ...
,
OCaml OCaml ( , formerly Objective Caml) is a general-purpose, multi-paradigm programming language Programming paradigms are a way to classify programming languages based on their features. Languages can be classified into multiple paradigms. S ...
, and many other functional languages, most values are persistent: they cannot be modified by assignment. Assignable "reference cells" serve the unavoidable purposes of mutable references in imperative languages, and make the capability to be modified explicit. 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 an array of length 1. To preserve safety and efficient implementations, references cannot be type-cast in ML, nor can pointer arithmetic be performed. It is important to note that 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".


Symbolic references

Some languages, like
Perl Perl is a family of two high-level High-level and low-level, as technical terms, are used to classify, describe and point to specific Objective (goal), goals of a systematic operation; and are applied in a wide range of contexts, such as, for ...
, support symbolic references, which are just string values that contain the names of variables. When a value that is not a regular reference is dereferenced, Perl considers it to be a symbolic reference and gives the variable with the name given by the value.
PHP PHP is a general-purpose scripting language A scripting language or script language is a programming language A programming language is a formal language comprising a Instruction set architecture, set of instructions that produce various k ...

PHP
has a similar feature in the form of its $$var syntax.


References in object-oriented languages

Many object-oriented languages make extensive use of references. They may use references to access and assign objects. References are also used in function/
method Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In recent centuries it more often means a prescribed process for completing a task. It may refer to: *Scien ...
calls or message passing, and reference counts are frequently used to perform
garbage collection Waste collection is a part of the process of waste management Waste management (or waste disposal) includes the processes and actions required to manage waste Waste (or wastes) are unwanted or unusable materials. Waste ...
of unused objects.


See also

*
Reference type In computer programming, data types can be divided into two categories: value types and reference types. A value of value type is the actual value. A value of reference type is a Reference (computer science), reference to another value. Classifica ...
*
Abstraction (computer science) In software engineering Software engineering is the systematic application of engineering Engineering is the use of scientific principles to design and build machines, structures, and other items, including bridges, tunnels, roads, v ...
* Autovivification *
Bounded pointer In computer science, a bounded pointer is a pointer (computer programming), pointer that is augmented with additional information that enable the storage bounds within which it may point to be deduced. This additional information sometimes takes the ...
*
Linked data In computing Computing is any goal-oriented activity requiring, benefiting from, or creating computing machinery. It includes the study and experimentation of algorithmic processes and development of both computer hardware , hardware and soft ...
*
Magic cookie In computing Computing is any goal-oriented activity requiring, benefiting from, or creating computing machinery. It includes the study and experimentation of algorithmic processes and development of both computer hardware , hardware and softwa ...
*
Variable (programming) In computer programming Computer programming is the process of designing and building an executable computer program to accomplish a specific computing result or to perform a particular task. Programming involves tasks such as analysis, gene ...
*
Weak reference In computer programming Computer programming is the process of designing and building an executable computer program to accomplish a specific computing result or to perform a particular task. Programming involves tasks such as analysis, generat ...


References


External links


Pointer Fun With Binky
Introduction to pointers in a 3-minute educational video – Stanford Computer Science Education Library {{Web syndication Data types Programming language concepts Primitive types