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. 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. The following example code (in C++) shows a dangling pointer:
  #include   #include       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;  } 

 

<< PreviousWord BrowserNext >>
neil kirton
risk theory
mount morgan
gergesa
gogol bordello
independence rock (wyoming)
east asian calligraphy
independence rock
capillarity
keith edwards
peter mccardle
timeline (movie)
007 racing
jewish quota
king gogukcheon of goguryeo
hautefort
zorka of montenegro
tony campolo
thomas danforth
black match
for squirrels
glass wool
magicant
king nulji of silla
william james linton
takatsukasa family
thomas frank
fall out fall in
true path party
goanna
self control
skam records
anchieta, es, brazil
wood engraving
watmm
swing revival
apiaca, es, brazil
reg watson
atilio vivaqua
branigan 2
baixo guandu, es, brazil
barra de sao francisco, es, brazil
branigan
boa esperanca, es, brazil