How to avoid dangling pointers in C.


A hanging pointer In computer science, the term “tangling pointer” refers to a pointer that contains an invalid value and thus refers to a dynamic memory area (also called a heap) that does not exist or is not assigned to the pointer.[1]

Table of Contents


Stuck pointers often arise because they have not been initialized (in this case, alternatively wild pointer called) - but also by referring to a memory area that has already been released.[2] Hanging pointers can have unpredictable effects on the program run and cause the program to crash.[3][4]

While userspace programs usually terminate 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 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 also 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.


C ++

#include usingnamespacestd; intmain () {int * pPointer = newint; // pointer of type integer created and memory reserved in the heap * pPointer = 10; // write 10 into the heap, to the memory address where the pointer points cout << pPointer; // Displays the memory address in the heap << * pPointer; // Access the memory address in the heap, read it there and then display it ("10") deletepPointer; // Free memory on the heap cout << pPointer; / / The pointer variable still contains the memory address, this is displayed again // => pPointer is now a dangling pointercout << * pPointer; // (*) Read access via pointer to released memory area // => that (hopefully) creates one Program termination; if not, processing continues with // invalid data * pPointer = 20; // (*) Write access via pointer to released memory area // => even worse: that would (or can) overwrite other valid data pPointer = 0; // => pPointer is no longer a dangling pointer, it is now a null pointer return0;}

It shows good programming style, according to one delete to set the pointer to 0, even if the pointer is no longer accessed.[5]

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 Internet Archive)  Info: The archive link was inserted automatically and has not yet been 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. (Vagabond Pointers) Retrieved August 22, 2014.
  3. Garbage collection in C ++ - 2.1 Problems with traditional memory management - Stuck pointers Retrieved August 22, 2014.
  4. Security Insider - Crashed web server due to invalid pointers Retrieved August 23, 2014.
  5. Pointer. (Memento from November 22, 2013 in Internet Archive) In: C ++ in 21 days. ISBN 978-3-8272-5624-9. (Listing 8.9: Creating a Stray Pointer) Retrieved January 19, 2021.


Status of information: 02/23/2021 05:46:55 AM CET

Source: Wikipedia (authors [version history]) License: CC-BY-SA-3.0

Changes: All images and most of the design elements associated with them have been removed. Some of the icons have been replaced by FontAwesome icons. Some templates have been removed (such as "Article worth reading", "Excellent article") or rewritten. Most of the CSS classes have been removed or standardized.
Wikipedia-specific links that do not lead to articles or categories (such as "Redlink", "Edit links", "Portal links") have been removed. All external links have an additional FontAwesome icon. In addition to other small design adjustments, media containers, maps, navigation boxes, spoken versions and geo-microformats have been removed.

Important NOTE Since the given content was automatically taken over from Wikipedia at the specified time, manual checking was and is not possible. therefore does not guarantee the correctness and topicality of the content taken over. If the information is incorrect in the meantime or there are errors in the presentation, we ask you to contact us by: E-Mail.
Also note:Imprint & privacy policy.