Editing Pointer
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
''This article is intended to provide an introduction to and critical analysis of the concept of a pointer. Despite the samples of code shown below for various languages, it is not intended to provide a reference on how to use pointers in specific languages.'' | ''This article is intended to provide an introduction to and critical analysis of the concept of a pointer. Despite the samples of code shown below for various languages, it is not intended to provide a reference on how to use pointers in specific languages.'' | ||
− | A '''pointer''' is an object that contains information about the address of another object in memory (called the '''referent''' or the '''pointee''') and which may be used to access that object. Accessing an object using a pointer to it rather than by referring to the object itself is known as '''indirection'''; using a pointer is said to ''add a level of indirection'' | + | A '''pointer''' is an object that contains information about the address of another object in memory (called the '''referent''' or the '''pointee''') and which may be used to access that object. Accessing an object using a pointer to it rather than by referring to the object itself is known as '''indirection'''; using a pointer is said to ''add a level of indirection''. |
==Definition== | ==Definition== | ||
Line 142: | Line 142: | ||
A a; | A a; | ||
B b; | B b; | ||
− | void (A::*p)() = &A::f; | + | void (A::*p)(int) = &A::f; |
(a.*p)(); | (a.*p)(); | ||
(b.*p)(); | (b.*p)(); | ||
Line 197: | Line 197: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | It is clear from this example that <code>f</code> and <code>g</code> are not value ''instances'' of the class <code>Foo</code>; if they were, then the statement <code>g = f;</code> would create a copy of <code>f</code>. Instead, <code>f</code> and <code>g</code> are like pointers with an anonymous referent constructed by the <code>new</code> invocation, so that after <code>f</code> is assigned to <code>g</code>, the two pointers will have the same referent. | + | It is clear from this example that <code>f</code> and <code>g</code> are not value ''instances'' of the class <code>Foo</code>; if they were, then the statement <code>g = f;</code> would create a copy of <code>f</code>. Instead, <code>f</code> and <code>g</code> are like pointers with an anonymous referent constructed by the <code>new</code> invocation, so that after <code>f</code> is assigned to <code>g</code>, the two pointers will have the same referent. On the other hand, in semantics, <code>f</code> and <code>g</code> are more like C++ references; it would not make sense to use <code>&</code> and <code>*</code> operators because these Java pointers do not exist independently of their referents; only one or the other is exposed in the language proper. (Primitive types such as <code>int</code> cannot be referred to with pointers, and they cannot be passed by reference.) |
− | + | One can also argue about whether Java passes parameters by value or by reference. If we insist that Java passes all parameters by value, then we must accept that <code>f</code> and <code>g</code> have pointer nature, because if <code>f</code> is passed into a function, and the parameter that receives it is reassigned via the assignment operator, then <code>f</code> is unchanged, but if a method is called on the parameter that mutates a data member of <code>Foo</code>, then those changes will be reflected in <code>f</code>, which is just what we would expect if a C++ pointer were passed (and the method called with the <code>-></code> operator). However, the lack of independent existence of pointers and their referents is taken by some to imply that Java cannot really said to possess pointers. Thus, there is considerable controversy over the question of whether Java has pointers. | |
− | + | The names <code>f</code> and <code>g</code>, at any rate, probably do not refer to fixed memory addresses; this stands in contrast to C, where pointers probably ''do'' refer to fixed memory addresses (but this is not required by the standard). This is because Java's automatic memory management is allowed to move values around in memory, and "pointers" must therefore be defined in a way that allows them to follow this movement. | |
− | < | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | </ | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | </ | + | |
− | + | ||
− | + | ||
− | + | ||
<!-- TODO: Talk about Lisp, Scheme, Haskell, ML, and Perl --> | <!-- TODO: Talk about Lisp, Scheme, Haskell, ML, and Perl --> | ||
− | |||
===Disk (and other) pointers=== | ===Disk (and other) pointers=== | ||
File systems that organize data on disks also use pointers to keep track of where on the disk files are located; however, these are typically specialized data structures handled by an operating system kernel, and do not appear in application programs or in mainstream programming languages. These ''disk pointers'' are usually referred to as such to distinguish them from pointers into primary storage and virtual memory, which are simply called ''pointers''. | File systems that organize data on disks also use pointers to keep track of where on the disk files are located; however, these are typically specialized data structures handled by an operating system kernel, and do not appear in application programs or in mainstream programming languages. These ''disk pointers'' are usually referred to as such to distinguish them from pointers into primary storage and virtual memory, which are simply called ''pointers''. | ||
The term ''memory'' can, in general, refer not only to primary storage, disks, and virtual memory, but also to CPU registers, cache, and other machines (as in a network cluster). Nevertheless, the term ''pointer'' is very rarely used to describe objects that address these other forms of memory. For example, an IP address is not referred to as a pointer. | The term ''memory'' can, in general, refer not only to primary storage, disks, and virtual memory, but also to CPU registers, cache, and other machines (as in a network cluster). Nevertheless, the term ''pointer'' is very rarely used to describe objects that address these other forms of memory. For example, an IP address is not referred to as a pointer. |