Editing Pointer

Jump to: navigation, search

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''. When we use a particular pointer to access its referent, we say that we are '''dereferencing''' the pointer.
+
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.)
  
When an object is passed into a function, it is as though a pointer to the object's data were passed into a C function. Hence, if a function takes a parameter <code>Foo f</code>, then assigning to <code>f</code> inside the function will simply rebind the name, and leave the original argument untouched; but any modification to <code>f</code>'s members will be reflected in the original argument, as it entails the dereferencing of this pointer.
+
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.
  
Whereas in C we may have something like this:
+
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.
<syntaxhighlight lang="c">
+
typedef struct node
+
{
+
    int value;
+
    struct node* left;
+
    struct node* right;
+
} node;
+
</syntaxhighlight>
+
the equivalent code in Java would be:
+
<syntaxhighlight lang="java">
+
class Node
+
{
+
  public:
+
    int value;
+
    Node left;
+
    Node right;
+
}
+
</syntaxhighlight>
+
In the sense that pointers are necessary for the implementation of linked data structures, Java does have pointers; they are simply not presented as such to the programmer. The question is whether an entity can be called a pointer when its referent cannot be independently exposed in the language (in that it is impossible to pass an object "by value"). Also, there is no way to pass a primitive type such as <code>int</code> by reference, as would be done in C with a pointer to int; the int must instead be wrapped in an object. The inability of the programmer to create a reference to some data types may be a reason for saying that the language does not have pointers.
+
 
+
Of course, the knowledge of how to achieve equivalent effects to C's "pass by value" and "pass by reference" in Java and languages with similar reference semantics is far more important than a squabble over whether a language "has pointers" or not.
+
 
<!-- 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.

Please note that all contributions to PEGWiki are considered to be released under the Attribution 3.0 Unported (see PEGWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)