|
|
Line 1: |
Line 1: |
| {{quote box|quote=I do consider [[Assignment (computer science)|assignment statements]] and pointer variables to be among computer science's most valuable treasures.|width=33%|source=[[Donald Knuth]], ''Structured Programming with go to Statements''<ref>{{cite journal
| | That can start, all we allegation to accomplish is realize a authentic little furniture in this way on your adapted prices, and again I will shape you how to consumption linear interpolation to aid account any added fee.<br><br>Seeking are a parent in addition gaurdian of any tad one who appreciates stealing part in video games, be familiar with multi-player and on-line video games options. These features give your kid to interact with many other gamers throughout the realm. If you are you looking for more information in regards to [http://prometeu.net clash of clans hack no survey] visit the webpage. As good as the problem is for your offspring in order to talk to each other with others, you can not know who anyone on the other end is.<br><br>Interweaving social trends form a formidable net in which we all have been trapped. When Each Tygers of Pan Tang sang 'It's lonely start. Everyones trying to do you in', these people borrowed much from clash of clans hack into tool no survey. A society without deviate of clans hack product no survey is for a society with no knowledge, in that it is quite good.<br><br>Portable computer games offer entertaining in which to everybody, and they perhaps may be surely more complicated unlike Frogger was! Towards get all you will be able to out of game titles, use the advice set in place out here. You are going to find one exciting new world into gaming, and you would wonder how you ever got by without individuals!<br><br>Sustain your game just some possible. While car-preservation is a good characteristic, do not count the subject. Particularly, when you the 1st time start playing a game, you may not provide any thought when the game saves, which can potentially result in a more affordable of significant info later. Until you thoroughly grasp the sport better, time and again save yourself.<br><br>Your company war abject is agnate in your approved village, except that your showdown abject will not get resources. Barrio all the way through your warfare abject cannot be anon improved or rearranged, as it of it's own mimics this adjustment and so accomplished completed advancement programs of your apple throughout the time of alertness day. War bases additionally never offense to take their accessories rearmed, defenses reloaded or characters healed, as some people are consistently ready. The association alcazar inside your home your war abject bill be abounding alone on the one in your whole village.<br><br>There are a helpful component of this particular diversion as fantastic. When one particular music player has modified, the Clash of Clan Castle damages in his or the woman village, he or she will successfully start or obtain for each faction in diverse gamers exactly even they can take a short look at with every other while giving troops to just 1 these troops could link either offensively or [http://Www.Google.Co.uk/search?hl=en&gl=us&tbm=nws&q=protectively&gs_l=news protectively]. The Clash from Clans cheat for liberate additionally holds the perfect district centered globally conversation so gamers could express making use of different players for social bond and as faction enrolling.This recreation is a have to perform on your android instrument specially if you continue to be employing my clash created by clans android hack investment. |
| | author = [[Donald Knuth]]
| |
| | title = Structured Programming with go to Statements
| |
| | journal = Computing Surveys | |
| | volume = 6
| |
| | issue = 4
| |
| | year = 1974
| |
| | url = http://pplab.snu.ac.kr/courses/adv_pl05/papers/p261-knuth.pdf
| |
| | pages = 261–301
| |
| | doi = 10.1145/356635.356640
| |
| }}</ref>}}
| |
| | |
| [[File:Pointers.svg|thumb|Pointer ''a'' pointing to the memory address associated with variable ''b''. Note that in this particular diagram, the computing architecture uses the same [[address space]] and [[#Formal_description|data primitive]] for both pointers and non-pointers; this need not be the case.]]
| |
| | |
| In [[computer science]], a '''pointer''' is a [[programming language]] object whose value refers directly to (or "'''points''' to") another value stored elsewhere in the [[computer memory]] using its [[Memory address|address]]. For [[high-level programming language]]s, pointers effectively take the place of [[general purpose register]]s in low-level languages such as [[assembly language]] or [[machine code]], but may be in available [[Virtual memory|memory]]. A pointer ''references'' a location in memory, and obtaining the value stored at that location is known as ''dereferencing'' the pointer. A pointer is a simple, more concrete implementation of the more abstract ''[[reference (computer science)|reference]]'' data type. Several languages support some type of pointer, although some have more restrictions on their use than others. As an analogy, a page number in a book's index could be considered a pointer to the corresponding page; dereferencing such a pointer would be done by flipping to the page with the given page number.
| |
| | |
| Pointers to data significantly improve performance for repetitive operations such as traversing [[String (computer science)#String processing algorithms|strings]], [[lookup table]]s, [[control table]]s and [[Tree (data structure)|tree]] structures. In particular, it is often much cheaper in time and space to copy and dereference pointers than it is to copy and access the data to which the pointers point.
| |
| | |
| Pointers are also used to hold the addresses of entry points for [[System call|call]]ed subroutines in [[procedural programming]] and for [[Dynamic link library#Explicit run-time linking|run-time linking to dynamic link libraries (DLLs)]]. In [[object-oriented programming]], [[Function pointer|pointers to functions]] are used for [[Name binding|binding]] [[Method (computer science)|methods]], often using what are called [[virtual method table]]s.
| |
| | |
| While "pointer" has been used to refer to references in general, it more properly applies to data structures whose interface explicitly allows the pointer to be manipulated (arithmetically via ''pointer arithmetic'') as a memory address, as opposed to a [[magic cookie]] or [[capability-based security|capability]] where this is not possible. {{Citation needed|date=May 2009}} Because pointers allow both protected and unprotected access to memory addresses, there are risks associated with using them particularly in the latter case. Primitive pointers are often stored in a format similar to an [[integer]]; however, attempting to dereference or "look up" a pointer whose value was never a valid memory address would cause a program to crash. To alleviate this potential problem, as a matter of [[type safety]], pointers are considered a separate type parameterized by the type of data they point to, even if the underlying representation is an integer. Other measures may also be taken.{{Which|date=October 2011}}
| |
| | |
| == History ==
| |
| | |
| [[Harold Lawson]] is credited with the 1964 invention of the pointer.<ref>[http://books.google.com/books?id=JTYPKxug49IC&pg=PA204&lpg=PA204&dq=Harold+Lawson+pointer&source=web&ots=C5QdVz2xM8&sig=xhh0SWuR-L72H6H9xgEmxD5qzBc&hl=en&ei=lCuLSbTwKY_-0AWErp2iBw&sa=X&oi=book_result&resnum=10&ct=result Milestones in Computer Science and Information Technology]</ref> In 2000, Lawson was presented the Computer Pioneer Award by the [[IEEE]] “[f]or inventing the pointer variable and introducing this concept into PL/I, thus providing for the first time, the capability to flexibly treat linked lists in a general-purpose high level language”.<ref>[http://awards.computer.org/ana/award/viewPastRecipients.action?id=13 IEEE Computer Society awards list]</ref>
| |
| | |
| ==Formal description==
| |
| In [[computer science]], a pointer is a kind of [[Reference (computer science)|reference]].
| |
| | |
| A ''data primitive'' (or just ''primitive'') is any datum that can be read from or written to [[computer memory]] using one memory access (for instance, both a ''[[byte]]'' and a ''[[Word (data type)|word]]'' are primitives).
| |
| | |
| A ''data aggregate'' (or just ''aggregate'') is a group of primitives that are [[Logical address|logically]] contiguous in memory and that are viewed collectively as one datum (for instance, an aggregate could be 3 logically contiguous bytes, the values of which represent the 3 coordinates of a point in space); when an aggregate is entirely composed of the same type of primitive, the aggregate may be called an [[Array data structure|''array'']]; in a sense, a multi-byte ''word'' primitive is an array of bytes, and some programs use words in this way.
| |
| | |
| In the context of these definitions, a ''byte'' is the smallest primitive; each [[memory address]] specifies a different byte. The memory address of the first byte of a datum is considered the memory address (or ''base memory address'') of the entire datum.
| |
| | |
| A ''memory pointer'' (or just ''pointer'') is a primitive, the value of which is intended to be used as a memory address; it is said that ''a pointer points to a memory address''. It is also said that ''a pointer points to a datum [in memory]'' when the pointer's value is the datum's memory address.
| |
| | |
| More generally, a pointer is a kind of [[Reference (computer science)|reference]], and it is said that ''a pointer references a datum stored somewhere in memory''; to obtain that datum is ''to dereference the pointer''. The feature that separates pointers from other kinds of reference is that a pointer's value is meant to be interpreted as a memory address, which is a rather low-level concept.
| |
| | |
| References serve as a level of indirection: A pointer's value determines which memory address (that is, which datum) is to be used in a calculation. Because indirection is a fundamental aspect of algorithms, pointers are often expressed as a fundamental [[data type]] in [[programming language]]s; in [[Type system#Static typing|statically]] (or [[Strongly typed programming language|strongly]]) typed programming languages, the [[Type system|type]] of a pointer determines the type of the datum to which the pointer points.
| |
| | |
| ==Use in data structures==
| |
| When setting up [[data structure]]s like [[List (computing)|lists]], [[Queue (data structure)|queues]] and trees, it is necessary to have pointers to help manage how the structure is implemented and controlled. Typical examples of pointers are start pointers, end pointers, and [[Stack (data structure)|stack]] pointers. These pointers can either be '''absolute''' (the actual [[physical address]] or a [[virtual address]] in [[virtual memory]]) or '''relative''' (an [[offset (computer science)|offset]] from an absolute start address ("base") that typically uses fewer bits than a full address, but will usually require one additional arithmetic operation to resolve).
| |
| | |
| A two-byte offset, containing a 16-bit, unsigned integer, can be used to provide relative addressing for up to 64 kilobytes of a data structure. This can easily be extended to 128K, 256K or 512K if the address pointed to is forced to be on a half-word, word or double-word boundary (but, requiring an additional "shift left" [[bitwise operation]]—by 1, 2 or 3 bits—in order to adjust the offset by a factor of 2, 4 or 8, before its addition to the base address). Generally, though, such schemes are a lot of trouble, and for convenience to the programmer a ''flat address space'' is preferred.
| |
| | |
| A one byte offset, such as the hexadecimal [[ASCII]] value of a character (e.g. X'29') can be used to point to an alternative integer value (or index) in an array (e.g. X'01'). In this way, characters can be very efficiently translated from '[[raw data]]' to a usable sequential [[Array data structure|index]] and then to an absolute address without a [[lookup table]].
| |
| | |
| ==Use in control tables==
| |
| [[Control table]]s, that are used to control [[program flow]], usually make extensive use of pointers. The pointers, usually embedded in a table entry, may, for instance, be used to hold the entry points to [[subroutine]]s to be executed, based on certain conditions defined in the same table entry. The pointers can however be simply indexes to other separate, but associated, tables comprising an array of the actual addresses or the addresses themselves (depending upon the programming language constructs available). They can also be used to point (back) to earlier table entries (as in loop processing) or forward to skip some table entries (as in a [[Switch statement|switch]] or "early" exit from a loop). For this latter purpose, the "pointer" may simply be the table entry number itself and can be transformed into an actual address by simple arithmetic.
| |
| | |
| ==Architectural roots==
| |
| Pointers are a very thin [[Abstraction (computer science)|abstraction]] on top of the addressing capabilities provided by most modern [[Software architecture|architecture]]s. In the simplest scheme, an ''[[Memory address|address]]'', or a numeric [[Array data structure|index]], is assigned to each unit of memory in the system, where the unit is typically either a [[byte]] or a [[Word (data type)|word]], effectively transforming all of memory into a very large [[Array data structure|array]]. Then, if we have an address, the system provides an operation to retrieve the value stored in the memory unit at that address (usually utilizing the machine's [[general purpose register]]s).
| |
| | |
| In the usual case, a pointer is large enough to hold more addresses than there are units of memory in the system. This introduces the possibility that a program may attempt to access an address which corresponds to no unit of memory, either because not enough memory is installed (i.e. beyond the range of available memory) or the architecture does not support such addresses. The first case may, in certain platforms such as the [[x86|Intel x86]] architecture, be called a [[segmentation fault]] (segfault). The second case is possible in the current implementation of [[x86-64|AMD64]], where pointers are 64 bit long and addresses only extend to 48 bits. There, pointers must conform to certain rules (canonical addresses), so if a noncanonical pointer is dereferenced, the processor raises a [[general protection fault]].
| |
| | |
| On the other hand, some systems have more units of memory than there are addresses. In this case, a more complex scheme such as [[memory segmentation]] or [[paging]] is employed to use different parts of the memory at different times. The last incarnations of the x86 architecture support up to 36 bits of physical memory addresses, which were mapped to the 32-bit linear address space through the [[Physical Address Extension|PAE]] paging mechanism. Thus, only 1/16 of the possible total memory may be accessed at a time. Another example in the same computer family was the 16-bit [[protected mode]] of the [[80286]] processor, which, though supporting only 16 MiB of physical memory, could access up to 1 GiB of virtual memory, but the combination of 16-bit address and segment registers made accessing more than 64 KiB in one data structure cumbersome. Some restrictions of ANSI pointer arithmetic may have been due to the segmented memory models of this processor family. {{cn|date=February 2013}}
| |
| | |
| In order to provide a consistent interface, some architectures provide [[memory-mapped I/O]], which allows some addresses to refer to units of memory while others refer to [[device register]]s of other devices in the computer. There are analogous concepts such as file offsets, array indices, and remote object references that serve some of the same purposes as addresses for other types of objects.
| |
| | |
| ==Uses==
| |
| Pointers are directly supported without restrictions in languages such as [[PL/I]], [[C (programming language)|C]], [[C++]], [[Pascal programming language|Pascal]], and most [[assembly language]]s. They are primarily used for constructing [[reference (computer science)|reference]]s, which in turn are fundamental to constructing nearly all [[data structure]]s, as well as in passing data between different parts of a program.
| |
| | |
| In functional programming languages that rely heavily on lists, pointers and references are managed abstractly by the language using internal constructs like [[cons]].
| |
| | |
| When dealing with arrays, the critical [[lookup table|lookup]] operation typically involves a stage called ''address calculation'' which involves constructing a pointer to the desired data element in the array. If the data elements in the array have lengths that are divisible by powers of two, this arithmetic is usually a bit more efficient {{Citation needed|date=January 2014}}. Padding is frequently used as a mechanism for ensuring this is the case, despite the increased memory requirement. In other data structures, such as [[linked list]]s, pointers are used as references to explicitly tie one piece of the structure to another.
| |
| | |
| Pointers are used to pass parameters by reference. This is useful if the programmer wants a function's modifications to a parameter to be visible to the function's caller. This is also useful for returning multiple values from a function.
| |
| | |
| Pointers can also be used to [[Memory allocation|allocate]] and deallocate dynamic variables and arrays in memory. Since a variable will often become redundant after it has served its purpose, it is a waste of memory to keep it, and therefore it is good practice to deallocate it (using the original pointer reference) when it is no longer needed. Failure to do so may result in a ''[[memory leak]]'' (where available free memory gradually, or in severe cases rapidly, diminishes because of an accumulation of numerous redundant memory blocks).
| |
| | |
| ===C pointers===
| |
| The basic syntax to define a pointer is:<ref>[[#c-std|ISO/IEC 9899]], clause 6.7.5.1, paragraph 1.</ref>
| |
| | |
| <source lang="C">int *ptr; </source>
| |
| | |
| This declares <code>ptr</code> as the identifier of an object of the following type:
| |
| * pointer that points to an object of type <code>int</code>
| |
| This is usually stated more succinctly as '<code>ptr</code> is a pointer to <code>int</code>.'
| |
| | |
| Because the C language does not specify an implicit initialization for objects of automatic storage duration,<ref>[[#c-std|ISO/IEC 9899]], clause 6.7.8, paragraph 10.</ref> care should often be taken to ensure that the address to which <code>ptr</code> points is valid; this is why it is sometimes suggested that a pointer be explicitly initialized to the [[#Null_pointer|null pointer]] value, which is traditionally specified in C with the standardized macro <code>NULL</code>:<ref name="c-NULL">[[#c-std|ISO/IEC 9899]], clause 7.17, paragraph 3: ''NULL... which expands to an implementation-defined null pointer constant...''</ref>
| |
| | |
| <source lang="C">int *ptr = NULL;</source>
| |
| | |
| Dereferencing a null pointer in C produces [[undefined behavior]],<ref>[[#c-std|ISO/IEC 9899]], clause 6.5.3.2, paragraph 4, footnote 87: ''If an invalid value has been assigned to the pointer, the behavior of the unary * operator is undefined... Among the invalid values for dereferencing a pointer by the unary * operator are a null pointer...''</ref> which could be catastrophic. However, most implementations{{citation needed|date=July 2011}} simply halt execution of the program in question, usually with a segmentation fault.
| |
| | |
| However, initializing pointers unnecessarily could hinder program analysis, thereby hiding bugs.
| |
| | |
| In any case, once a pointer has been declared, the next logical step is for it to point at something:
| |
| | |
| <source lang="C">
| |
| int a = 5;
| |
| int *ptr = NULL;
| |
| | |
| ptr = &a;
| |
| </source>
| |
| | |
| This assigns the value of the address of <code>a</code> to <code>ptr</code>. For example, if <code>a</code> is stored at memory location of 0x8130 then the value of <code>ptr</code> will be 0x8130 after the assignment. To dereference the pointer, an asterisk is used again:
| |
| | |
| <source lang="C">*ptr = 8;</source>
| |
| | |
| This means take the contents of <code>ptr</code> (which is 0x8130), "locate" that address in memory and set its value to 8.
| |
| If <code>a</code> is later accessed again, its new value will be 8.
| |
| | |
| This example may be more clear if memory is examined directly.
| |
| Assume that <code>a</code> is located at address 0x8130 in memory and <code>ptr</code> at 0x8134; also assume this is a 32-bit machine such that an int is 32-bits wide. The following is what would be in memory after the following code snippet is executed:
| |
| | |
| <source lang="C">
| |
| int a = 5;
| |
| int *ptr = NULL;
| |
| </source>
| |
| | |
| :{| class="wikitable"
| |
| ! Address !! Contents
| |
| |-
| |
| | '''0x8130''' || 0x00000005
| |
| |-
| |
| | '''0x8134''' || 0x00000000
| |
| |}
| |
| | |
| (The NULL pointer shown here is 0x00000000.)
| |
| By assigning the address of <code>a</code> to <code>ptr</code>:
| |
| | |
| <source lang="C">
| |
| ptr = &a;
| |
| </source>
| |
| | |
| yields the following memory values:
| |
| | |
| :{| class="wikitable"
| |
| ! Address !! Contents
| |
| |-
| |
| | '''0x8130''' || 0x00000005
| |
| |-
| |
| | '''0x8134''' || 0x00008130
| |
| |}
| |
| | |
| Then by dereferencing <code>ptr</code> by coding:
| |
| | |
| <source lang="C">
| |
| *ptr = 8;
| |
| </source>
| |
| | |
| the computer will take the contents of <code>ptr</code> (which is 0x8130), 'locate' that address, and assign 8 to that location yielding the following memory:
| |
| | |
| :{| class="wikitable"
| |
| ! Address !! Contents
| |
| |-
| |
| | '''0x8130''' || 0x00000008
| |
| |-
| |
| | '''0x8134''' || 0x00008130
| |
| |}
| |
| | |
| Clearly, accessing <code>a</code> will yield the value of 8 because the previous instruction modified the contents of <code>a</code> by way of the pointer <code>ptr</code>.
| |
| | |
| ===C arrays===
| |
| In C, array indexing is formally defined in terms of pointer arithmetic; that is, the language specification requires that <code>array[i]</code> be equivalent to <code>*(array + i)</code>.<ref name="Plauger1992">{{cite book |title=ANSI and ISO Standard C Programmer's Reference | last=Plauger |first=P J |authorlink=P. J. Plauger |coauthors = Brodie, Jim |year=1992 |publisher=Microsoft Press|location=Redmond, WA |isbn=1-55615-359-7 |pages=108, 51 |quote=An array type does not contain additional holes because all other types pack tightly when composed into arrays ''[at page 51]''}}</ref> Thus in C, arrays can be thought of as pointers to consecutive areas of memory (with no gaps),<ref name="Plauger1992" /> and the syntax for accessing arrays is identical for that which can be used to dereference pointers. For example, an array <code>array</code> can be declared and used in the following manner:
| |
| | |
| <source lang="C">
| |
| int array[5]; /* Declares 5 contiguous integers */
| |
| int *ptr = array; /* Arrays can be used as pointers */
| |
| ptr[0] = 1; /* Pointers can be indexed with array syntax */
| |
| *(array + 1) = 2; /* Arrays can be dereferenced with pointer syntax */
| |
| *(1 + array) = 3; /* Pointer addition is commutative */
| |
| 2[array] = 4; /* Subscript operator is commutative */
| |
| </source>
| |
| | |
| This allocates a block of five integers and names the block <code>array</code>, which acts as a pointer to the block. Another common use of pointers is to point to dynamically allocated memory from [[malloc]] which returns a consecutive block of memory of no less than the requested size that can be used as an array.
| |
| | |
| While most operators on arrays and pointers are equivalent, it is important to note that the <code>[[Sizeof#Using_sizeof_with_arrays|sizeof]]</code> operator will differ. In this example, <code>sizeof(array)</code> will evaluate to <code>5*sizeof(int)</code> (the size of the array), while <code>sizeof(ptr)</code> will evaluate to <code>sizeof(int*)</code>, the size of the pointer itself.
| |
| | |
| Default values of an array can be declared like:
| |
| | |
| <source lang="C">
| |
| int array[5] = {2,4,3,1,5};
| |
| </source>
| |
| | |
| If you assume that <code>array</code> is located in memory starting at address 0x1000 on a 32-bit [[Endianness#Little-endian|little-endian]] machine then memory will contain the following (values are in [[hexadecimal]], like the addresses):
| |
| | |
| :{| class="wikitable"
| |
| |-
| |
| | || '''0''' || '''1''' || '''2''' || '''3'''
| |
| |-
| |
| | '''1000''' || 2 || 0 || 0 || 0
| |
| |-
| |
| | '''1004''' || 4 || 0 || 0 || 0
| |
| |-
| |
| | '''1008''' || 3 || 0 || 0 || 0
| |
| |-
| |
| | '''100C''' || 1 || 0 || 0 || 0
| |
| |-
| |
| | '''1010''' || 5 || 0 || 0 || 0
| |
| |}
| |
| | |
| Represented here are five integers: 2, 4, 3, 1, and 5. These five integers occupy 32 bits (4 bytes) each with the least-significant byte stored first (this is a little-endian [[CPU architecture]]) and are stored consecutively starting at address 0x1000.
| |
| | |
| The syntax for C with pointers is:
| |
| * <code>array</code> means 0x1000
| |
| * <code>array+1</code> means 0x1004 (note that the "+1" really means to add one times the size of an <code>int</code> (4 bytes) not literally "plus one")
| |
| * <code>*array</code> means to dereference the contents of <code>array</code>. Considering the contents as a memory address (0x1000), look up the value at that location (0x0002).
| |
| * <code>array[i]</code> means element number <code>i</code>, 0-based, of <code>array</code> which is translated into <code>*(array + i)</code>
| |
| | |
| The last example is how to access the contents of <code>array</code>. Breaking it down:
| |
| * <code>array + i</code> is the memory location of the (i+1)<sup>th</sup> element of <code>array</code>
| |
| * <code>*(array + i)</code> takes that memory address and dereferences it to access the value.
| |
| | |
| E.g. <code>array[3]</code> is synonymous with <code>*(array+3)</code>, meaning <code>*(0x1000 + 3*sizeof(int))</code>, which says "dereference the value stored at <code>0x100C</code>", in this case <code>0x0001</code>.
| |
| | |
| ===C linked list===
| |
| Below is an example definition of a [[linked list]] in C.
| |
| | |
| <source lang="C">
| |
| /* the empty linked list is represented by NULL
| |
| * or some other sentinel value */
| |
| #define EMPTY_LIST NULL
| |
| | |
| struct link {
| |
| void *data; /* data of this link */
| |
| struct link *next; /* next link; EMPTY_LIST if there is none */
| |
| };
| |
| </source>
| |
| | |
| Note that this pointer-recursive definition is essentially the same as the reference-recursive definition from the [[Haskell (programming language)|Haskell programming language]]:
| |
| | |
| <source lang="haskell">
| |
| data Link a = Nil
| |
| | Cons a (Link a)
| |
| </source>
| |
| <code>Nil</code> is the empty list, and <code>Cons a (Link a)</code> is a [[cons]] cell of type <code>a</code> with another link also of type <code>a</code>.
| |
| | |
| The definition with references, however, is type-checked and does not use potentially confusing signal values. For this reason, data structures in C are usually dealt with via [[wrapper function]]s, which are carefully checked for correctness.
| |
| | |
| ===Pass-by-address using pointers===
| |
| Pointers can be used to pass variables by their address, allowing their value to be changed. For example consider the following [[C (programming language)|C]] code:
| |
| | |
| <source lang="C">
| |
| /* a copy of the int n can be changed within the function without affecting the calling code */
| |
| void passByValue(int n) {
| |
| n = 12;
| |
| }
| |
| | |
| /* a pointer to m is passed instead. No copy of m itself is created */
| |
| void passByAddress(int *m) {
| |
| *m = 14;
| |
| }
| |
| | |
| int main(void) {
| |
| int x = 3;
| |
| | |
| /* pass a copy of x's value as the argument */
| |
| passByValue(x);
| |
| // the value was changed inside the function, but x is still 3 here afterwards
| |
| | |
| /* pass x's address as the argument */
| |
| passByAddress(&x);
| |
| // x was actually changed by the function and is now equal to 14 here
| |
| | |
| return 0;
| |
| }
| |
| </source>
| |
| | |
| ===Dynamic memory allocation===
| |
| Pointers are used to store and manage the addresses of [[dynamic memory allocation|dynamically allocated]] blocks of memory. Such blocks are used to store data objects or arrays of objects. Most structured and object-oriented languages provide an area of memory, called the ''heap'' or ''free store'', from which objects are dynamically allocated.
| |
| | |
| The example C code below illustrates how structure objects are dynamically allocated and referenced. The [[standard C library]] provides the function [[malloc|<code>malloc()</code>]] for allocating memory blocks from the heap. It takes the size of an object to allocate as a parameter and returns a pointer to a newly allocated block of memory suitable for storing the object, or it returns a null pointer if the allocation failed.
| |
| | |
| <source lang="C">
| |
| /* Parts inventory item */
| |
| struct Item {
| |
| int id; /* Part number */
| |
| char * name; /* Part name */
| |
| float cost; /* Cost */
| |
| };
| |
| | |
| /* Allocate and initialize a new Item object */
| |
| struct Item * make_item(const char *name) {
| |
| struct Item * item;
| |
| | |
| /* Allocate a block of memory for a new Item object */
| |
| item = (struct Item *)malloc(sizeof(struct Item));
| |
| if (item == NULL)
| |
| return NULL;
| |
| | |
| /* Initialize the members of the new Item */
| |
| memset(item, 0, sizeof(struct Item));
| |
| item->id = -1;
| |
| item->name = NULL;
| |
| item->cost = 0.0;
| |
| | |
| /* Save a copy of the name in the new Item */
| |
| item->name = (char *)malloc(strlen(name) + 1);
| |
| if (item->name == NULL) {
| |
| free(item);
| |
| return NULL;
| |
| }
| |
| strcpy(item->name, name);
| |
| | |
| /* Return the newly created Item object */
| |
| return item;
| |
| }
| |
| </source>
| |
| | |
| The code below illustrates how memory objects are dynamically deallocated, i.e., returned to the heap or free store. The standard C library provides the function [[free()|<code>free()</code>]] for deallocating a previously allocated memory block and returning it back to the heap.
| |
| | |
| <source lang="C">
| |
| /* Deallocate an Item object */
| |
| void destroy_item(struct Item *item) {
| |
| /* Check for a null object pointer */
| |
| if (item == NULL)
| |
| return;
| |
| | |
| /* Deallocate the name string saved within the Item */
| |
| if (item->name != NULL) {
| |
| free(item->name);
| |
| item->name = NULL;
| |
| }
| |
| | |
| /* Deallocate the Item object itself */
| |
| free(item);
| |
| }
| |
| </source>
| |
| | |
| ===Memory-mapped hardware===
| |
| On some computing architectures, pointers can be used to directly manipulate memory or memory-mapped devices.
| |
| | |
| Assigning addresses to pointers is an invaluable tool when programming [[Microcontroller|microcontrollers]]. Below is a simple example declaring a pointer of type int and initialising it to a [[hexadecimal]] address in this example the constant <code>0x7FFF</code>:
| |
| | |
| <source lang="C">
| |
| int *hardware_address = (int *)0x7FFF;
| |
| </source>
| |
| | |
| In the mid 80s, using the [[BIOS]] to access the video capabilities of PCs was slow. Applications that were display-intensive typically used to access [[Color Graphics Adapter|CGA]] video memory directly by casting the [[hexadecimal]] constant <code>0xB8000</code> to a pointer to an array of 80 unsigned 16-bit int values. Each value consisted of an [[ASCII]] code in the low byte, and a colour in the high byte. Thus, to put the letter 'A' at row 5, column 2 in bright white on blue, one would write code like the following:
| |
| | |
| <source lang="C">
| |
| #define VID ((unsigned short (*)[80])0xB8000)
| |
| | |
| void foo() {
| |
| VID[4][1] = 0x1F00 | 'A';
| |
| }
| |
| </source>
| |
| | |
| ==Typed pointers and casting==
| |
| In many languages, pointers have the additional restriction that the object they point to has a specific [[datatype|type]]. For example, a pointer may be declared to point to an [[integer]]; the language will then attempt to prevent the programmer from pointing it to objects which are not integers, such as [[floating-point number]]s, eliminating some errors.
| |
| | |
| For example, in C
| |
| | |
| <source lang="C">
| |
| int *money;
| |
| char *bags;
| |
| </source>
| |
| | |
| <code>money</code> would be an integer pointer and <code>bags</code> would be a char pointer.
| |
| The following would yield a compiler warning of "assignment from incompatible pointer type" under [[GNU Compiler Collection|GCC]]
| |
| | |
| <source lang="C">
| |
| bags = money;
| |
| </source>
| |
| | |
| because <code>money</code> and <code>bags</code> were declared with different types.
| |
| To suppress the compiler warning, it must be made explicit that you do indeed wish to make the assignment by [[Type conversion|typecasting]] it
| |
| | |
| <source lang="C">
| |
| bags = (char *)money;
| |
| </source>
| |
| | |
| which says to cast the integer pointer of <code>money</code> to a char pointer and assign to <code>bags</code>.
| |
| | |
| A 2005 draft of the C standard requires that casting a pointer derived from one type to one of another type should maintain the alignment correctness for both types (6.3.2.3 Pointers, par. 7):<ref>[http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf WG14 N1124], [http://www.open-std.org/jtc1/sc22/wg14/www/standards.html C – Approved standards: ISO/IEC 9899 – Programming languages – C], 2005-05-06.</ref>
| |
| <source lang="C">
| |
| char *external_buffer = "abcdef";
| |
| int *internal_data;
| |
| | |
| internal_data = (int *)external_buffer; // UNDEFINED BEHAVIOUR if "the resulting pointer
| |
| // is not correctly aligned"
| |
| </source>
| |
| | |
| In languages that allow pointer arithmetic, arithmetic on pointers takes into account the size of the type. For example, adding an integer number to a pointer produces another pointer that points to an address that is higher by that number times the size of the type. This allows us to easily compute the address of elements of an array of a given type, as was shown in the C arrays example above. When a pointer of one type is cast to another type of a different size, the programmer should expect that pointer arithmetic will be calculated differently. In C, for example, if the <code>money</code> array starts at 0x2000 and <code>sizeof(int)</code> is 4 bytes whereas <code>sizeof(char)</code> is 1 bytes, then <code>(money+1)</code> will point to 0x2004 but <code>(bags+1)</code> will point to 0x2001. Other risks of casting include loss of data when "wide" data is written to "narrow" locations (e.g. <code>bags[0]=65537;</code>), unexpected results when [[Bitwise operation#Bit shifts|bit-shifting]] values, and comparison problems, especially with signed vs unsigned values.
| |
| | |
| Although it is impossible in general to determine at compile-time which casts are safe, some languages store [[run-time type information]] which can be used to confirm that these dangerous casts are valid at runtime. Other languages merely accept a conservative approximation of safe casts, or none at all.
| |
| | |
| ==Making pointers safer==
| |
| Because a pointer allows a program to attempt to access an object that may not be defined, pointers can be the source of a variety of [[software bug|programming errors]]. However, the usefulness of pointers is so great that it can be difficult to perform programming tasks without them. Consequently, many languages have created constructs designed to provide some of the useful features of pointers without some of their [[Anti-pattern|pitfalls]], also sometimes referred to as "pointer hazards".
| |
| | |
| One major problem with pointers is that as long as they can be directly manipulated as a number, they can be made to point to unused addresses or to data which is being used for other purposes. Many languages, including most [[functional programming language]]s and recent [[imperative languages]] like [[Java (programming language)|Java]], replace pointers with a more opaque type of reference, typically referred to as simply a ''reference'', which can only be used to refer to objects and not [[pointer arithmetic|manipulated as numbers]], preventing this type of error. Array indexing is handled as a special case.
| |
| | |
| A pointer which does not have any address assigned to it is called a [[wild pointer]]. Any attempt to use such uninitialized pointers can cause unexpected behavior, either because the initial value is not a valid address, or because using it may damage other parts of the program. The result is often a [[segmentation fault]], [[storage violation]] or [[wild branch]] (if used as a function pointer or branch address).
| |
| | |
| In systems with explicit memory allocation, it is possible to create a [[dangling pointer]] by deallocating the memory region it points into. This type of pointer is dangerous and subtle because a deallocated memory region may contain the same data as it did before it was deallocated but may be then reallocated and overwritten by unrelated code, unknown to the earlier code. Languages with [[garbage collection (computer science)|garbage collection]] prevent this type of error because deallocation is performed automatically when there are no more references in scope.
| |
| | |
| Some languages, like [[C++]], support [[smart pointer]]s, which use a simple form of [[reference counting]] to help track allocation of dynamic memory in addition to acting as a reference. In the absence of reference cycles, where an object refers to itself indirectly through a sequence of smart pointers, these eliminate the possibility of dangling pointers and memory leaks. [[Borland Delphi|Delphi]] strings support reference counting natively.
| |
| | |
| ==Null pointer==
| |
| A '''[[wikt:null|null]] pointer''' has a value reserved for indicating that the pointer does not refer to a valid object. Null pointers are routinely used to represent conditions such as the end of a [[List (computing)|list]] of unknown length or the failure to perform some action; this use of null pointers can be compared to [[nullable type]]s and to the ''Nothing'' value in an [[option type]].
| |
| | |
| Null pointers are often considered similar to null values in [[relational database]]s, but they have somewhat different semantics. A null pointer in most programming languages means "no value", while a null value in relational database means "unknown value". This leads to important differences in practice: two null pointers are considered equal in most programming languages, but two null values in relational databases are not (since they represent unknown values, it is unknown whether they are equal).
| |
| | |
| In some programming language environments (at least one proprietary Lisp implementation, for example),{{citation needed|date=September 2011}} the value used as the null pointer (called <code>nil</code> in Lisp) may actually be a pointer to a block of internal data useful to the implementation (but not explicitly reachable from user programs), thus allowing the same register to be used as a useful constant and a quick way of accessing implementation internals. This is known as the ''<code>nil</code> vector''.
| |
| | |
| In C, two null pointers of any type are guaranteed to compare equal.<ref>[[#c-std|ISO/IEC 9899]], clause 6.3.2.3, paragraph 4.</ref> The macro <code>NULL</code> is defined as an implementation-defined null pointer constant,<ref name="c-NULL"/> which in C99 can be portably expressed as the integer value <code>0</code> converted implicitly or explicitly to the type <code>void*</code>.<ref>[[#c-std|ISO/IEC 9899]], clause 6.3.2.3, paragraph 3.</ref>
| |
| | |
| Dereferencing the NULL pointer typically results in an attempted read or write from memory that is not mapped - triggering a [[segmentation fault]] or access violation. This may represent itself to the developer as a program crash, or be transformed into an exception that can be caught. There are, however, certainly circumstances where this is not the case. For example, in x86-real mode, the address 0000:0000 is readable and usually writable, hence dereferencing the null pointer is a perfectly valid but typically unwanted action that may lead to undefined but non-crashing behaviour in the application. Note also that there are occasions when dereferencing the NULL ''is'' intentional and well defined; for example BIOS code written in C for 16-bit real-mode x86 devices may write the IDT at physical address 0 of the machine by dereferencing a NULL pointer for writing.
| |
| | |
| In C++, while the <code>NULL</code> macro was inherited from C, the integer literal for zero has been traditionally preferred to represent a null pointer constant.<ref>{{cite book |last1=Stroustrup |first=Bjarne |authorlink1=Bjarne_Stroustrup |title=[[The C++ Programming Language]] |edition=14th printing of 3rd |date=March 2001 |publisher=Addison–Wesley |location=United States and Canada |isbn=0-201-88954-4 |page=88 |chapter=Chapter 5: Pointers, Arrays, and Structures: 5.1.1: Zero |quote=In C, it has been popular to define a macro <code>NULL</code> to represent the zero pointer. Because of C++'s tighter type checking, the use of plain 0, rather than any suggested <code>NULL</code> macro, leads to fewer problems. If you feel you must define <code>NULL</code>. use
| |
| : <code>const int NULL = 0;</code>
| |
| The <code>const</code> qualifier (§5.4) prevents accidental redefinition of <code>NULL</code> and ensures that <code>NULL</code> can be used where a constant is required.}}</ref> However, C++11 has introduced an explicit [[C++11#Null_pointer_constant|<code>nullptr</code> constant]] to be used instead.
| |
| | |
| A null pointer should not be confused with an uninitialized pointer: A null pointer is guaranteed to compare unequal to any pointer that points to a valid object. However, depending on the language and implementation, an uninitialized pointer has either an indeterminate (random or meaningless) value or a specific value that is not necessarily any kind of null pointer constant.
| |
| | |
| In 2009 [[C.A.R. Hoare]] stated<ref>{{cite web
| |
| |url=http://qconlondon.com/london-2009/presentation/Null+References:+The+Billion+Dollar+Mistake
| |
| |title=Null References: The Billion Dollar Mistake
| |
| |location=QCon London
| |
| |year=2009
| |
| |author=Tony Hoare}}</ref><ref>{{cite web
| |
| |url=http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare
| |
| |title=Null References: The Billion Dollar Mistake
| |
| |publisher=InfoQ.com
| |
| |date=2009-08-25
| |
| |author=[[Tony Hoare]]}}</ref>
| |
| that he invented the null reference in 1965 as part of the [[Algol W]] language, although NIL had existed in [[Lisp]] since 1959. In that 2009 reference Hoare describes his invention as a "billion-dollar mistake":
| |
| {{quote|I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language ([[ALGOL W]]). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.}}
| |
| | |
| Because a null pointer does not point to a meaningful object, an attempt to dereference a null pointer usually (but not always) causes a run-time error or immediate program crash.
| |
| * In C, the behavior of dereferencing a null pointer is undefined.<ref>[[#c-std|ISO/IEC 9899]], clause 6.5.3.2, paragraph 4.</ref> Many implementations cause such code to result in the program being halted with a [[segmentation fault]], because the null pointer representation is chosen to be an address that is never allocated by the system for storing objects. However, this behavior is not universal.
| |
| * In Java, access to a null reference triggers a {{Javadoc:SE|java/lang|NullPointerException}} (NPE), which can be caught by error handling code, but the preferred practice is to ensure that such exceptions never occur.
| |
| * In .NET, access to null reference triggers a NullReferenceException to be thrown. Although catching these is generally considered bad practice, this exception type can be caught and handled by the program.
| |
| * In [[Objective-C]], messages may be sent to a <code>nil</code> object (which is essentially a null pointer) without causing the program to be interrupted; the message is simply ignored, and the return value (if any) is <code>nil</code> or <code>0</code>, depending on the type.<ref>''The Objective-C 2.0 Programming Language'', [http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html#//apple_ref/doc/uid/TP30001163-CH11-SW7 section "Sending Messages to nil"].</ref>
| |
| In languages with a [[tagged architecture]], a possibly null pointer can be replaced with a [[tagged union]] which enforces explicit handling of the exceptional case; in fact, a possibly null pointer can be seen as a [[tagged pointer]] with a computed tag.
| |
| | |
| == Autorelative pointer ==
| |
| The term ''autorelative'' pointer may refer to a pointer whose value is interpreted as an offset from the address of the pointer itself; thus, if a data structure, <math>M</math>, has an autorelative pointer member, <math>p</math>, that points to some portion of <math>M</math> itself, then <math>M</math> may be relocated in memory without having to update the value of <math>p</math>.<ref>{{cite patent
| |
| | country = us
| |
| | number = 6625718
| |
| | status = patent
| |
| | title = Pointers that are relative to their own present locations
| |
| | gdate = 2003-09-23
| |
| | fdate = 1998-02-05
| |
| | inventor = Steiner, Robert C. (Broomfield, CO)
| |
| | invent1 = Steiner, Robert
| |
| | assign1 = Avaya Technology Corp. (Basking Ridge, NJ)
| |
| }}
| |
| </ref>
| |
| | |
| The cited patent <!-- This "patent" cannot be valid as this is simply an attempt to essentially rename the meaning of the word "offset" that has been in constant use since the dawn of computers !-->also uses the term ''self-relative'' pointer to mean the same thing. However, the meaning of that term has been used in other ways:
| |
| * It is often used to mean an offset from the address of a structure rather than from the address of the pointer itself.{{citation needed|date=September 2011}}
| |
| * It has been used to mean a pointer containing its own address, which can be useful for reconstructing in any arbitrary region of memory a collection of data structures that point to each other.<ref>{{cite patent
| |
| | country = us
| |
| | number = 6115721
| |
| | status = patent
| |
| | title = System and method for database save and restore using self-pointers
| |
| | gdate = 2000-09-05
| |
| | fdate = 1998-06-23
| |
| | inventor = Nagy, Michael (Tampa, FL)
| |
| | invent1 = Nagy, Michael
| |
| | assign1 = IBM (Armonk, NY)
| |
| }}
| |
| </ref>
| |
| | |
| == Based pointer ==
| |
| A '''based pointer''' is a pointer whose value is an offset from the value of another pointer. This can be used to store and load blocks of data, assigning the address of the beginning of the block to the base pointer.<ref>[http://msdn.microsoft.com/en-us/library/57a97k4e.aspx Based Pointers]</ref>
| |
| | |
| ==Multiple indirection==
| |
| In some languages, a pointer can reference another pointer, requiring multiple dereference operations to get to the original value. While each level of indirection may add a performance cost, it is sometimes necessary in order to provide correct behavior for complex [[data structures]]. For example, in C it is typical to define a [[linked list]] in terms of an element that contains a pointer to the next element of the list:
| |
| | |
| <source lang="C">
| |
| struct element
| |
| {
| |
| struct element * next;
| |
| int value;
| |
| };
| |
| | |
| struct element * head = NULL;
| |
| </source>
| |
| | |
| This implementation uses a pointer to the first element in the list as a surrogate for the entire list. If a new value is added to the beginning of the list, <code>head</code> has to be changed to point to the new element. Since C arguments are always passed by value, using double indirection allows the insertion to be implemented correctly, and has the desirable side-effect of eliminating special case code to deal with insertions at the front of the list:
| |
| | |
| <source lang="C">
| |
| // Given a sorted list at *head, insert the element item at the first
| |
| // location where all earlier elements have lesser or equal value.
| |
| void insert(struct element **head, struct element *item)
| |
| {
| |
| struct element ** p; // p points to a pointer to an element
| |
| | |
| for (p = head; *p != NULL; p = &(*p)->next)
| |
| {
| |
| if (item->value <= (*p)->value)
| |
| break;
| |
| }
| |
| item->next = *p;
| |
| *p = item;
| |
| }
| |
| | |
| // Caller does this:
| |
| insert(&head, item);
| |
| </source>
| |
| | |
| In this case, if the value of <code>item</code> is less than that of <code>head</code>, the caller's <code>head</code> is properly updated to the address of the new item.
| |
| | |
| A basic example is in the [[argv]] argument to the [[main function#C and C.2B.2B|main function in C (and C++)]], which is given in the prototype as <code>char **argv</code> – this is because the variable <code>argv</code> itself is a pointer to an array of strings (an array of arrays), so <code>*argv</code> is a pointer to the 0th string (by convention the name of the program), and <code>**argv</code> is the 0th character of the 0th string.
| |
| | |
| ==Function Pointer==
| |
| In some languages, a pointer can reference a function. The [[function pointer]] will store the address location of the function to be invoked. While this facility can be used to call functions dynamically, it is often the favorite method of virus and other malicious software writers. For example, in C it is possible to define a function pointer.
| |
| | |
| <source lang="C">
| |
| int a, b, x, y;
| |
| int sum(int n1, int n2); // Function with two integer parameters returning an integer value
| |
| int (*fp)(int, int); // Function pointer which can point to a function like sum
| |
| | |
| fp = ∑ // fp now points to function sum
| |
| x = (*fp)(a, b); // Calls function sum with arguments a and b
| |
| y = sum(a, b); // Calls function sum with arguments a and b
| |
| </source>
| |
| | |
| ==Wild pointers==
| |
| A '''wild pointer''' is a pointer that has not been initialized (that is, a wild pointer has not had any address assigned to it) and may make a program crash or behave oddly. In the [[Pascal programming language|Pascal]] or [[C (programming language)|C programming languages]], pointers that are not specifically initialized may point to unpredictable addresses in memory.
| |
| | |
| The following example code shows a wild pointer:
| |
| | |
| <source lang="C">
| |
| int func(void)
| |
| {
| |
| char *p1 = malloc(sizeof(char)); /* (undefined) value of some place on the heap */
| |
| char *p2; /* wild (uninitialized) pointer */
| |
| *p1 = 'a'; /* This is OK, assuming malloc() has not returned NULL. */
| |
| *p2 = 'b'; /* This invokes undefined behavior */
| |
| }
| |
| </source>
| |
| | |
| Here, <code>p2</code> may point to anywhere in memory, so performing the assignment <code>*p2 = 'b'</code> can corrupt an unknown area of memory or trigger a [[segmentation fault]].
| |
| | |
| ==Wild branch==
| |
| Where a pointer is used as the address of the entry point to a program or start of a [[subroutine]] and is also either uninitialized or corrupted, if a call or [[unconditional branch|jump]] is nevertheless made to this address, a "[[wild branch]]" is said to have occurred. The consequences are usually unpredictable and the error may present itself in several different ways depending upon whether or not the pointer is a "valid" address and whether or not there is (coincidentally) a valid instruction (opcode) at that address. The detection of a wild branch can present one of the most difficult and frustrating debugging exercises since much of the evidence may already have been destroyed beforehand or by execution of one or more inappropriate instructions at the branch location. If available, an [[instruction set simulator]] can usually not only detect a wild branch before it takes effect, but also provide a complete or partial trace of its history.
| |
| | |
| ==Simulation using an array index==
| |
| It is possible to simulate pointer behavior using an index to an (normally one-dimensional) array.
| |
| | |
| Primarily for languages which do not support pointers explicitly but ''do'' support arrays, the [[Array data type|array]] can be thought of and processed as if it were the entire memory range (within the scope of the particular array) and any index to it can be thought of as equivalent to a [[general purpose register]] in assembly language (that points to the individual bytes but whose actual value is relative to the start of the array, not its absolute address in memory).
| |
| Assuming the array is, say, a contiguous 16 [[megabyte]] character [[data structure]], individual bytes (or a [[String (computer science)|string]] of contiguous bytes within the array) can be directly addressed and manipulated using the name of the array with a 31 bit unsigned [[integer]] as the simulated pointer (this is quite similar to the ''C arrays'' example shown above). Pointer arithmetic can be simulated by adding or subtracting from the index, with minimal additional overhead compared to genuine pointer arithmetic.
| |
| | |
| It is even theoretically possible, using the above technique, together with a suitable [[instruction set simulator]] to simulate ''any'' [[machine code]] or the intermediate ([[byte code]]) of ''any'' processor/language in another language that does not support pointers at all (for example [[Java (programming language)|Java]] / [[JavaScript]]). To achieve this, the [[binary numeral system|binary]] code can initially be loaded into contiguous bytes of the array for the simulator to "read", interpret and action entirely within the memory contained of the same array.
| |
| If necessary, to completely avoid [[buffer overflow]] problems, [[bounds checking]] can usually be actioned for the compiler (or if not, hand coded in the simulator).
| |
| | |
| ==Support in various programming languages==
| |
| | |
| ===Ada===
| |
| [[Ada programming language|Ada]] is a strongly typed language where all pointers are typed and only safe type conversions are permitted. All pointers are by default initialized to <code>null</code>, and any attempt to access data through a <code>null</code> pointer causes an [[Exception handling|exception]] to be raised. Pointers in Ada are called ''[[access type]]s''. Ada 83 did not permit arithmetic on access types (although many compiler vendors provided for it as a non-standard feature), but Ada 95 supports “safe” arithmetic on access types via the package <code>System.Storage_Elements</code>.
| |
| | |
| ===BASIC===
| |
| Several old versions of [[BASIC]] for the Windows platform had support for STRPTR() to return the address of a string, and for VARPTR() to return the address of a variable. Visual Basic 5 also had support for OBJPTR() to return the address of an object interface, and for an ADDRESSOF operator to return the address of a function. The types of all of these are integers, but their values are equivalent to those held by pointer types.
| |
| | |
| Newer dialects of [[BASIC]], such as [[FreeBASIC]] or [[BlitzMax]], have exhaustive pointer implementations, however. In FreeBASIC, arithmetic on <code>ANY</code> pointers (equivalent to C's <code>void*</code>) are treated as though the <code>ANY</code> pointer was a byte width. <code>ANY</code> pointers cannot be dereferenced, as in C. Also, casting between <code>ANY</code> and any other type's pointers will not generate any warnings.
| |
| | |
| <source lang="freebasic">
| |
| dim as integer f = 257
| |
| dim as any ptr g = @f
| |
| dim as integer ptr i = g
| |
| assert(*i = 257)
| |
| assert( (g + 4) = (@f + 1) )
| |
| </source>
| |
| | |
| ===C and C++===
| |
| In [[C (programming language)|C]] and [[C++]] pointers are variables that store addresses and can be ''null''. Each pointer has a type it points to, but one can freely cast between pointer types (but not between a function pointer and non-function pointer type). A special pointer type called the “void pointer” allows pointing to any (non-function) variable type, but is limited by the fact that it cannot be dereferenced directly. The address itself can often be directly manipulated by casting a pointer to and from an integral type of sufficient size, though the results are implementation-defined and may indeed cause undefined behavior; while earlier C standards did not have an integral type that was guaranteed to be large enough, [[C99]] specifies the <code>uintptr_t</code> ''typedef name'' defined in <code>[[Stdint.h#Integers wide enough to hold pointers|<stdint.h>]]</code>, but an implementation need not provide it.
| |
| | |
| [[C++]] fully supports C pointers and C typecasting. It also supports a new group of typecasting operators to help catch some unintended dangerous casts at compile-time. Since [[C++11]], the [[C++ standard library]] also provides [[smart pointer]]s (<code>unique_ptr</code>, <code>shared_ptr</code> and <code>weak_ptr</code>) which can be used in some situations as a safe alternative to primitive C pointers. C++ also supports another form of reference, quite different from a pointer, called simply a ''[[reference (C++)|reference]]'' or ''reference type''.
| |
| | |
| '''Pointer arithmetic''', that is, the ability to modify a pointer's target address with arithmetic operations (as well as magnitude comparisons), is restricted by the language standard to remain within the bounds of a single array object (or just after it), and will otherwise invoke [[undefined behavior]]. Adding or subtracting from a pointer moves it by a multiple of the size of the [[datatype]] it points to. For example, adding 1 to a pointer to 4-byte integer values will increment the pointer by 4. This has the effect of incrementing the pointer to point at the next element in a contiguous array of integers—which is often the intended result. Pointer arithmetic cannot be performed on <code>void</code> pointers because the [[void type]] has no size, and thus the pointed address can not be added to, although [[GNU Compiler Collection|gcc]] and other compilers will perform byte arithmetic on <code>void*</code> as a non-standard extension. For working "directly" with bytes they usually cast pointers to <code>BYTE*</code>, or <code>unsigned char*</code> if <code>BYTE</code> is not defined in the standard library used.
| |
| | |
| Pointer arithmetic provides the programmer with a single way of dealing with different types: adding and subtracting the number of elements required instead of the actual offset in bytes. (though the <code>char</code> pointer, <code>char</code> being defined as always having a size of one byte, allows the element offset of pointer arithmetic to in practice be equal to a byte offset) In particular, the C definition explicitly declares that the syntax <code>a[n]</code>, which is the <code>n</code>-th element of the array <code>a</code>, is equivalent to <code>*(a+n)</code>, which is the content of the element pointed by <code>a+n</code>. This implies that <code>n[a]</code> is equivalent to <code>a[n]</code>, and one can write, e.g., <code>a[3]</code> or <code>3[a]</code> equally well to access the fourth element of an array <code>a</code>.
| |
| | |
| While powerful, pointer arithmetic can be a source of [[computer bug]]s. It tends to confuse novice [[programmer]]s, forcing them into different contexts: an expression can be an ordinary arithmetic one or a pointer arithmetic one, and sometimes it is easy to mistake one for the other. In response to this, many modern high-level computer languages (for example [[Java (programming language)|Java]]) do not permit direct access to memory using addresses. Also, the safe C dialect [[Cyclone programming language|Cyclone]] addresses many of the issues with pointers. See [[C (programming language)#Pointers|C programming language]] for more discussion.
| |
| | |
| '''The <code>void</code> pointer''', or '''<code>void*</code>''', is supported in ANSI C and C++ as a generic pointer type. A pointer to <code>void</code> can store an address to any non-function data type, and, in C, is implicitly converted to any other pointer type on assignment, but it must be explicitly cast if dereferenced inline.
| |
| [[The C Programming Language (book)|K&R]] C used <code>char*</code> for the “type-agnostic pointer” purpose (before ANSI C).
| |
| | |
| <source lang="C">
| |
| int x = 4;
| |
| void* q = &x;
| |
| int* p = q; /* void* implicitly converted to int*: valid C, but not C++ */
| |
| int i = *p;
| |
| int j = *(int*)q; /* when dereferencing inline, there is no implicit conversion */
| |
| </source>
| |
| | |
| C++ does not allow the implicit conversion of <code>void*</code> to other pointer types, even in assignments. This was a design decision to avoid careless and even unintended casts, though most compilers only output warnings, not errors, when encountering other ill casts.
| |
| | |
| <source lang="Cpp">
| |
| int x = 4;
| |
| void* q = &x;
| |
| // int* p = q; This fails in C++: there is no implicit conversion from void*
| |
| int* a = (int*)q; // C-style cast
| |
| int* b = static_cast<int*>(q); // C++ cast
| |
| </source>
| |
| | |
| In C++, there is no <code>void&</code> (reference to void) to complement <code>void*</code> (pointer to void), because references behave like aliases to the variables they point to, and there can never be a variable whose type is <code>void</code>.
| |
| | |
| ===C#===
| |
| In the [[C Sharp (programming language)|C# programming language]], pointers are supported only under certain conditions: any block of code including pointers must be marked with the <code>unsafe</code> keyword. Such blocks usually require higher security permissions than pointerless code to be allowed to run.
| |
| The syntax is essentially the same as in C++, and the address pointed can be either [[Managed code|managed]] or [[Managed code|unmanaged]] memory. However, pointers to managed memory (any pointer to a managed object) must be declared using the <code>fixed</code> keyword, which prevents the [[Garbage collection (computer science)|garbage collector]] from moving the pointed object as part of memory management while the pointer is in scope, thus keeping the pointer address valid.
| |
| | |
| An exception to this is from using the <code>IntPtr</code> structure, which is a safe managed equivalent to <code>int*</code>, and does not require unsafe code. This type is often returned when using methods from the <code>System.Runtime.InteropServices</code>, for example:
| |
| | |
| <source lang="csharp">
| |
| // Get 16 bytes of memory from the process's unmanaged memory
| |
| IntPtr pointer = System.Runtime.InteropServices.Marshal.AllocHGlobal(16);
| |
| | |
| // Do something with the allocated memory
| |
| | |
| // Free the allocated memory
| |
| System.Runtime.InteropServices.Marshal.FreeHGlobal(pointer);
| |
| </source>
| |
| | |
| The [[Microsoft .NET|.NET]] framework includes many classes and methods in the <code>System</code> and <code>System.Runtime.InteropServices</code> namespaces (such as the <code>Marshal</code> class) which convert .NET types (for example, <code>System.String</code>) to and from many [[Managed code|unmanaged]] types and pointers (for example, <code>LPWSTR</code> or <code>void *</code>) to allow communication with [[Managed code|unmanaged code]].
| |
| | |
| ===COBOL===
| |
| The [[COBOL]] programming language supports pointers to variables. Primitive or group (record) data objects declared within the <code>LINKAGE</code> <code>SECTION</code> of a program are inherently pointer-based, where the only memory allocated within the program is space for the address of the data item (typically a single memory word). In program source code, these data items are used just like any other <code>WORKING-STORAGE</code> variable, but their contents are implicitly accessed indirectly through their <code>LINKAGE</code> pointers.
| |
| | |
| Memory space for each pointed-to data object is typically [[dynamic memory allocation|allocated dynamically]] using external [[subroutine|<code>CALL</code>]] statements or via embedded extended language constructs such as [[CICS|<code>EXEC</code> <code>CICS</code>]] or [[SQL|<code>EXEC</code> <code>SQL</code>]] statements.
| |
| | |
| Extended versions of COBOL also provide pointer variables declared with <code>USAGE</code> <code>IS</code> <code>POINTER</code> clauses. The values of such pointer variables are established and modified using <code>SET</code> and <code>SET</code> <code>ADDRESS</code> statements.
| |
| | |
| Some extended versions of COBOL also provide <code>PROCEDURE-POINTER</code> variables, which are capable of storing the [[function pointer|addresses of executable code]].
| |
| | |
| ===PL/I===
| |
| The [[PL/I]] language provides full support for pointers to all data types (including pointers to structures), [[recursion]], [[Computer multitasking|multitasking]], string handling, and extensive built-in [[function (computer science)|function]]s. PL/I was quite a leap forward compared to the programming languages of its time.
| |
| | |
| ===D===
| |
| The [[D programming language]] is a derivative of C and C++ which fully supports C pointers and C typecasting.
| |
| | |
| ===Eiffel===
| |
| The [[Eiffel (language)|Eiffel object-oriented language]] supports pointers in the form of [[Reference (computer science)|references]], which are typed and do not allow any form of pointer arithmetic. The [[Ecma International|ECMA]] standard for Eiffel includes an "attached type" mechanism that claims to guarantee [[void safety]].
| |
| | |
| ===Fortran===
| |
| [[Fortran|Fortran-90]] introduced a strongly typed pointer capability. Fortran pointers contain more than just a simple memory address. They also encapsulate the lower and upper bounds of array dimensions, strides (for example, to support arbitrary array sections), and other metadata. An ''association operator'', <code>=></code> is used to associate a <code>POINTER</code> to a variable which has a <code>TARGET</code> attribute. The Fortran-90 <code>ALLOCATE</code> statement may also be used to associate a pointer to a block of memory. For example, the following code might be used to define and create a linked list structure:
| |
| <source lang="Fortran">
| |
| type real_list_t
| |
| real :: sample_data(100)
| |
| type (real_list_t), pointer :: next => null ()
| |
| end type
| |
| | |
| type (real_list_t), target :: my_real_list
| |
| type (real_list_t), pointer :: real_list_temp
| |
| | |
| real_list_temp => my_real_list
| |
| do
| |
| read (1,iostat=ioerr) real_list_temp%sample_data
| |
| if (ioerr /= 0) exit
| |
| allocate (real_list_temp%next)
| |
| real_list_temp => real_list_temp%next
| |
| end do
| |
| </source>
| |
| | |
| Fortran-2003 adds support for procedure pointers. Also, as part of the ''C Interoperability'' feature, Fortran-2003 supports intrinsic functions for converting C-style pointers into Fortran pointers and back.
| |
| | |
| ===Go===
| |
| [[Go (programming language)|Go]] has pointers. Its declaration syntax is equivalent to that of C, but written the other way around, ending with the type. Unlike C, Go has garbage collection, and disallows pointer arithmetic. Reference types, like in C++, do not exist. Some built-in types, like maps and channels, are boxed (i.e. internally they are pointers to mutable structures), and are initialized using the <code>make</code> function. As a different (than reference types) approach to unified syntax between pointers and non-pointers, the arrow (<code>-></code>) operator has been dropped—it is possible to use the dot operator directly on a pointer to a data type to access a field or method of the dereferenced value, as if the dot operator were used on the underlying data type. This, however, only works with 1 level of indirection.
| |
| | |
| ===Java===
| |
| Unlike [[C (programming language)|C]], [[C++]], or [[Pascal (programming language)|Pascal]], there is no explicit representation of pointers in [[Java (programming language)|Java]]. Instead, more complex data structures like [[Object-oriented programming|objects]] and [[Array data structure|arrays]] are implemented using [[reference (computer science)|references]]. The language does not provide any explicit pointer manipulation operators. It is still possible for code to attempt to dereference a null reference (null pointer), however, which results in a run-time [[exception handling|exception]] being thrown. The space occupied by unreferenced memory objects is recovered automatically by [[garbage collection (computer science)|garbage collection]] at run-time.<ref>Nick Parlante, [http://cslibrary.stanford.edu/102/PointersAndMemory.pdf#%22Pointers%20and%20Memory%22], [http://cslibrary.stanford.edu# Stanford Computer Science Education Library], pp. 9–10 (2000).</ref>
| |
| | |
| ===Modula-2===
| |
| Pointers are implemented very much as in Pascal, as are <code>VAR</code> parameters in procedure calls. [[Modula-2]] is even more strongly typed than Pascal, with fewer ways to escape the type system. Some of the variants of Modula-2 (such as [[Modula-3]]) include garbage collection.
| |
| <!-- +++assistance is needed here+++ -->
| |
| | |
| ===Oberon===
| |
| Much as with Modula-2, pointers are available. There are still fewer ways to evade the type system and so [[Oberon (programming language)|Oberon]] and its variants are still safer with respect to pointers than Modula-2 or its variants. As with [[Modula-3]], garbage collection is a part of the language specification.
| |
| <!-- +++assistance is needed here+++ -->
| |
| | |
| ===Pascal===
| |
| Unlike many languages that feature pointers, standard [[International Organization for Standardization|ISO]] [[Pascal (programming language)|Pascal]] only allows pointers to reference dynamically created variables that are anonymous and does not allow them to reference standard static or local variables.<ref>ISO 7185 Pascal Standard (unofficial copy), section [http://standardpascal.org/iso7185.html#6.4.4%20Pointer-types 6.4.4 Pointer-types] and subsequent.</ref> It does not have pointer arithmetic. Pointers also must have an associated type and a pointer to one type is not compatible with a pointer to another type (e.g. a pointer to a char is not compatible with a pointer to an integer). This helps eliminate the type security issues inherent with other pointer implementations, particularly those used for [[PL/I]] or [[C (Programming Language)|C]]. It also removes some risks caused by [[dangling pointers]], but the ability to dynamically let go of referenced space by using the <code>dispose</code> standard procedure (which has the same effect as the <code>free</code> library function found in [[C (Programming Language)|C]]) means that the risk of dangling pointers has not been entirely eliminated.<ref>J. Welsh, W. J. Sneeringer, and C. A. R. Hoare, "Ambiguities and Insecurities in Pascal," ''Software Practice and Experience 7'', pp. 685–696 (1977)</ref>
| |
| | |
| However, in some commercial and open source Pascal (or derivatives) compiler implementations —like [[Free Pascal]],<ref>Free Pascal Language Reference guide, section [http://www.freepascal.org/docs-html/ref/refse15.html#x43-490003.4 3.4 Pointers]</ref> [[Turbo Pascal]] or the [[Object Pascal]] in [[Embarcadero Delphi]]— a pointer is allowed to reference standard static or local variables and can be cast from one pointer type to another. Moreover pointer arithmetic is unrestricted: adding or subtracting from a pointer moves it by that number of bytes in either direction, but using the <code>Inc</code> or <code>Dec</code> standard procedures with it moves the pointer by the size of the [[datatype]] it is ''declared'' to point to.
| |
| | |
| ===Perl===
| |
| The [[Perl]] [[programming language]] supports pointers, although rarely used, in the form of the pack and unpack functions. These are intended only for simple interactions with compiled OS libraries. In all other cases, Perl uses [[Reference (computer science)|references]], which are typed and do not allow any form of pointer arithmetic. They are used to construct complex data structures.<ref>[http://perldoc.perl.org/perlref.html#Making-References // Making References (Perl References and nested data structures)]</ref>
| |
| | |
| ==See also==
| |
| *[[Address constant]]
| |
| *[[Bounded pointer]]
| |
| *[[Buffer overflow]]
| |
| *[[Function pointer]]
| |
| *[[Hazard pointer]]
| |
| *[[Opaque pointer]]
| |
| *[[Pointer swizzling]]
| |
| *[[Reference (computer science)]]
| |
| *[[Static code analysis]]
| |
| *[[Storage violation]]
| |
| *[[Variable (programming)]]
| |
| | |
| ==References==
| |
| {{Reflist|30em}}
| |
| | |
| ==External links==
| |
| {{Commons category|Pointers (computing)}}
| |
| {{Wikiversity|Pointers}}
| |
| {{Wikibooks|C Programming|Pointers}}
| |
| *[http://cslibrary.stanford.edu/ Pointers and Memory] Introduction to pointers – Stanford Computer Science Education Library
| |
| *[http://0pointer.de/ 0pointer.de] A terse list of minimum length source codes that dereference a null pointer in several different programming languages
| |
| *[http://publications.gbdirect.co.uk/c_book/chapter5/pointers.html "The C book" – containing pointer examples in ANSI C]
| |
| *{{cite book | ref = c-std | last = Joint Technical Committe ISO/IEC JTC 1, Subcommittee SC 22, Working Group WG 14 | title = International Standard ISO/IEC 9899 | date = 2007-09-08<!-- 19:22:32 +0000 -->| url = http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf | format = PDF | format = Committe Draft}}.
| |
| | |
| {{Data types|state=uncollapsed}}
| |
| {{Nulls}}
| |
| | |
| {{DEFAULTSORT:Pointer (Computing)}}
| |
| [[Category:Articles with example C code]]
| |
| [[Category:Data types]]
| |
| [[Category:Primitive types]]
| |
| [[Category:American inventions]]
| |
| | |
| [[sv:Datatyp#Pekare och referenstyper]]
| |