FACTOID # 14: North Carolina has a larger Native American population than North Dakota, South Dakota and Montana combined.
 Home   Encyclopedia   Statistics   States A-Z   Flags   Maps   FAQ   About 


FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:



(* = Graphable)



Encyclopedia > Dangling pointer

Dangling pointers in programming are pointers whose objects have since been deleted or deallocated, without modifying the value of the pointer. In many languages (particularly the C programming language), deleting an object from memory does not alter any associated pointers. The pointer still points to the location in memory where the object or data was, even though the object or data has since been deleted and the memory may now be used for other purposes. A pointer in such a situation is called a dangling pointer. Computer programming (often simply programming) is the craft of implementing one or more interrelated abstract algorithms using a particular programming language to produce a concrete computer program. ... It has been suggested that wild pointer be merged into this article or section. ... The C Programming Language, Brian Kernighan and Dennis Ritchie, the original edition that served for many years as an informal specification of the language The C programming language is a standardized imperative computer programming language developed in the early 1970s by Ken Thompson and Dennis Ritchie for use on the...

Using a dangling pointer under the assumption that the object it points to is still valid can cause unpredictable behavior. This is because the program may not terminate as soon as the dangling pointer is used: the data referenced by the dangling pointer may not immediately be reused for other purposes. The use of dangling pointers can also result in the silent corruption of unrelated data. To avoid bugs of this kind, one common programming technique is to set pointers to the null pointer once the storage they point to has been released. When the null pointer is dereferenced the program will immediately terminate — there is no potential for data corruption or unpredictable behavior. This makes the underlying programming mistake easier to find and resolve. In the C Programming Language, a null pointer is a special pointer which is guaranteed to compare unequal to a pointer to any object or function. ...

The following example code (in C++) shows a dangling pointer: C++ (pronounced see plus plus) is a general-purpose computer programming language. ...

 #include <iostream> #include <string> using namespace std; // the string object is in the "std" namespace int main(void) { // create a pointer to a string object containing "This is a string." string *stringPointer = new string("This is a string."); // display the address of the string and its value cout << stringPointer << ": " << *stringPointer << endl; // the string has now been deleted; however, stringPointer // still points to the string's former location in memory delete stringPointer; // display the address (unchanged) and the new value; this will // likely crash the program or cause unpredictable behaivor cout << stringPointer << ": " << *stringPointer << endl; return 0; } 

A dangling pointer (also known as a wild pointer) is a pointer, which does not point to a valid memory location. By validity of a location, we mean that a running process has certain restrictions on accessing memory locations that do not fall under its address space.

A pointer not handled properly can produce serious bugs or a badly behaving program. Dangling pointers get, or can be, created in several ways. The following list gives you an idea about dangling pointers: their sources of creation, methods of prevention and effects in C.

A straightforward example can be the following one:

 { char *cp = NULL; /* ... */ { char c; cp = &c; } /* The memory location, which c was occupying, is released here */ /* cp here is now a dangling pointer */ } 

In the above, a better solution to avoid the dangling pointer is to make cp a null pointer after the inner block is exited.

The design philosophy of C make a compiler to believe that the programmer knows what he is doing. Though a code analysis tool, like lint, can help in finding potential programming mistakes, it is up to the programmer to ensure a good behaving program. As stated earlier, misapplied pointers can create a badly behaving program. Following paragraph points up an example. Lint is a computer programming tool that performs the lexical and syntactic portions of the compilation with substantial additional checks, noting when variables had been used before being set, when they were used as a datatype other than that of their definition, and numerous other programming errors. ...

A dangling pointer in a program, by definition, points to a memory location outside the process space. The location pointed to by the dangling pointer may or may not contain a valid object. If modified, the valid object's value will change unexpectedly, distorting the performance of the process owning the object. This condition is called memory corruption. This could lead the system's state into a vicious circle, crashing it ultimately.

A clear-cut technique to avoid dangling pointers is to initialize them to NULL, whenever they are declared and no more required.

A common programming misstep to create a dangling pointer is returning the address of a local variable.

 char * func ( void ) { char ca[] = "Pointers and Arrays - II"; /* ... */ return ca; } 

In the above, if it is required to return the address of ca, declare it with the static storage specifier.

A frequent source of creating dangling pointers is a jumbled combination of malloc() and free() library calls. A pointer becomes dangling, when the block of memory pointed it is freed.

 #include <stdlib.h> { char *cp = malloc ( A_CONST ); /* ... */ free ( cp ); /* cp now becomes a dangling pointer */ cp = NULL; /* cp is no longer dangling */ /* ... */ } 

This is a another example of a dangling pointer:

 unsigned int *i = 0; 

for (; i < 0xFFFFFFFC; i = i + 4) { *i = 0; }

For more information on pointers in C, visit: Pointers and Arrays (C99)

  Results from FactBites:
Firm finds danger in dangling pointers | The Register (607 words)
Known as a dangling, or wild, pointer, the flaw was caused by an IIS service process that attempted to use memory that was already freed by the program.
The discovery means that developers and security researchers need to start considering dangling pointers as security problems as serious as buffer overflows, said Danny Allan, director of security research for Watchfire.
A dangling pointer is a software bug that occurs when a programmer creates an object in memory, uses the object, frees up the memory for reuse and then mistakenly tries to access the object again.
  More results at FactBites »



Share your thoughts, questions and commentary here
Your name
Your comments

Want to know more?
Search encyclopedia, statistics and forums:


Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms, 1022, m