Share to: share facebook share twitter share wa share telegram print page

Reference (computer science)

In computer programming, a reference is a value that enables a program to indirectly access a particular datum, such as a variable's value or a record, in the computer's memory 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.

A reference is an abstract 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 the memory address of the data, i.e. a reference is implemented as a pointer. 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, an index, or identifier used in a lookup operation into an array or table, an operating system handle, a physical address on a storage device, or a network address such as a URL.

Formal representation

A reference R 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]

interface Reference<T> {
  T value();
}

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

Use

References are widely used in programming, especially to efficiently pass large or mutable data as arguments to procedures, 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 and of many linked data structures, such as linked lists. 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.

References can cause significant complexity in a program, partially due to the possibility of dangling and wild references and partially because the topology of data with references is a directed graph, whose analysis can be quite complicated. Nonetheless, references are still simpler to analyze than pointers due to the absence of pointer 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, the call by reference 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 a program, particularly due to dangling pointers or wild pointers. Smart pointers are opaque data structures that act like pointers but can only be accessed through particular methods.

A handle is an abstract reference, and may be represented in various ways. A common example are file handles (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 on the file, and a specific position within the file's content, as when reading a file.

In distributed 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, a WSDL 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. 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.

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 ∪ {null} 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, 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:

  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 and dynamic allocation 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 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, Smalltalk, Python, and Scheme, do not support internal storage. In these languages, all objects are uniformly accessed through references.

Language support

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.[3] PHP has a similar feature in the form of its $$var syntax.[4]

See also

References

  1. ^ a b Sherman, 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. Retrieved 10 May 2022.
  3. ^ "perlref". perldoc.perl.org. Retrieved 2013-08-19.
  4. ^ "Variable variables - Manual". PHP. Retrieved 2013-08-19.
  • Pointer Fun With Binky Introduction to pointers in a 3-minute educational video – Stanford Computer Science Education Library

Read other articles:

هونوي فالس     الإحداثيات 42°57′23″N 77°35′14″W / 42.956388888889°N 77.587222222222°W / 42.956388888889; -77.587222222222  تاريخ التأسيس 1791  تقسيم إداري  البلد الولايات المتحدة[1]  التقسيم الأعلى مقاطعة مونرو، نيويورك  خصائص جغرافية  المساحة 6.720211 كيلومتر مربع6.720214 كيلومتر ...

قناة بيو-داي-بي في منافسة قناة تي-سيريس التاريخ 2018  تعديل مصدري - تعديل   قناة بيو-داي-بي في منافسة قناة تي-سيريس بيو-داي-بي في 2019T-Series' logo التاريخ 29 August 2018 – 28 April 2019 (7 months, 4 weeks, 2 days) النتائج T-Series overtook PewDiePie as the most-subscribed YouTube channel and became the first YouTube channel to reach 100 million subscribers.[1]...

Hepburn Shire Local Government Area van Australië Locatie van Hepburn Shire in Victoria Situering Staat Victoria Hoofdplaats Daylesford Coördinaten 37°18'50ZB, 144°8'16OL Algemene informatie Oppervlakte 1.470 km² Inwoners 14.959 (juni 2006) Overig Wards 5 Portaal    Australië Hepburn Shire is een Local Government Area (LGA) in Australië in de staat Victoria. Hepburn Shire telt 14.959 inwoners. De hoofdplaats is Daylesford.

Office skyscraper in Manhattan, New York Lefcourt Colonial BuildingGeneral informationStatusCompletedLocation295 Madison Ave.,New York, New YorkCoordinates40°45′06″N 73°58′45.7″W / 40.75167°N 73.979361°W / 40.75167; -73.979361Construction started1929Completed1930HeightAntenna spire538 ft (164 m)Technical detailsFloor count45Design and constructionArchitect(s)Charles F. Moyer Company and Bark & DjorupReferences[1] The Lefcourt Colonial ...

ColdplayColdplay setelah konser di Stadion Gelora Bung Karno, November 2023. Kiri ke kanan:Will Champion, Guy Berryman, Chris Martin dan Jonny BucklandInformasi latar belakangNama lainBig Fat Noises (1997)Starfish (1998)Los Unidades (2018)AsalLondon, InggrisGenre Rock alternatif pop rock post-Britpop pop Tahun aktif1997–sekarangLabel Parlophone Atlantic Capitol Warner Music EMI Fierce Panda Situs webcoldplay.comAnggota Chris Martin Jonny Buckland Guy Berryman Will Champion Phil Harvey[a...

PERT Probability density functionExample density curves for the PERT probability distribution Cumulative distribution functionExample cumulative distribution curves for the PERT probability distributionParameters b > a {\displaystyle b>a\,} (real) c > b {\displaystyle c>b\,} (real)Support x ∈ [ a , c ] {\displaystyle x\in [a,c]\,} PDF ( x − a ) α − 1 ( c − x ) β − 1 B ( α , β ) ( c − a ) α + β ...

Pour les articles homonymes, voir Bataille de Bohama. Opération Colère de Bohama Camion militaire tchadien Renault Kerax de retour à N'Djaména après l'operation, le 13 avril 2020 Informations générales Date 31 mars - 8 avril 2020 Lieu Lac Tchad Issue Victoire tchadienne Belligérants Tchad Nigeria État islamique en Afrique de l'Ouest Boko Haram Commandants Idriss Déby Forces en présence 6 000 hommes[1]Inconnues Inconnues Pertes 52 morts[2]Inconnues Plusieurs dizaines ou plusieu...

Kansas Department of Wildlife and Parks(KDWP)Agency overviewJurisdictionKansasHeadquarters1020 S. KansasTopeka, Kansas39°02′44″N 95°40′33″W / 39.045631°N 95.675873°W / 39.045631; -95.675873Employees420Agency executiveBrad Loveless, Secretary of Wildlife and ParksParent agencyState of KansasWebsiteKDWP Website Rock formation at Mushroom Rock State Park, Kansas (1916)[1] The Kansas Department of Wildlife and Parks (KDWP) is a state cabinet-level agenc...

Governor of Rhode IslandSeal of the governorFlag of the governorIncumbentDan McKeesince March 2, 2021StyleGovernor(informal)The Honorable(formal)StatusHead of stateHead of governmentTerm lengthFour years, renewable onceInaugural holderNicholas CookeFormationNovember 7, 1775(248 years ago) (1775-11-07)DeputyLieutenant Governor of Rhode IslandSalary$128,210 (2013)[1]Websitegovernor.ri.gov The governor of Rhode Island is the head of government of Rhode Island and serves as...

У Вікіпедії є статті про інші значення цього терміна: Мата Харі (значення). Мата ГаріMata Hari Жанр драмаРежисер Джордж ФіцморісПродюсер Джордж ФіцморісІрвінґ ТалберґСценарист Бенджамін Глейзер Лео Брінскі Доріс Андерсон Ґілберт ЕймеріУ головних ролях Ґрета ҐарбоРамон ...

1941 film by John Brahm This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed.Find sources: Wild Geese Calling – news · newspapers · books · scholar · JSTOR (April 2019) (Learn how and when to remove this template message) Wild Geese CallingDirected byJohn BrahmWritten byHorace McCoyProduced byHarry Joe BrownStarringHenry Fonda...

Public research university in Porto, Portugal This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed.Find sources: University of Porto – news · newspapers · books · scholar · JSTOR (January 2013) (Learn how and when to remove this template message) University of PortoUniversidade do PortoLatin: Universitas PortucalensisMottoVirt...

American scientist and engineer (1882–1953) Herbert Eugene IvesIves circa 1913Born(1882-07-31)July 31, 1882Philadelphia, PennsylvaniaDiedNovember 13, 1953(1953-11-13) (aged 71)Upper Montclair, New JerseyEducationUniversity of PennsylvaniaOccupationEngineerSpouseMabel Lorenz (m. 1908)ChildrenBarbara Ives BeyerKenneth IvesRonald IvesParent(s)Frederic Eugene IvesMary OlmsteadEngineering careerProjectsfacsimile transmissionvideotelephonytelevisionlenticular 3D photographyAwardsEdward Longs...

Hungarian medical school Semmelweis UniversitySemmelweis University Coat Of ArmsLatin: Universitas Budapestinensis de Semmelweis nominataMottoServamus Vitam Atque ServimusMotto in EnglishProtecting And Serving LifeTypePublicEstablished1769; 254 years ago (1769)RectorBéla MerkelyStudentsabout 12,000 (in 2021/2022)[1]LocationBudapest, HungaryCampusUrbanAffiliationsNCFMEA, EUA, WHOWebsitesemmelweis.hu Semmelweis University (Hungarian: Semmelweis Egyetem) is a rese...

When You BelieveSingel oleh Mariah Carey dan Whitney Houstondari album The Prince of Egypt, #1's, dan My Love Is Your LoveArti judulSaat Kau PercayaDirilis2 November 1998 (1998-11-02)Format 7 inci CD kaset DirekamAgustus 1998Genre Gospel soul R&B Durasi5:01 (versi album)4:39 (versi singel)Label DreamWorks Arista Columbia Pencipta Stephen Schwartz Kenneth Edmons ProduserBabyfaceKronologi singel Mariah Carey Sweetheart (1998) When You Believe (1998) I Still Believe (1999) Kronolog...

This biography of a living person needs additional citations for verification. Please help by adding reliable sources. Contentious material about living persons that is unsourced or poorly sourced must be removed immediately from the article and its talk page, especially if potentially libelous.Find sources: Anna Montañana – news · newspapers · books · scholar · JSTOR (September 2010) (Learn how and when to remove this template message) Anna Montañan...

2021 film by Richard Bates Jr. King KnightTheatrical release posterDirected byRichard Bates Jr.Written byRichard Bates Jr.Produced by Rob Higginbotham Colin Tanner Starring Matthew Gray Gubler Angela Sarafyan Andy Milonakis Kate Comer Nelson Franklin Emily Chang Johnny Pemberton Josh Fadem Barbara Crampton Ray Wise CinematographyShaheen SethEdited byBrit DeLilloMusic bySteve Damstra IIDistributed byXYZ FilmsRelease dates August 8, 2021 (2021-08-08) (Fantasia Film Festival) ...

Halaman ini berisi artikel tentang wilayah di India Tengah. Untuk wilayah di India Utara, lihat Malwa, Punjab. Untuk pemakaian lainnya, lihat Malwa (disambiguasi).MalwaWilayah alam(bekas divisi administratif)Wilayah perkantonan Mhow di MalwaNegaraIndiaLuas • Total81.767 km2 (31,570 sq mi)Ketinggian[1]500 m (1,600 ft)Populasi (2001) • Total18.889.000 • Kepadatan230/km2 (600/sq mi)Bahasa • Bahasa utamaMal...

This article is about the French sounding rocket of the 1970s. For the US spacecraft used to resupply the ISS, see SpaceX Dragon. DragonCountry of originFrance[1]Applicationhigh altitude researchsounding rocket[2] Sud-Aviation Belier rocket family. The Dragon is a two-stage French solid propellant[3] sounding rocket used for high altitude research between 1962 and 1973.[4][5][6][7][8] It belonged thereby to a family of solid-prop...

Bolivian-British politician, model and beauty pageant titleholder This biography of a living person needs additional citations for verification. Please help by adding reliable sources. Contentious material about living persons that is unsourced or poorly sourced must be removed immediately from the article and its talk page, especially if potentially libelous.Find sources: Jessica Jordan – news · newspapers · books · scholar · JSTOR (November 2010) (Le...

Kembali kehalaman sebelumnya