Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Reference (computer science)

From Wikipedia, the free encyclopedia
Data type which allows a program to indirectly access a particular value in memory
This article is about the general concept in computing. For the more specific concept in C++, seeReference (C++).
icon
This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed.
Find sources: "Reference" computer science – news ·newspapers ·books ·scholar ·JSTOR
(November 2010) (Learn how and when to remove this message)

Incomputer programming, areference is a value that enables a program to indirectly access a particulardatum, such as avariable's value or arecord, in thecomputer'smemory or in some otherstorage device. The reference is said torefer to the datum, and accessing the datum is calleddereferencing the reference. A reference is distinct from the datum itself.

A reference is anabstract data type and may be implemented in many ways. Typically, a reference refers to data stored in memory on a given system, and its internal value is thememory address of the data, i.e. a reference is implemented as apointer. For this reason a reference is often said to "point to" the data. Other implementations include an offset (difference) between the datum's address and some fixed "base" address, anindex, oridentifier used in alookup operation into anarray ortable, an operating systemhandle, aphysical address on a storage device, or a network address such as aURL.

Formal representation

[edit]

A referenceR is a value that admits one operation,dereference(R), which yields a value. Usually the reference is typed so that it returns values of a specific type, e.g.:[1][2]

interfaceReference<T>{Tvalue();}

Often the reference also admits an assignment operationstore(R,x), meaning it is anabstract variable.[1]

Use

[edit]

References are widely used inprogramming, especially to efficiently pass large or mutable data asarguments toprocedures, 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 ofindirect addressing and of manylinked data structures, such aslinked lists. References increase flexibility in where objects can be stored, how they are allocated, and how they are passed between areas ofcode. 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.

References can cause significant complexity in a program, partially due to the possibility ofdangling andwild references and partially because thetopology of data with references is adirected graph, whose analysis can be quite complicated. Nonetheless, references are still simpler to analyze thanpointers due to the absence ofpointer arithmetic.

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, thecall by reference calling convention can be implemented with either explicit or implicit use of references.

Examples

[edit]

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 toundefined behavior in a program, particularly due todangling pointers orwild pointers.Smart pointers areopaque data structures that act like pointers but can only be accessed through particular methods.

Ahandle is an abstract reference, and may be represented in various ways. A common example arefile handles (the FILE data structure in theC standard I/O library), used to abstract file content. It usually represents both the file itself, as when requesting alock on the file, and a specific position within the file's content, as when reading a file.

Indistributed computing, 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, aWSDL 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 particularweb service. A reference to alive distributed object is another example: it is a complete specification for how to construct a small software component called aproxy 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.

If we have a set of keysK and a set of data objectsD, any well-defined (single-valued) function fromK toD ∪ {null} defines a type of reference, wherenull is the image of a key not referring to anything meaningful.

An alternative representation of such a function is a directed graph called areachability graph. Here, each datum is represented by a vertex and there is an edge fromu tov if the datum inu refers to the datum inv. The maximumout-degree is one. These graphs are valuable ingarbage collection, where they can be used to separate accessible frominaccessible objects.

External and internal storage

[edit]

In many data structures, large, complex objects are composed of smaller objects. These objects are typically stored in one of two ways:

  1. With internal storage, the contents of the smaller object are stored inside the larger object.
  2. 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 anddynamic allocation metadata, and a time cost associated with dereferencing a reference and with allocating the memory for the smaller objects. Internal storage also enhanceslocality of reference 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 thedata 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 asJava,Smalltalk,Python, andScheme, do not support internal storage. In these languages, all objects are uniformly accessed through references.

Language support

[edit]

Assembly

[edit]

Inassembly 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

[edit]

One of the earliest opaque references was that of theLisp languagecons cell, which is simply arecord containing two references to other Lisp objects, including possibly other cons cells. This simple structure is most commonly used to build singlylinked lists, but can also be used to build simplebinary trees and so-called "dotted lists", which terminate not with a null reference but a value.

C/C++

[edit]
Further information:Reference (C++)

Thepointer 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 staticdatatype which can be used at compile-time to ensure that the data it refers to is not misinterpreted. However, because C has aweak type system which can be violated usingcasts (explicit conversions between various pointer types and between pointer types and integers), misinterpretation is still possible, if more difficult. Its successorC++ tried to increasetype safety of pointers with new cast operators, areference type&, and smart pointers inits standard library, but still retained the ability to circumvent these safety mechanisms for compatibility.

Fortran

[edit]

Fortran does not have an explicit representation of references, but does use them implicitly in itscall-by-reference calling semantics. AFortran reference is best thought of as analias 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

[edit]

A number of object-oriented languages such asEiffel,Java,C#, andVisual Basic have adopted a much more opaque type of reference, usually referred to as simply areference. 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 andassign objects. References are also used in function/method calls or message passing, andreference counts are frequently used to performgarbage collection of unused objects.

Functional languages

[edit]

InStandard ML,OCaml, and many other functional languages, most values are persistent: they cannot be modified by assignment. Assignable "reference cells" providemutable variables, data that can be modified. Such reference cells can hold any value, and so are given thepolymorphic 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 betype-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 powerfulalgebraic 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

[edit]

Perl supports hard references, which function similarly to those in other languages, andsymbolic 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.[3]PHP has a similar feature in the form of its$$var syntax.[4]

See also

[edit]

References

[edit]
  1. ^abSherman, Mark S. (April 1985).Paragon: A Language Using Type Hierarchies for the Specification, Implementation, and Selection of Abstract Data Types. Springer Science & Business Media. p. 175.ISBN 978-3-540-15212-5.
  2. ^"Reference (Java Platform SE 7)".docs.oracle.com. Retrieved10 May 2022.
  3. ^"perlref". perldoc.perl.org. Retrieved2013-08-19.
  4. ^"Variable variables - Manual". PHP. Retrieved2013-08-19.

External links

[edit]
Look updereference in Wiktionary, the free dictionary.
  • Pointer Fun With Binky Introduction to pointers in a 3-minute educational video – Stanford Computer Science Education Library
Uninterpreted
Numeric
Pointer
Text
Composite
Other
Related
topics
Background
Sub-topics
Applications
Related topics
Standards
Syntax and supporting technologies
Schemas, ontologies and rules
Semantic annotation
Common vocabularies
Microformat vocabularies
Types
Technology
General
Features
Mechanism
Memetics
RSS
Social
Standard
Form
Media
Alternative media
Micromedia
Related
Retrieved from "https://en.wikipedia.org/w/index.php?title=Reference_(computer_science)&oldid=1319598271"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp