Hanging pointer

from Wikipedia, the free encyclopedia

A dangling pointer ( English dangling pointer ) referred to in the computer science a pointer , which contains an invalid value, and thereby a non-existent or not associated with the pointer dynamic memory area (also called heap) refers.

background

Stuck pointers often arise because they have not been initialized (in this case they are alternatively called wild pointers ) - but also because they refer to a memory area that has already been released. Hanging pointers can have unpredictable effects on the program run and cause the program to crash .

While userspace programs are usually terminated when a pointer that points to an invalid memory area is dereferenced, such a pointer in the kernel or its modules can in the worst case damage the entire system without the user noticing anything before it is too late , since there is no control instance that z. B. could prevent the overwriting of foreign code. It is therefore important to ensure that they are used correctly when developing the kernel and driver .

The unpredictability arises from the fact that access to a memory area that has already been released does not necessarily immediately trigger a runtime error ( protection violation ), since a certain amount of time can pass between the memory release by the programmer and the actual release by the runtime system; if necessary, the memory area has now been reassigned and has actually been reassigned to the process, but in a different semantic context. If there is an access to (still) accessible (released) memory (lines (*) in the example), it does not trigger an error. However, since this cannot be reproduced, these sporadic errors are particularly difficult to detect.

example

C ++

#include <iostream>

using namespace std;

int main()
{
    int * pPointer = new int; // Pointer vom Typ integer angelegt und Speicher im Heap reservieren
    *pPointer = 10;           // 10 in den Heap schreiben, an die Speicheradresse, wohin der Pointer zeigt
    cout << pPointer;         // Zeigt die Speicheradresse im Heap an
    cout << *pPointer;        // Auf die Speicheradresse im Heap zugreifen, dort lesen und dann anzeigen ("10")
    delete pPointer;          // Speicher auf dem Heap freigeben
    cout << pPointer;         // Die Zeigervariable enthält noch immer die Speicheradresse, diese wird erneut angezeigt
                              // => pPointer ist nun ein Dangling Pointer
    cout << *pPointer;        // (*) Lesender Zugriff über Pointer auf freigegebenen Speicherbereich
                              // => das erzeugt (hoffentlich) einen Programmabbruch; wenn nicht, wird mit
                              // ungültigen Daten weitergearbeitet
    *pPointer = 20;           // (*) Schreibender Zugriff über Pointer auf freigegebenen Speicherbereich
                              // => noch schlimmer: das würde (bzw. kann) andere gültige Daten überschreiben
    pPointer = 0;             // => pPointer ist jetzt kein Dangling Pointer mehr, er ist nun ein Nullpointer
    return 0;
}

It shows good programming style to set the pointer to 0 after a delete , even if the pointer is no longer accessed.

See also

Web links

Individual evidence

  1. 12. Dynamic memory management - 12.3. Notes on using malloc, calloc and free Retrieved August 22, 2014.
  2. Pointer. ( Memento of the original from November 22, 2013 in the Internet Archive ) Info: The archive link was automatically inserted and not yet checked. Please check the original and archive link according to the instructions and then remove this notice. In: C ++ in 21 days. ISBN 978-3-8272-5624-9 . (Vagrant Pointers) Retrieved August 22, 2014.  @1@ 2Template: Webachiv / IABot / www.informit.de
  3. Garbage Collection in C ++ - 2.1 Problems with Traditional Memory Management - Stuck Pointer. Retrieved August 22, 2014.
  4. Security Insider - Web server crashed by invalid pointers.Retrieved August 23, 2014.
  5. Pointer. ( Memento of the original from November 22, 2013 in the Internet Archive ) Info: The archive link was automatically inserted and not yet checked. Please check the original and archive link according to the instructions and then remove this notice. In: C ++ in 21 days. ISBN 978-3-8272-5624-9 . (Listing 8.9: Creating a stray pointer) Retrieved August 22, 2014.  @1@ 2Template: Webachiv / IABot / www.informit.de