Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Pointer (computer programming)
(section)
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.
Anti-spam check. Do
not
fill this in!
== Special kinds of pointers == === Kinds defined by value === ====Null pointer==== {{Main|Null pointer}} A '''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]]. ====Dangling pointer==== {{Main|Dangling pointer}} A '''dangling pointer''' is a pointer that does not point to a valid object and consequently 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 dangling pointer: <syntaxhighlight lang="C"> int func(void) { char *p1 = malloc(sizeof(char)); /* (undefined) value of some place on the heap */ char *p2; /* dangling (uninitialized) pointer */ *p1 = 'a'; /* This is OK, assuming malloc() has not returned NULL. */ *p2 = 'b'; /* This invokes undefined behavior */ } </syntaxhighlight> 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|function which doesn't return anything]] 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. In other words, a wild branch is a function pointer that is wild (dangling). 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. === Kinds defined by structure === ==== Autorelative pointer ==== An '''autorelative pointer''' is a pointer whose value is interpreted as an offset from the address of the pointer itself; thus, if a data structure has an autorelative pointer member that points to some portion of the data structure itself, then the data structure may be relocated in memory without having to update the value of the auto relative pointer.<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: * to mean an offset from the address of a structure rather than from the address of the pointer itself;{{citation needed|date=September 2011}} * 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>{{cite web|url=http://msdn.microsoft.com/en-us/library/57a97k4e.aspx |title=Based Pointers |publisher=Msdn.microsoft.com |access-date=2018-04-13}}</ref> === Kinds defined by use or datatype === ====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: <syntaxhighlight lang="C"> struct element { struct element *next; int value; }; struct element *head = NULL; </syntaxhighlight> 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: <syntaxhighlight 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); </syntaxhighlight> 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++|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 executable code, i.e., it can point to a function, method, or procedure. A [[function pointer]] will store the address of a function to be invoked. While this facility can be used to call functions dynamically, it is often a favorite technique of virus and other malicious software writers. <syntaxhighlight lang="C"> int sum(int n1, int n2) { // Function with two integer parameters returning an integer value return n1 + n2; } int main(void) { int a, b, x, y; 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 } </syntaxhighlight> ====Back pointer==== In doubly [[linked list]]s or [[tree (data structure)|tree structures]], a back pointer held on an element 'points back' to the item referring to the current element. These are useful for navigation and manipulation, at the expense of greater memory use.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)